@profullstack/waitlist 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/build.js +28 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@profullstack/waitlist",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Email waitlist for any launch page: a <waitlist-form> web component, a live signup chart, and a static-site builder on @profullstack/css + @profullstack/charts.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/build.js CHANGED
@@ -2,9 +2,10 @@
2
2
  * @profullstack/waitlist/build: write a complete static waitlist site (no server)
3
3
  * for one name, ready for `c0mpute hosting publish <dir> --name <site>`.
4
4
  */
5
- import { mkdir, writeFile, copyFile, readFile } from 'node:fs/promises';
5
+ import { mkdir, writeFile, copyFile, readFile, readdir } from 'node:fs/promises';
6
6
  import { pluginSet, keywordsFor, runPlugins, baseSlot } from './plugins.js';
7
7
  import { createRequire } from 'node:module';
8
+ import { createHash } from 'node:crypto';
8
9
  import { dirname, join } from 'node:path';
9
10
  import { fileURLToPath } from 'node:url';
10
11
  import {
@@ -237,8 +238,11 @@ export async function buildSite(spec, out, { footers = true, images = true, fetc
237
238
  }
238
239
  const foot = footers ? await renderFooters(s, { fetchImpl }) : '';
239
240
  const { slot } = await runPlugins(s, out, { fetchImpl, cacheDir, maxAgeMs });
240
- await writeFile(join(out, 'index.html'), pageHtml(s, { footers: foot, slot }));
241
- await writeFile(join(out, '404.html'), pageHtml(s, { footers: foot, noindex: true, slot }));
241
+ // Assets are cached by browsers and gateways (max-age 1 h), so a page must name the
242
+ // exact build of every script and stylesheet it needs, or a new page runs old code.
243
+ const ver = await versionAssets(out);
244
+ await writeFile(join(out, 'index.html'), withAssetVersion(pageHtml(s, { footers: foot, slot }), ver));
245
+ await writeFile(join(out, '404.html'), withAssetVersion(pageHtml(s, { footers: foot, noindex: true, slot }), ver));
242
246
  await writeFile(join(out, 'robots.txt'), robotsTxt(s));
243
247
  await writeFile(join(out, 'sitemap.xml'), sitemapXml(s));
244
248
  await writeFile(join(out, 'llms.txt'), llmsTxt(s));
@@ -247,6 +251,27 @@ export async function buildSite(spec, out, { footers = true, images = true, fetc
247
251
  return s;
248
252
  }
249
253
 
254
+ /** A short hash of everything in assets/; module imports between assets carry it too. */
255
+ export async function versionAssets(out) {
256
+ const dir = join(out, 'assets');
257
+ const names = (await readdir(dir)).sort();
258
+ const h = createHash('sha256');
259
+ for (const n of names) h.update(n).update(await readFile(join(dir, n)));
260
+ const ver = h.digest('hex').slice(0, 10);
261
+ for (const n of names.filter((x) => x.endsWith('.js'))) {
262
+ const f = join(dir, n);
263
+ const src = await readFile(f, 'utf8');
264
+ const next = src.replace(/(from\s+|import\s*\(\s*)(['"])(\.\/[^'"?]+\.js)\2/g, (_, pre, q, path) => `${pre}${q}${path}?v=${ver}${q}`);
265
+ if (next !== src) await writeFile(f, next);
266
+ }
267
+ return ver;
268
+ }
269
+
270
+ /** assets/x.css -> assets/x.css?v=<ver> in href/src attributes. */
271
+ export function withAssetVersion(html, ver) {
272
+ return html.replace(/((?:href|src)=")(assets\/[^"?#]+)(")/g, `$1$2?v=${ver}$3`);
273
+ }
274
+
250
275
  /** Build many: specs is an array; each lands in `${outRoot}/${name}`. */
251
276
  export async function buildMany(specs, outRoot, opts = {}) {
252
277
  const done = [];