mechanica-shared 2.0.0-alpha.8 → 2.0.0-alpha.9

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.
package/dist/index.js CHANGED
@@ -479,7 +479,10 @@ async function generatePage(options) {
479
479
  index = index.slice(0, start) + rendered + index.slice(end);
480
480
  const links = options.pageLinks?.(options.state.content) ?? [];
481
481
  if (links.length) index = index.replace("</head>", `${links.join("\n")}\n</head>`);
482
- if (options.assetsUrl) index = index.replace(/\/assets\//g, options.assetsUrl);
482
+ if (options.assetsUrl) {
483
+ const base = options.assetsUrl.replace(/\/+$/, "");
484
+ index = index.replace(/(["'(=])\/assets\//g, (_, edge) => `${edge}${base}/assets/`);
485
+ }
483
486
  const stateScript = `<script>window.state=${serializeState(state)}<\/script>`;
484
487
  return {
485
488
  html: index.replace("</body>", `${stateScript}\n</body>`),
@@ -56,7 +56,14 @@ export interface GeneratePageOptions {
56
56
  locales?: LocalesConfig;
57
57
  baseUrl?: string;
58
58
  path?: string;
59
- /** Rewrite `/assets/` to this base when set. */
59
+ /**
60
+ * Serve build assets from this base URL (a CDN origin like
61
+ * `https://cdn.example.com`). Root-relative `/assets/…` references in the
62
+ * HTML are prefixed with it — `/assets/x.js` → `<assetsUrl>/assets/x.js` — so
63
+ * the built `assets/` folder can live on a CDN. A trailing slash is ignored,
64
+ * and an already-absolute `https://…/assets/` URL is left untouched. Uploaded
65
+ * media (`/media/…`) is rewritten by the export's `onFile`, not here.
66
+ */
60
67
  assetsUrl?: string;
61
68
  /**
62
69
  * Extra `<link>` tags for this page's content, injected before `</head>` —
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanica-shared",
3
- "version": "2.0.0-alpha.8",
3
+ "version": "2.0.0-alpha.9",
4
4
  "description": "DOM-free types, schema helpers and page-generation core shared across Mechanica",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -64,7 +64,14 @@ export interface GeneratePageOptions {
64
64
  locales?: LocalesConfig
65
65
  baseUrl?: string
66
66
  path?: string
67
- /** Rewrite `/assets/` to this base when set. */
67
+ /**
68
+ * Serve build assets from this base URL (a CDN origin like
69
+ * `https://cdn.example.com`). Root-relative `/assets/…` references in the
70
+ * HTML are prefixed with it — `/assets/x.js` → `<assetsUrl>/assets/x.js` — so
71
+ * the built `assets/` folder can live on a CDN. A trailing slash is ignored,
72
+ * and an already-absolute `https://…/assets/` URL is left untouched. Uploaded
73
+ * media (`/media/…`) is rewritten by the export's `onFile`, not here.
74
+ */
68
75
  assetsUrl?: string
69
76
  /**
70
77
  * Extra `<link>` tags for this page's content, injected before `</head>` —
@@ -136,7 +143,12 @@ export async function generatePage(
136
143
  const links = options.pageLinks?.(options.state.content) ?? []
137
144
  if (links.length) index = index.replace('</head>', `${links.join('\n')}\n</head>`)
138
145
 
139
- if (options.assetsUrl) index = index.replace(/\/assets\//g, options.assetsUrl)
146
+ if (options.assetsUrl) {
147
+ const base = options.assetsUrl.replace(/\/+$/, '')
148
+ // Prefix only genuine root-relative refs (right after a quote/paren/equals),
149
+ // so an already-absolute `https://…/assets/` URL is never double-prefixed.
150
+ index = index.replace(/(["'(=])\/assets\//g, (_, edge) => `${edge}${base}/assets/`)
151
+ }
140
152
 
141
153
  const stateScript = `<script>window.state=${serializeState(state)}</script>`
142
154
  return { html: index.replace('</body>', `${stateScript}\n</body>`), query }