@harlan-zw/nuxt-cloudflare 0.5.1 → 0.5.3

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/module.d.mts CHANGED
@@ -50,6 +50,7 @@ interface NitroCloudflareShape {
50
50
  }
51
51
  declare function findPopulatedRuntimeSecretPaths(config: unknown, environment: Readonly<Record<string, string | undefined>>): string[];
52
52
  interface NitroCloudflareContext {
53
+ nitroVersion?: number;
53
54
  /** The project directory Wrangler and Nitro read the authored config from. */
54
55
  rootDir?: string;
55
56
  /** Whether Nuxt emits server source maps. */
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": "^4.6.0 || ^5.0.0"
6
6
  },
7
- "version": "0.5.1",
7
+ "version": "0.5.3",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import process from 'node:process';
2
- import { useLogger, defineNuxtModule, createResolver, resolveServerVariant, hasNuxtModule, addTypeTemplate } from '@nuxt/kit';
2
+ import { useLogger, defineNuxtModule, createResolver, resolveServerVariant, hasNuxtModule, addTypeTemplate, getNitroVersion } from '@nuxt/kit';
3
3
  import { resolve } from 'pathe';
4
4
  import { a as diagnoseWranglerSourceConfigs, d as discoverWranglerSourceConfigs, b as diagnoseStaticAssetRules, c as readStaticAssetRuleFiles, f as resolveBuildStaticAssetDirectory, e as evaluateWranglerDiagnostics } from './shared/nuxt-cloudflare.Drgx8RDh.mjs';
5
5
  import { resolveHtmlCacheGuarantee, staleDirectivesAreDisabled } from '../dist/runtime/server/utils/workers-cache.js';
@@ -110,6 +110,10 @@ const runtimeConfigPluginPath = resolve(
110
110
  import.meta.dirname,
111
111
  import.meta.url.endsWith(".ts") ? "runtime/server/plugins/runtime-config.ts" : "runtime/server/plugins/runtime-config.js"
112
112
  );
113
+ const requestLifetimePluginPath = resolve(
114
+ import.meta.dirname,
115
+ import.meta.url.endsWith(".ts") ? "runtime/server/plugins/request-lifetime.ts" : "runtime/server/plugins/request-lifetime.js"
116
+ );
113
117
  const WIDE_EVENT_FIELDS = [
114
118
  "cf.colo",
115
119
  "cf.country",
@@ -154,10 +158,21 @@ function addNitroPlugin(nitro, path) {
154
158
  if (!nitro.plugins.includes(path))
155
159
  nitro.plugins.push(path);
156
160
  }
161
+ function leaveAuthoredCompatibilityFlags(config, authored) {
162
+ const authoredFlags = new Set(authored.compatibility_flags);
163
+ if (config.compatibility_flags)
164
+ config.compatibility_flags = config.compatibility_flags.filter((flag) => !authoredFlags.has(flag));
165
+ for (const [name, environment] of Object.entries(config.env ?? {})) {
166
+ const authoredEnvironment = authored.env?.[name];
167
+ if (authoredEnvironment)
168
+ leaveAuthoredCompatibilityFlags(environment, authoredEnvironment);
169
+ }
170
+ }
157
171
  function configureNitroCloudflare(nitro, options, context = {}) {
158
172
  const authored = readAuthoredWranglerConfig(context.rootDir);
159
173
  if (authored._tag === "invalid")
160
174
  throw new Error(`[nuxt-cloudflare] Wrangler config "${authored.path}" cannot be parsed: ${authored.reason}`);
175
+ const authoredConfig = authored._tag === "authored" ? authored.config : {};
161
176
  nitro.preset ??= "cloudflare-module";
162
177
  nitro.cloudflare ??= {};
163
178
  nitro.cloudflare.deployConfig = true;
@@ -171,8 +186,11 @@ function configureNitroCloudflare(nitro, options, context = {}) {
171
186
  uploadSourceMaps: options.sourceMaps ?? nitro.sourceMap ?? context.serverSourceMaps ?? false,
172
187
  versionMetadataBinding: options.versionMetadataBinding,
173
188
  workersCache: resolveModuleWorkersCachePolicy(options)
174
- }, authored._tag === "authored" ? authored.config : {});
189
+ }, authoredConfig);
190
+ if (context.nitroVersion === 3)
191
+ leaveAuthoredCompatibilityFlags(nitro.cloudflare.wrangler, authoredConfig);
175
192
  addNitroPlugin(nitro, runtimeConfigPluginPath);
193
+ addNitroPlugin(nitro, requestLifetimePluginPath);
176
194
  if (nitro.cloudflare.wrangler.cache?.enabled)
177
195
  addNitroPlugin(nitro, workersCachePluginPath);
178
196
  if (options.kvCache === false)
@@ -268,6 +286,7 @@ function setupCloudflareModule(options, nuxt) {
268
286
  nuxt.options.nitro,
269
287
  options,
270
288
  {
289
+ nitroVersion: getNitroVersion(nuxt),
271
290
  rootDir: nuxt.options.rootDir,
272
291
  serverSourceMaps: Boolean(nuxt.options.sourcemap.server)
273
292
  }
@@ -363,6 +382,12 @@ A chunk-retention guarantee does not cover these. A prerendered route is served
363
382
  nuxt.hook("nitro:init", (nitro) => {
364
383
  if (!nitro.options.preset?.includes("cloudflare"))
365
384
  return;
385
+ nitro.hooks.hook("prerender:init", (prerenderer) => {
386
+ const { storage, devStorage } = prerenderer.options;
387
+ const mounts = devStorage?.cache ? devStorage : storage;
388
+ if (mounts?.cache?.driver === cacheDriverPath)
389
+ mounts.cache = { driver: "memory" };
390
+ });
366
391
  nitro.hooks.hook("compiled", () => auditGeneratedWranglerConfig(
367
392
  nitro,
368
393
  options,
@@ -1,4 +1,5 @@
1
1
  declare module '#nuxt-cloudflare/nitro' {
2
2
  export const defineNitroPlugin: typeof import('./nitro2').defineNitroPlugin
3
+ export const defineNitroAppPlugin: typeof import('./nitro2').defineNitroAppPlugin
3
4
  export const useRuntimeConfig: typeof import('./nitro2').useRuntimeConfig
4
5
  }
@@ -1,3 +1,5 @@
1
1
  import type { NitroAppPlugin } from 'nitropack/types';
2
2
  export { useRuntimeConfig } from 'nitropack/runtime';
3
+ /** Registers a plugin that receives the Nitro app itself, not the hook-adapted copy. */
4
+ export declare const defineNitroAppPlugin: (setup: NitroAppPlugin) => NitroAppPlugin;
3
5
  export declare function defineNitroPlugin(setup: NitroAppPlugin): NitroAppPlugin;
@@ -1,5 +1,6 @@
1
1
  import { defineNitroPlugin as definePlugin } from "nitropack/runtime";
2
2
  export { useRuntimeConfig } from "nitropack/runtime";
3
+ export const defineNitroAppPlugin = definePlugin;
3
4
  export function defineNitroPlugin(setup) {
4
5
  return definePlugin((app) => {
5
6
  const hooks = app.hooks;
@@ -1,3 +1,5 @@
1
1
  import type { NitroAppPlugin } from 'nitro/types';
2
2
  export { useRuntimeConfig } from 'nuxt/server';
3
+ /** Registers a plugin that receives the Nitro app itself, not the hook-adapted copy. */
4
+ export declare const defineNitroAppPlugin: (setup: NitroAppPlugin) => NitroAppPlugin;
3
5
  export declare function defineNitroPlugin(setup: NitroAppPlugin): NitroAppPlugin;
@@ -1,5 +1,6 @@
1
1
  import { definePlugin } from "nitro";
2
2
  export { useRuntimeConfig } from "nuxt/server";
3
+ export const defineNitroAppPlugin = definePlugin;
3
4
  export function defineNitroPlugin(setup) {
4
5
  return definePlugin((app) => {
5
6
  const hooks = app.hooks;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Keeps each Worker request alive until its response settles, so a visitor who
3
+ * leaves cannot strand a promise that later requests share.
4
+ *
5
+ * The Cloudflare entry calls the app's `fetch` on Nitro 3 and `localFetch` on Nitro 2.
6
+ */
7
+ declare const _default: any;
8
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { defineNitroAppPlugin } from "#nuxt-cloudflare/nitro";
2
+ import { keepNativeRequestAlive, keepRequestAlive } from "../utils/request-lifetime.js";
3
+ export default defineNitroAppPlugin((nitroApp) => {
4
+ const app = nitroApp;
5
+ if (typeof app.fetch === "function")
6
+ app.fetch = keepNativeRequestAlive(app.fetch.bind(app));
7
+ else if (typeof app.localFetch === "function")
8
+ app.localFetch = keepRequestAlive(app.localFetch);
9
+ });
@@ -0,0 +1,30 @@
1
+ /** The Worker context Nitro's Cloudflare entry passes to `localFetch`. */
2
+ export interface LocalFetchInit {
3
+ context?: {
4
+ waitUntil?: (promise: Promise<unknown>) => void;
5
+ };
6
+ }
7
+ /** Nitro 2's in-process fetch, `nitroApp.localFetch`. */
8
+ export type LocalFetch = (input: string, init?: LocalFetchInit) => Promise<Response>;
9
+ interface WorkerRequest extends Request {
10
+ waitUntil?: (promise: Promise<unknown>) => void;
11
+ }
12
+ /** Nitro 3's in-process fetch. The Cloudflare entry attaches `waitUntil` to the request. */
13
+ export type NativeFetch = (request: WorkerRequest) => Response | Promise<Response>;
14
+ /**
15
+ * Wraps Nitro's `localFetch` so each Worker request stays alive until its own
16
+ * response settles.
17
+ *
18
+ * Nitro's cached functions and Nuxt Content share one in-flight promise per key
19
+ * across every request in an isolate. On Workers a promise can only settle in
20
+ * the request that created it. When a visitor leaves, Cloudflare cancels that
21
+ * request, the shared promise can never settle, and every later request for the
22
+ * key fails. Registering the response with `waitUntil` keeps the creating
23
+ * request alive for up to 30 seconds after its client disconnects.
24
+ *
25
+ * A fetch without a Worker context, such as an internal `$fetch` outside a
26
+ * request, passes through unchanged.
27
+ */
28
+ export declare function keepRequestAlive(localFetch: LocalFetch): LocalFetch;
29
+ export declare function keepNativeRequestAlive(fetch: NativeFetch): NativeFetch;
30
+ export {};
@@ -0,0 +1,14 @@
1
+ export function keepRequestAlive(localFetch) {
2
+ return (input, init) => {
3
+ const response = Promise.resolve(localFetch(input, init));
4
+ init?.context?.waitUntil?.(response.then(() => void 0, () => void 0));
5
+ return response;
6
+ };
7
+ }
8
+ export function keepNativeRequestAlive(fetch) {
9
+ return (request) => {
10
+ const response = fetch(request);
11
+ request.waitUntil?.(Promise.resolve(response).then(() => void 0, () => void 0));
12
+ return response;
13
+ };
14
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@harlan-zw/nuxt-cloudflare",
3
3
  "type": "module",
4
- "version": "0.5.1",
4
+ "version": "0.5.3",
5
5
  "description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -116,25 +116,25 @@
116
116
  }
117
117
  },
118
118
  "dependencies": {
119
- "@nuxt/kit": "^4.6.0",
119
+ "@nuxt/kit": "^4.6.4",
120
120
  "citty": "^0.2.2",
121
121
  "confbox": "^0.2.4",
122
122
  "consola": "^3.4.2",
123
123
  "h3": "2.0.1-rc.25",
124
- "nitropack": "^2.13.4",
124
+ "nitropack": "^2.13.6",
125
125
  "pathe": "^2.0.3",
126
- "unstorage": "^1.17.5"
126
+ "unstorage": "^1.17.6"
127
127
  },
128
128
  "devDependencies": {
129
129
  "@antfu/eslint-config": "^9.3.0",
130
130
  "@arethetypeswrong/cli": "^0.18.5",
131
131
  "@cloudflare/workers-types": "^5.20260811.1",
132
132
  "@nuxt/module-builder": "^1.0.2",
133
- "@nuxt/schema": "^4.6.0",
134
- "@types/node": "^26.1.1",
135
- "eslint": "^10.8.1",
133
+ "@nuxt/schema": "^4.6.4",
134
+ "@types/node": "^26.6.6",
135
+ "eslint": "^10.15.0",
136
136
  "nitro": "3.0.260903-beta",
137
- "nuxt": "^4.6.0",
137
+ "nuxt": "^4.6.4",
138
138
  "typescript": "^5.9.3",
139
139
  "unbuild": "^3.6.1",
140
140
  "vitest": "^4.1.10",