@wave-rf/cloudflare-md-router 0.0.0-dev.h55da3453dfaa → 0.2.0

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/README.md CHANGED
@@ -22,8 +22,6 @@ Link: </foo/bar.md>; rel="alternate"; type="text/markdown"
22
22
 
23
23
  This is on by default (only for a `200 text/html` reply to an extension-less GET); disable it with `advertiseTwin: false`.
24
24
 
25
- Because the worker negotiates on the `Accept` header, two clients can get different representations of the same URL. If you put a shared cache (a CDN, Cloudflare's own cache) in front of it, set `vary: true` to add a `Vary: Accept` header to the negotiated responses so the cache keys on `Accept` and doesn't serve the HTML page to a client that asked for markdown — or vice-versa. It's off by default (verbatim pass-through stays byte-for-byte; `Vary: User-Agent` is intentionally not added, as it would defeat shared caching).
26
-
27
25
  The included bot list covers the common ones: GPTBot, ChatGPT-User, OAI-SearchBot, ClaudeBot, Claude-Web, anthropic-ai, PerplexityBot, CCBot, Applebot-Extended, Google-Extended, cohere-ai, Bytespider, Diffbot. See `src/bots.ts`.
28
26
 
29
27
  ## Install
@@ -82,10 +80,6 @@ export default createMdRouter({
82
80
 
83
81
  // Don't advertise the `.md` twin via a `Link` header (default: true).
84
82
  advertiseTwin: false,
85
-
86
- // Add `Vary: Accept` to negotiated responses so a shared cache doesn't
87
- // cross-serve the HTML and markdown representations (default: false).
88
- vary: true,
89
83
  });
90
84
  ```
91
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wave-rf/cloudflare-md-router",
3
- "version": "0.0.0-dev.h55da3453dfaa",
3
+ "version": "0.2.0",
4
4
  "description": "Cloudflare Worker that serves a `.md` twin of any static page when the request is from a known LLM crawler or explicitly asks for `text/markdown`. Falls back to the HTML response otherwise.",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -62,6 +62,6 @@
62
62
  "devDependencies": {
63
63
  "@biomejs/biome": "^2.4.15",
64
64
  "@cloudflare/workers-types": "^4.20240208.0",
65
- "typescript": "^6.0.3"
65
+ "typescript": "^5.6.0"
66
66
  }
67
67
  }
package/src/worker.ts CHANGED
@@ -21,17 +21,6 @@ export interface MdRouterOptions {
21
21
  * `text/html` reply to an extension-less GET); the `.md` and pass-through
22
22
  * responses are untouched. Default: `true`. */
23
23
  advertiseTwin?: boolean;
24
- /** Append `Vary: Accept` to the responses this worker negotiates on the
25
- * `Accept` header (the `.md` twin, the HTML fallback when the twin is
26
- * missing, and the normal HTML page), so a shared cache keyed only on the
27
- * URL doesn't serve the HTML representation to a client that asked for
28
- * markdown — or vice-versa (RFC 9110 §12.5.5). Merges with any existing
29
- * `Vary` rather than replacing it; verbatim pass-through responses (non-GET,
30
- * extension-bearing paths) are left untouched. Only `Accept` is listed:
31
- * `User-Agent` is also a routing input, but `Vary: User-Agent` defeats
32
- * shared caching (huge UA cardinality), so it is intentionally omitted.
33
- * Default: `false`. */
34
- vary?: boolean;
35
24
  }
36
25
 
37
26
  const defaultMdPathFor = (pathname: string): string => {
@@ -47,10 +36,7 @@ const defaultMdPathFor = (pathname: string): string => {
47
36
  * Pass through unchanged for non-GET, requests with a file extension,
48
37
  * and "normal" browser requests — though the normal HTML page response also
49
38
  * gets a `Link` header advertising its `.md` twin unless `advertiseTwin` is
50
- * disabled (see {@link MdRouterOptions.advertiseTwin}). Opt into a
51
- * `Vary: Accept` header on the `Accept`-negotiated responses with
52
- * {@link MdRouterOptions.vary} so shared caches don't cross-serve the two
53
- * representations.
39
+ * disabled (see {@link MdRouterOptions.advertiseTwin}).
54
40
  *
55
41
  * Requires an `ASSETS` Fetcher binding in your `wrangler.jsonc`:
56
42
  * ```jsonc
@@ -73,19 +59,6 @@ export function createMdRouter<Env extends MdRouterEnv = MdRouterEnv>(
73
59
  const mdPathFor = options.mdPathFor ?? defaultMdPathFor;
74
60
  const acceptTokens = ["text/markdown", ...(options.acceptMarkdown ?? [])];
75
61
  const advertiseTwin = options.advertiseTwin ?? true;
76
- const vary = options.vary ?? false;
77
-
78
- // Append `Vary: Accept` to a negotiated response when `vary` is enabled.
79
- // Wraps via `new Response(body, response)` because an asset response's
80
- // headers are immutable; `append` (not `set`) preserves any existing `Vary`.
81
- const withVary = (response: Response): Response => {
82
- if (!vary) {
83
- return response;
84
- }
85
- const varied = new Response(response.body, response);
86
- varied.headers.append("Vary", "Accept");
87
- return varied;
88
- };
89
62
 
90
63
  return {
91
64
  async fetch(request, env): Promise<Response> {
@@ -107,7 +80,7 @@ export function createMdRouter<Env extends MdRouterEnv = MdRouterEnv>(
107
80
  .trim();
108
81
  const isHtml = contentType === "text/html";
109
82
  if (!advertiseTwin || response.status !== 200 || !isHtml) {
110
- return withVary(response);
83
+ return response;
111
84
  }
112
85
  // `new Response(body, response)` is the only way to add a header to an
113
86
  // otherwise-immutable asset response. A relative URI-Reference target is
@@ -118,19 +91,13 @@ export function createMdRouter<Env extends MdRouterEnv = MdRouterEnv>(
118
91
  .map((segment) => encodeURIComponent(segment))
119
92
  .join("/");
120
93
  withTwin.headers.append("Link", `<${encodedPath}>; rel="alternate"; type="text/markdown"`);
121
- // Already a mutable wrapper — append `Vary` here rather than re-wrapping.
122
- if (vary) {
123
- withTwin.headers.append("Vary", "Accept");
124
- }
125
94
  return withTwin;
126
95
  }
127
96
 
128
97
  const mdPath = mdPathFor(url.pathname);
129
98
  const mdResponse = await env.ASSETS.fetch(new Request(new URL(mdPath, url.origin), request));
130
99
 
131
- return mdResponse.status === 404
132
- ? withVary(await env.ASSETS.fetch(request))
133
- : withVary(mdResponse);
100
+ return mdResponse.status === 404 ? env.ASSETS.fetch(request) : mdResponse;
134
101
  },
135
102
  };
136
103
  }