rosetack 0.1.3 → 0.1.4

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-cli/edge.mjs CHANGED
@@ -21,7 +21,7 @@ function getStyles() {
21
21
  return "";
22
22
  }
23
23
  }
24
- var BOOTSTRAP = esbuild.transformSync(`
24
+ var BOOTSTRAP_SOURCE = `
25
25
  // shell = false: the document's SSR content sits directly in <body> - no
26
26
  // #app wrapper, no demo styles, so a real theme's CSS owns the document from
27
27
  // the first byte (and a JS-less visitor never sees either). The bundle still
@@ -144,16 +144,47 @@ if (__state.__notFound) {
144
144
  } else {
145
145
  start(__app, window.location.pathname);
146
146
  }
147
- `, { minify: true }).code;
147
+ `;
148
+ var BOOTSTRAP_CACHE = null;
149
+ function bootstrap() {
150
+ if (BOOTSTRAP_CACHE === null) {
151
+ if (globalThis.__rosetack_SKIP_MINIFY) {
152
+ BOOTSTRAP_CACHE = BOOTSTRAP_SOURCE;
153
+ } else {
154
+ try {
155
+ BOOTSTRAP_CACHE = esbuild.transformSync(BOOTSTRAP_SOURCE, { minify: true }).code;
156
+ } catch {
157
+ BOOTSTRAP_CACHE = BOOTSTRAP_SOURCE;
158
+ }
159
+ }
160
+ }
161
+ return BOOTSTRAP_CACHE;
162
+ }
148
163
  var CSP_TAIL = `style-src 'unsafe-inline'; img-src 'self' data: https://fastly.picsum.photos; font-src 'self' data:; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'; object-src 'none'`;
149
164
  var cspCache = null;
150
- var EXTERNAL_BOOTSTRAP = `
151
- import { start, prefetch, postForm, prefetchStats } from '/client.js';
152
- ${BOOTSTRAP}`;
165
+ var EXTERNAL_BOOTSTRAP_CACHE = null;
166
+ function externalBootstrap() {
167
+ if (EXTERNAL_BOOTSTRAP_CACHE === null) EXTERNAL_BOOTSTRAP_CACHE = `
168
+ import { start, startNotFound, prefetch, postForm, prefetchStats, serializeState } from '/client.js';
169
+ ${bootstrap()}`;
170
+ return EXTERNAL_BOOTSTRAP_CACHE;
171
+ }
153
172
  function inlineScript(client) {
173
+ const m = /\bexport\s*\{([^}]*)\}\s*;?\s*$/.exec(client);
174
+ let body = client;
175
+ let aliases = "";
176
+ if (m) {
177
+ body = client.slice(0, m.index) + client.slice(m.index + m[0].length);
178
+ aliases = m[1].split(",").map((pair) => {
179
+ const parts = pair.split(" as ").map((x) => x.trim());
180
+ if (parts.length === 1 || parts[0] === parts[1]) return "";
181
+ return parts[0] && parts[1] ? `var ${parts[1]} = ${parts[0]};` : "";
182
+ }).join("");
183
+ }
154
184
  return `
155
- ${client}
156
- ${BOOTSTRAP}`;
185
+ ${body}
186
+ ${aliases}
187
+ ${bootstrap()}`;
157
188
  }
158
189
  function mintNonce() {
159
190
  const bytes = new Uint8Array(16);
@@ -165,7 +196,7 @@ function mintNonce() {
165
196
  function securityHeaders(client, nonce = "") {
166
197
  if (nonce) return { "Content-Security-Policy": `script-src 'nonce-${nonce}' 'strict-dynamic'; ${CSP_TAIL}` };
167
198
  if (!cspCache || cspCache.for !== client) {
168
- const scriptSrc = client ? `'sha256-${createHash("sha256").update(inlineScript(client), "utf-8").digest("base64")}'` : `'self' 'sha256-${createHash("sha256").update(EXTERNAL_BOOTSTRAP, "utf-8").digest("base64")}'`;
199
+ const scriptSrc = client ? `'sha256-${createHash("sha256").update(inlineScript(client), "utf-8").digest("base64")}'` : `'self' 'sha256-${createHash("sha256").update(externalBootstrap(), "utf-8").digest("base64")}'`;
169
200
  cspCache = {
170
201
  for: client,
171
202
  value: `script-src ${scriptSrc}; ${CSP_TAIL}`
@@ -230,7 +261,7 @@ function mergeHeadBlocks(blocks) {
230
261
  function clientScriptTag(client, nonce = "") {
231
262
  const n = nonce ? ` nonce="${nonce}"` : "";
232
263
  return client ? `<script type="module"${n}>${inlineScript(client)}</script>` : `<script type="module"${n} src="/client.js"></script>
233
- <script type="module"${n}>${EXTERNAL_BOOTSTRAP}</script>`;
264
+ <script type="module"${n}>${externalBootstrap()}</script>`;
234
265
  }
235
266
  function shellOpen(styles = getStyles(), lang = "en", dir = "", shell = true) {
236
267
  const styleTag = styles ? `
@@ -322,6 +353,7 @@ async function createEdgeHandler(outDir, preloaded) {
322
353
  client = null;
323
354
  }
324
355
  }
356
+ if (client && client.includes("chunks/")) client = null;
325
357
  if (preloaded && "styles" in preloaded) {
326
358
  styles = preloaded.styles ?? "";
327
359
  } else {
@@ -371,6 +403,7 @@ async function createEdgeHandler(outDir, preloaded) {
371
403
  return hookCtx ? mod.runResponseHooks(hookCtx, apiRes) : apiRes;
372
404
  }
373
405
  const form = request.method === "POST" ? await request.formData() : void 0;
406
+ if (form) mod.invalidateRenderCache?.();
374
407
  if (!form && !broken.has(pathname) && !mod.isBuffered?.(pathname) && mod.canStream(pathname) && !mod.isNoJs(pathname)) {
375
408
  const nonce2 = mod.cspNonce?.(pathname) ? mintNonce() : "";
376
409
  const streamed = new Response(null, {
@@ -393,7 +426,9 @@ async function createEdgeHandler(outDir, preloaded) {
393
426
  });
394
427
  return new Response(stream, { status: head.status, headers: head.headers });
395
428
  }
396
- const page = await mod.renderPage(pathname, form);
429
+ const cookieHeader = request.headers.get("cookie") || "";
430
+ const memoHit = !form && mod.isCachedRoute?.(pathname) ? mod.peekRenderCache?.(pathname, cookieHeader) : null;
431
+ const page = memoHit ?? (mod.renderPageCached && !form ? await mod.renderPageCached(pathname, cookieHeader) : await mod.renderPage(pathname, form));
397
432
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : "";
398
433
  const html = buildShell(page.html, page.state, client, page.head, styles, page.csr !== false, page.lang, page.dir, nonce, page.shell !== false);
399
434
  const buffered = new Response(html, {