mdorigin 0.8.0 → 0.8.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.
@@ -80,6 +80,18 @@ export interface CreateCloudflareWorkerOptions {
80
80
  /** Maximum isolate-memoized search results. Defaults to 100. */
81
81
  searchCacheLimit?: number;
82
82
  }
83
+ /**
84
+ * Browser cache policy for external binary assets, regardless of whether
85
+ * they are R2-backed or staged in the static assets bundle. Public paths
86
+ * stay stable while the underlying object can change on a redeploy, so
87
+ * responses must stay revalidatable (no immutable): browsers reuse images
88
+ * for a day, then revalidate in the background while stale content keeps
89
+ * rendering. Without this header browsers fall back to Cloudflare's default
90
+ * max-age=0 must-revalidate and pay one conditional round trip per image on
91
+ * every visit. The build step mirrors this value into the generated assets
92
+ * `_headers` file, so keep the two in sync.
93
+ */
94
+ export declare const EXTERNAL_BINARY_BROWSER_CACHE_CONTROL = "public, max-age=86400, stale-while-revalidate=604800";
83
95
  export declare function createCloudflareWorker(manifest: CloudflareManifest, options?: CreateCloudflareWorkerOptions): ExportedHandlerLike;
84
96
  /**
85
97
  * Memoizes deterministic search results per isolate. The search index is
@@ -8,15 +8,17 @@ const CDN_CACHE_CONTROL = 'max-age=86400';
8
8
  const L2_CACHE_CONTROL = 'public, max-age=86400';
9
9
  const L2_CACHE_KEY_ORIGIN = 'https://cache.mdorigin.internal';
10
10
  /**
11
- * Browser cache policy for R2-backed binary assets in external binary mode.
12
- * Public paths stay stable while the underlying object can change on a
13
- * redeploy, so responses must stay revalidatable (no immutable): browsers
14
- * reuse images for a day, then revalidate in the background while stale
15
- * content keeps rendering. Without this header browsers fall back to
16
- * Cloudflare's default max-age=0 must-revalidate and pay one conditional
17
- * round trip per image on every visit.
11
+ * Browser cache policy for external binary assets, regardless of whether
12
+ * they are R2-backed or staged in the static assets bundle. Public paths
13
+ * stay stable while the underlying object can change on a redeploy, so
14
+ * responses must stay revalidatable (no immutable): browsers reuse images
15
+ * for a day, then revalidate in the background while stale content keeps
16
+ * rendering. Without this header browsers fall back to Cloudflare's default
17
+ * max-age=0 must-revalidate and pay one conditional round trip per image on
18
+ * every visit. The build step mirrors this value into the generated assets
19
+ * `_headers` file, so keep the two in sync.
18
20
  */
19
- const EXTERNAL_BINARY_BROWSER_CACHE_CONTROL = 'public, max-age=86400, stale-while-revalidate=604800';
21
+ export const EXTERNAL_BINARY_BROWSER_CACHE_CONTROL = 'public, max-age=86400, stale-while-revalidate=604800';
20
22
  /**
21
23
  * Edge cache policy for R2-backed binary assets. Mirrors the HTML CDN policy
22
24
  * so edge hits are deterministic while a redeploy can serve stale bytes for
@@ -472,7 +474,8 @@ async function tryServeExternalBinary(manifest, request, env, resolved) {
472
474
  }
473
475
  const assetUrl = new URL(request.url);
474
476
  assetUrl.pathname = `/${manifestEntry.storageKey}`;
475
- return assetsBinding.fetch(new Request(assetUrl.toString(), request));
477
+ const assetResponse = await assetsBinding.fetch(new Request(assetUrl.toString(), request));
478
+ return withExternalBinaryBrowserCacheControl(assetResponse);
476
479
  }
477
480
  const bucket = env?.[manifest.runtime?.r2Binding ?? 'MDORIGIN_R2'];
478
481
  if (!bucket) {
@@ -526,6 +529,28 @@ async function tryServeExternalBinary(manifest, request, env, resolved) {
526
529
  headers,
527
530
  });
528
531
  }
532
+ /**
533
+ * Applies the external-binary browser cache policy to a Workers Assets
534
+ * binding response. Requests that match a static asset are normally served
535
+ * by the assets layer without invoking the Worker, so the build step also
536
+ * emits an assets `_headers` rule with this policy; this override keeps
537
+ * Worker-served fallbacks (for example sites running `run_worker_first:
538
+ * true`) consistent with that rule. Only the browser directive is
539
+ * overridden: the assets layer owns its edge cache (`cf-cache-status`) and
540
+ * already resolved this response, so no CDN directive is added here. Error
541
+ * responses pass through untouched.
542
+ */
543
+ function withExternalBinaryBrowserCacheControl(response) {
544
+ if (response.status !== 200 && response.status !== 304) {
545
+ return response;
546
+ }
547
+ const headers = new Headers(response.headers);
548
+ headers.set('cache-control', EXTERNAL_BINARY_BROWSER_CACHE_CONTROL);
549
+ return new Response(response.body, {
550
+ status: response.status,
551
+ headers,
552
+ });
553
+ }
529
554
  function isExternalBinaryEntry(entry) {
530
555
  return entry.kind === 'binary' && 'storageKind' in entry;
531
556
  }
@@ -3,7 +3,7 @@ import { createHash } from 'node:crypto';
3
3
  import { createReadStream } from 'node:fs';
4
4
  import { copyFile, mkdir, readFile, readdir, realpath, rm, stat, writeFile, } from 'node:fs/promises';
5
5
  import path from 'node:path';
6
- import { createCloudflareWorker } from './adapters/cloudflare.js';
6
+ import { createCloudflareWorker, EXTERNAL_BINARY_BROWSER_CACHE_CONTROL, } from './adapters/cloudflare.js';
7
7
  import { getMediaTypeForPath, isIgnoredContentName, isLikelyTextPath, normalizeContentPath, } from './core/content-store.js';
8
8
  export { createCloudflareWorker };
9
9
  const DEFAULT_ASSETS_MAX_BYTES = 25 * 1024 * 1024;
@@ -273,6 +273,7 @@ async function writeExternalStaging(rootDir, searchDir, outDir, manifest, option
273
273
  const stagedObjects = new Map();
274
274
  let hasAssets = false;
275
275
  let hasR2 = false;
276
+ let hasAssetsBinaries = false;
276
277
  if (options.binaryMode === 'external') {
277
278
  for (const entry of manifest.entries) {
278
279
  if (entry.kind !== 'binary' || !('storageKind' in entry)) {
@@ -284,6 +285,7 @@ async function writeExternalStaging(rootDir, searchDir, outDir, manifest, option
284
285
  await mkdir(path.dirname(targetFile), { recursive: true });
285
286
  await copyFile(sourceFile, targetFile);
286
287
  hasAssets = true;
288
+ hasAssetsBinaries = true;
287
289
  stagedObjects.set(`assets:${entry.storageKey}`, {
288
290
  kind: 'binary',
289
291
  path: entry.path,
@@ -350,6 +352,17 @@ async function writeExternalStaging(rootDir, searchDir, outDir, manifest, option
350
352
  }
351
353
  }
352
354
  }
355
+ if (hasAssetsBinaries) {
356
+ // Binaries staged in the static assets bundle are served by the assets
357
+ // layer without invoking the Worker when `run_worker_first` is false
358
+ // (the generated wrangler config), so the browser cache policy has to
359
+ // ride along in the assets `_headers` file. A single `/*` rule stays
360
+ // well within Cloudflare's 100-rule limit and the assets directory only
361
+ // holds mdorigin-staged binaries plus internal search bundle files. The
362
+ // Worker adapter applies the same policy to binding-served fallbacks, so
363
+ // keep both in sync.
364
+ await writeFile(path.join(assetsDir, '_headers'), `/*\n\tCache-Control: ${EXTERNAL_BINARY_BROWSER_CACHE_CONTROL}\n`, 'utf8');
365
+ }
353
366
  return {
354
367
  version: 2,
355
368
  workerEntry: 'worker.mjs',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdorigin",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "type": "module",
5
5
  "description": "Markdown-first publishing for humans and agents.",
6
6
  "repository": {