@harlan-zw/nuxt-cloudflare 0.0.4 → 0.0.6
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 +13 -3
- package/dist/cli/index.mjs +1 -1
- package/dist/module.d.mts +1 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +108 -2
- package/dist/runtime/server/plugins/workers-cache.d.ts +2 -0
- package/dist/runtime/server/plugins/workers-cache.js +14 -0
- package/dist/runtime/server/utils/workers-cache.d.ts +2 -0
- package/dist/runtime/server/utils/workers-cache.js +20 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -12,7 +12,8 @@ The module extracts production patterns already proven in Nuxt SEO and gscdump.
|
|
|
12
12
|
- Preview URLs disabled unless explicitly enabled
|
|
13
13
|
- `workers_dev` disabled when a route proves the Worker remains reachable; workers without routes must choose explicitly
|
|
14
14
|
- Version metadata binding at `CF_VERSION_METADATA`
|
|
15
|
-
- Workers Caching
|
|
15
|
+
- Workers Caching enabled by default with per-version isolation
|
|
16
|
+
- Dynamic responses default to `private, no-store`; rendered HTML is forcibly private and conflicting route rules are diagnosed
|
|
16
17
|
- Smart Placement enabled by default; explicit region, host, or hostname placement is preserved
|
|
17
18
|
- Source-map upload when the Nitro build emits maps; explicit `false` is preserved
|
|
18
19
|
- Module-wide `secrets.required` names copied to each environment; source root secrets remain scoped to the root
|
|
@@ -31,7 +32,6 @@ export default defineNuxtConfig({
|
|
|
31
32
|
|
|
32
33
|
nuxtCloudflare: {
|
|
33
34
|
requiredSecrets: ['NUXT_SESSION_PASSWORD'],
|
|
34
|
-
workersCache: { _tag: 'enabled', crossVersion: false },
|
|
35
35
|
},
|
|
36
36
|
|
|
37
37
|
nitro: {
|
|
@@ -58,7 +58,17 @@ export default defineNuxtConfig({
|
|
|
58
58
|
|
|
59
59
|
Cloudflare KV requires TTLs of at least 60 seconds.
|
|
60
60
|
|
|
61
|
-
Workers Caching is separate from Nitro's KV-backed cache.
|
|
61
|
+
Workers Caching is separate from Nitro's KV-backed cache. It is enabled by default with `cross_version_cache: false`, so each deployment starts with an isolated cache. Dynamic Nitro responses default to `Cache-Control: private, no-store` and `Cloudflare-CDN-Cache-Control: no-store` unless they declare an explicit cache policy. Rendered HTML always receives those private directives; this is required because Cloudflare otherwise heuristically caches a `200` response with no cache header for two hours.
|
|
62
|
+
|
|
63
|
+
Potential HTML route rules warn when they use `cache`, `isr`, `swr`, or publicly cacheable `Cache-Control`, `CDN-Cache-Control`, and `Cloudflare-CDN-Cache-Control` headers. Rules proven to be HTML through prerendering, a `.html` path, or an explicit `text/html` content type fail the build. Explicit `private` and `no-store` policies are safe. API, Nuxt OG Image, and static asset routes remain available for explicit caching. Disable Workers Caching only for a gateway that must execute on every request:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
export default defineNuxtConfig({
|
|
67
|
+
nuxtCloudflare: {
|
|
68
|
+
workersCache: { _tag: 'disabled' },
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
```
|
|
62
72
|
|
|
63
73
|
Smart Placement moves fetch handlers only when Cloudflare measures a faster location near upstream services. Assets-first delivery remains close to the user. Queue handlers, RPC methods, and named entrypoints are unaffected. An authored region, host, or hostname placement overrides the smart default.
|
|
64
74
|
|
package/dist/cli/index.mjs
CHANGED
|
@@ -187,7 +187,7 @@ const doctor = defineCommand({
|
|
|
187
187
|
const main = defineCommand({
|
|
188
188
|
meta: {
|
|
189
189
|
name: "nuxt-cloudflare",
|
|
190
|
-
version: "0.0.
|
|
190
|
+
version: "0.0.6",
|
|
191
191
|
description: "Cloudflare deployment defaults and diagnostics for Nuxt"
|
|
192
192
|
},
|
|
193
193
|
subCommands: { doctor }
|
package/dist/module.d.mts
CHANGED
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -5,8 +5,93 @@ import { readWranglerJsonFile, diagnoseWranglerConfig, formatWranglerDiagnostics
|
|
|
5
5
|
import 'node:fs';
|
|
6
6
|
import 'node:fs/promises';
|
|
7
7
|
|
|
8
|
+
const NON_HTML_ROUTE_PREFIXES = [
|
|
9
|
+
"/api",
|
|
10
|
+
"/_ipx",
|
|
11
|
+
"/_nuxt",
|
|
12
|
+
"/_og",
|
|
13
|
+
"/assets",
|
|
14
|
+
"/fonts",
|
|
15
|
+
"/images"
|
|
16
|
+
];
|
|
17
|
+
const NON_HTML_EXTENSION_RE = /\.(?:avif|css|csv|gif|ico|jpe?g|js|json|map|md|mjs|pdf|png|svg|txt|webmanifest|webp|woff2?|xml)(?:$|[?*])/i;
|
|
18
|
+
const CACHE_HEADER_NAMES = /* @__PURE__ */ new Set([
|
|
19
|
+
"cache-control",
|
|
20
|
+
"cdn-cache-control",
|
|
21
|
+
"cloudflare-cdn-cache-control"
|
|
22
|
+
]);
|
|
23
|
+
const CACHE_ROUTE_RULE_NAMES = ["cache", "isr", "swr"];
|
|
24
|
+
const PRIVATE_CACHE_DIRECTIVE_RE = /(?:^|,)\s*(?:no-store|private)\s*(?:,|$)/i;
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
function isHtmlCapableRoute(route) {
|
|
29
|
+
if (NON_HTML_EXTENSION_RE.test(route))
|
|
30
|
+
return false;
|
|
31
|
+
return !NON_HTML_ROUTE_PREFIXES.some((prefix) => route === prefix || route.startsWith(`${prefix}/`));
|
|
32
|
+
}
|
|
33
|
+
function isPrivateCachePolicy(value) {
|
|
34
|
+
return typeof value === "string" && PRIVATE_CACHE_DIRECTIVE_RE.test(value);
|
|
35
|
+
}
|
|
36
|
+
function hasHtmlContentType(headers) {
|
|
37
|
+
if (!headers)
|
|
38
|
+
return false;
|
|
39
|
+
const contentType = Object.entries(headers).find(([name]) => name.toLowerCase() === "content-type")?.[1];
|
|
40
|
+
return typeof contentType === "string" && /^text\/html(?:;|$)/i.test(contentType);
|
|
41
|
+
}
|
|
42
|
+
function resolveViolationSeverity(route, value) {
|
|
43
|
+
const definitelyHtml = value.prerender === true || /\.html(?:$|[?*])/i.test(route) || hasHtmlContentType(isRecord(value.headers) ? value.headers : void 0);
|
|
44
|
+
return definitelyHtml ? "error" : "warning";
|
|
45
|
+
}
|
|
46
|
+
function findHtmlCacheRouteRuleViolations(routeRules) {
|
|
47
|
+
if (!routeRules)
|
|
48
|
+
return [];
|
|
49
|
+
return Object.entries(routeRules).flatMap(([route, value]) => {
|
|
50
|
+
if (!isHtmlCapableRoute(route) || !isRecord(value))
|
|
51
|
+
return [];
|
|
52
|
+
const severity = resolveViolationSeverity(route, value);
|
|
53
|
+
const violations = [];
|
|
54
|
+
for (const name of CACHE_ROUTE_RULE_NAMES) {
|
|
55
|
+
if (value[name] !== void 0 && value[name] !== false) {
|
|
56
|
+
violations.push({
|
|
57
|
+
_tag: "html-cache-route-rule",
|
|
58
|
+
severity,
|
|
59
|
+
route,
|
|
60
|
+
configPath: `routeRules.${route}.${name}`
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!isRecord(value.headers))
|
|
65
|
+
return violations;
|
|
66
|
+
for (const name of Object.keys(value.headers)) {
|
|
67
|
+
if (CACHE_HEADER_NAMES.has(name.toLowerCase()) && !isPrivateCachePolicy(value.headers[name])) {
|
|
68
|
+
violations.push({
|
|
69
|
+
_tag: "html-cache-header",
|
|
70
|
+
severity,
|
|
71
|
+
route,
|
|
72
|
+
configPath: `routeRules.${route}.headers.${name}`
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return violations;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function formatHtmlCacheRouteRuleViolations(violations) {
|
|
80
|
+
return violations.map((violation) => {
|
|
81
|
+
const subject = violation.severity === "error" ? "HTML route" : "Potential HTML route";
|
|
82
|
+
return `${violation.configPath}: ${subject} must remain private and no-store when Workers Cache is enabled.`;
|
|
83
|
+
}).join("\n");
|
|
84
|
+
}
|
|
85
|
+
|
|
8
86
|
const logger = useLogger("nuxt-cloudflare");
|
|
9
87
|
const cacheDriverPath = resolve(import.meta.dirname, import.meta.url.endsWith(".ts") ? "storage.ts" : "storage.mjs");
|
|
88
|
+
const workersCachePluginPath = resolve(
|
|
89
|
+
import.meta.dirname,
|
|
90
|
+
import.meta.url.endsWith(".ts") ? "runtime/server/plugins/workers-cache.ts" : "runtime/server/plugins/workers-cache.js"
|
|
91
|
+
);
|
|
92
|
+
function resolveModuleWorkersCachePolicy(options) {
|
|
93
|
+
return options.workersCache ?? { _tag: "enabled", crossVersion: false };
|
|
94
|
+
}
|
|
10
95
|
function configureNitroCloudflare(nitro, options, nuxtServerSourceMaps) {
|
|
11
96
|
nitro.preset ??= "cloudflare-module";
|
|
12
97
|
nitro.cloudflare ??= {};
|
|
@@ -19,8 +104,13 @@ function configureNitroCloudflare(nitro, options, nuxtServerSourceMaps) {
|
|
|
19
104
|
tracesSampleRate: options.tracesSampleRate,
|
|
20
105
|
uploadSourceMaps: options.sourceMaps ?? nitro.sourceMap ?? nuxtServerSourceMaps ?? false,
|
|
21
106
|
versionMetadataBinding: options.versionMetadataBinding,
|
|
22
|
-
workersCache: options
|
|
107
|
+
workersCache: resolveModuleWorkersCachePolicy(options)
|
|
23
108
|
});
|
|
109
|
+
if (nitro.cloudflare.wrangler.cache?.enabled) {
|
|
110
|
+
nitro.plugins ??= [];
|
|
111
|
+
if (!nitro.plugins.includes(workersCachePluginPath))
|
|
112
|
+
nitro.plugins.push(workersCachePluginPath);
|
|
113
|
+
}
|
|
24
114
|
if (options.kvCache === false)
|
|
25
115
|
return;
|
|
26
116
|
const cache = nitro.storage?.cache;
|
|
@@ -75,6 +165,21 @@ function setupCloudflareModule(options, nuxt) {
|
|
|
75
165
|
nuxt.hook("modules:done", () => {
|
|
76
166
|
configure();
|
|
77
167
|
});
|
|
168
|
+
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
169
|
+
if (resolveModuleWorkersCachePolicy(options)._tag === "disabled")
|
|
170
|
+
return;
|
|
171
|
+
const violations = findHtmlCacheRouteRuleViolations(nitroConfig.routeRules);
|
|
172
|
+
const warnings = violations.filter((violation) => violation.severity === "warning");
|
|
173
|
+
const errors = violations.filter((violation) => violation.severity === "error");
|
|
174
|
+
if (warnings.length > 0)
|
|
175
|
+
logger.warn(formatHtmlCacheRouteRuleViolations(warnings));
|
|
176
|
+
if (errors.length > 0) {
|
|
177
|
+
throw new Error(
|
|
178
|
+
`[nuxt-cloudflare] HTML route rules conflict with Workers Caching:
|
|
179
|
+
${formatHtmlCacheRouteRuleViolations(errors)}`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
78
183
|
if (!nuxt.options.dev) {
|
|
79
184
|
nuxt.hook("nitro:init", (nitro) => {
|
|
80
185
|
nitro.hooks.hook("compiled", () => auditGeneratedWranglerConfig(nitro, options, nuxt.options.rootDir));
|
|
@@ -93,7 +198,8 @@ const module$1 = defineNuxtModule({
|
|
|
93
198
|
doctor: { _tag: "advisory" },
|
|
94
199
|
logsSampleRate: 0.1,
|
|
95
200
|
tracesSampleRate: 0.01,
|
|
96
|
-
versionMetadataBinding: "CF_VERSION_METADATA"
|
|
201
|
+
versionMetadataBinding: "CF_VERSION_METADATA",
|
|
202
|
+
workersCache: { _tag: "enabled", crossVersion: false }
|
|
97
203
|
},
|
|
98
204
|
setup: setupCloudflareModule
|
|
99
205
|
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getResponseHeader, setResponseHeader } from "h3";
|
|
2
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
3
|
+
import { hasExplicitCachePolicy, withHtmlNoStoreHeaders } from "../utils/workers-cache.js";
|
|
4
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
5
|
+
nitroApp.hooks.hook("render:response", (response) => {
|
|
6
|
+
response.headers = withHtmlNoStoreHeaders(response.headers);
|
|
7
|
+
});
|
|
8
|
+
nitroApp.hooks.hook("beforeResponse", (event) => {
|
|
9
|
+
if (hasExplicitCachePolicy((name) => getResponseHeader(event, name)))
|
|
10
|
+
return;
|
|
11
|
+
setResponseHeader(event, "cache-control", "private, no-store");
|
|
12
|
+
setResponseHeader(event, "cloudflare-cdn-cache-control", "no-store");
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const CACHE_POLICY_HEADER_NAMES = [
|
|
2
|
+
"cache-control",
|
|
3
|
+
"cdn-cache-control",
|
|
4
|
+
"cloudflare-cdn-cache-control",
|
|
5
|
+
"expires"
|
|
6
|
+
];
|
|
7
|
+
const HTML_CACHE_HEADER_NAMES = new Set(CACHE_POLICY_HEADER_NAMES.slice(0, 3));
|
|
8
|
+
export function hasExplicitCachePolicy(getHeader) {
|
|
9
|
+
return CACHE_POLICY_HEADER_NAMES.some((name) => Boolean(getHeader(name)));
|
|
10
|
+
}
|
|
11
|
+
export function withHtmlNoStoreHeaders(headers = {}) {
|
|
12
|
+
const preserved = Object.fromEntries(
|
|
13
|
+
Object.entries(headers).filter(([name]) => !HTML_CACHE_HEADER_NAMES.has(name.toLowerCase()))
|
|
14
|
+
);
|
|
15
|
+
return {
|
|
16
|
+
...preserved,
|
|
17
|
+
"cache-control": "private, no-store",
|
|
18
|
+
"cloudflare-cdn-cache-control": "no-store"
|
|
19
|
+
};
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@harlan-zw/nuxt-cloudflare",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -87,6 +87,8 @@
|
|
|
87
87
|
"@nuxt/kit": "^4.5.2",
|
|
88
88
|
"citty": "^0.2.2",
|
|
89
89
|
"consola": "^3.4.2",
|
|
90
|
+
"h3": "2.0.1-rc.25",
|
|
91
|
+
"nitropack": "^2.13.4",
|
|
90
92
|
"pathe": "^2.0.3",
|
|
91
93
|
"unstorage": "^1.17.5"
|
|
92
94
|
},
|