Postman delivers the assets of a webpage
Find a file
2024-07-12 01:51:20 -03:00
src Add improvements 2024-07-12 01:51:20 -03:00
.eslintrc.js Add eslint, prettier, ts-standard 2024-07-12 01:00:04 -03:00
.gitignore Update package-lock.json 2024-07-10 17:08:10 -03:00
.projectile First commit 2024-07-10 11:21:15 -03:00
LICENSE First commit 2024-07-10 11:21:15 -03:00
package.json Add improvements 2024-07-12 01:51:20 -03:00
README.md Update example 2024-07-12 00:35:17 -03:00
tsconfig.json First commit 2024-07-10 11:21:15 -03:00

About

Postman delivers the assets of a web page.
The library is typically paired with a progress bar that reports progress to the client.

Examples

Progress bar

The following example delivers fonts, scripts, images and stylesheets with the help of a progress bar. The progress bar is removed once the delivery is complete:

index.html

<!DOCTYPE html>
<html>
<head>
  <title>Postman</title>
  <script type="module" src="/postman.js"></script>
</head>
<body>
  <div class="postman loader">
    <progress value="0" max="100"></progress>
    <span class="percentage"></span>
   </div>
  </div>
</body>
</html>

postman.js

import postman, { item } from "postman";
document.addEventListener("DOMContentLoaded", () => {
  const bar = document.querySelector("progress");
  const span = document.querySelector(".percentage");
  const delivery = postman(
    item.font("Kanit Regular", "url(/fonts/kanit-regular.ttf)"),
    item.script("/js/app.js"),
    item.image("/images/app.png"),
    item.css("/css/app.css"),
    item.progress((percent) => {
      bar.value = percent;
      span.innerText = `${percent}%`;
    })
  ).deliver();

  delivery.then((package) => {
    /* Add page assets */
    package.fonts.forEach((font) => documents.fonts.add(font));
    package.scripts.forEach((script) => document.body.appendChild(script));
    package.css.forEach((css) => document.head.appendChild(css));
    /* Replace progress bar */
    bar.remove();
    span.remove();
  });
});

Sources

License

BSD Zero Clause
See LICENSE