@opennextjs/cloudflare 1.5.1 → 1.5.2
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/api/durable-objects/queue.js +9 -1
- package/dist/api/overrides/asset-resolver/index.js +1 -0
- package/dist/api/overrides/incremental-cache/static-assets-incremental-cache.js +3 -1
- package/dist/api/overrides/internal.js +11 -1
- package/dist/api/overrides/queue/memory-queue.js +9 -1
- package/dist/cli/build/patches/plugins/instrumentation.d.ts +1 -0
- package/dist/cli/build/patches/plugins/instrumentation.js +33 -0
- package/dist/cli/build/patches/plugins/load-manifest.js +6 -1
- package/dist/cli/templates/images.js +7 -5
- package/dist/cli/utils/run-wrangler.js +4 -0
- package/package.json +2 -2
|
@@ -77,11 +77,12 @@ export class DOQueueHandler extends DurableObject {
|
|
|
77
77
|
this.ctx.waitUntil(revalidationPromise);
|
|
78
78
|
}
|
|
79
79
|
async executeRevalidation(msg) {
|
|
80
|
+
let response;
|
|
80
81
|
try {
|
|
81
82
|
debug(`Revalidating ${msg.MessageBody.host}${msg.MessageBody.url}`);
|
|
82
83
|
const { MessageBody: { host, url }, } = msg;
|
|
83
84
|
const protocol = host.includes("localhost") ? "http" : "https";
|
|
84
|
-
|
|
85
|
+
response = await this.service.fetch(`${protocol}://${host}${url}`, {
|
|
85
86
|
method: "HEAD",
|
|
86
87
|
headers: {
|
|
87
88
|
// This is defined during build
|
|
@@ -134,6 +135,13 @@ export class DOQueueHandler extends DurableObject {
|
|
|
134
135
|
}
|
|
135
136
|
finally {
|
|
136
137
|
this.ongoingRevalidations.delete(msg.MessageDeduplicationId);
|
|
138
|
+
// Cancel the stream when it has not been consumed
|
|
139
|
+
try {
|
|
140
|
+
await response?.body?.cancel();
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// Ignore errors when the stream was actually consumed
|
|
144
|
+
}
|
|
137
145
|
}
|
|
138
146
|
}
|
|
139
147
|
async alarm() {
|
|
@@ -19,8 +19,10 @@ class StaticAssetsIncrementalCache {
|
|
|
19
19
|
debugCache(`Get ${key}`);
|
|
20
20
|
try {
|
|
21
21
|
const response = await assets.fetch(this.getAssetUrl(key, cacheType));
|
|
22
|
-
if (!response.ok)
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
await response.body?.cancel();
|
|
23
24
|
return null;
|
|
25
|
+
}
|
|
24
26
|
return {
|
|
25
27
|
value: await response.json(),
|
|
26
28
|
lastModified: globalThis.__BUILD_TIMESTAMP_MS__,
|
|
@@ -34,8 +34,9 @@ export async function internalPurgeCacheByTags(env, tags) {
|
|
|
34
34
|
debugCache("purgeCacheByTags", "No cache zone ID or API token provided. Skipping cache purge.");
|
|
35
35
|
return "missing-credentials";
|
|
36
36
|
}
|
|
37
|
+
let response;
|
|
37
38
|
try {
|
|
38
|
-
|
|
39
|
+
response = await fetch(`https://api.cloudflare.com/client/v4/zones/${env.CACHE_PURGE_ZONE_ID}/purge_cache`, {
|
|
39
40
|
headers: {
|
|
40
41
|
Authorization: `Bearer ${env.CACHE_PURGE_API_TOKEN}`,
|
|
41
42
|
"Content-Type": "application/json",
|
|
@@ -62,4 +63,13 @@ export async function internalPurgeCacheByTags(env, tags) {
|
|
|
62
63
|
console.error("Error purging cache by tags:", error);
|
|
63
64
|
return "purge-failed";
|
|
64
65
|
}
|
|
66
|
+
finally {
|
|
67
|
+
// Cancel the stream when it has not been consumed
|
|
68
|
+
try {
|
|
69
|
+
await response?.body?.cancel();
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// Ignore errors when the stream was actually consumed
|
|
73
|
+
}
|
|
74
|
+
}
|
|
65
75
|
}
|
|
@@ -24,12 +24,13 @@ export class MemoryQueue {
|
|
|
24
24
|
if (this.revalidatedPaths.has(MessageDeduplicationId))
|
|
25
25
|
return;
|
|
26
26
|
this.revalidatedPaths.add(MessageDeduplicationId);
|
|
27
|
+
let response;
|
|
27
28
|
try {
|
|
28
29
|
const protocol = host.includes("localhost") ? "http" : "https";
|
|
29
30
|
// TODO: Drop the import - https://github.com/opennextjs/opennextjs-cloudflare/issues/361
|
|
30
31
|
// @ts-ignore
|
|
31
32
|
const manifest = await import("./.next/prerender-manifest.json");
|
|
32
|
-
|
|
33
|
+
response = await service.fetch(`${protocol}://${host}${url}`, {
|
|
33
34
|
method: "HEAD",
|
|
34
35
|
headers: {
|
|
35
36
|
"x-prerender-revalidate": manifest.preview.previewModeId,
|
|
@@ -49,6 +50,13 @@ export class MemoryQueue {
|
|
|
49
50
|
}
|
|
50
51
|
finally {
|
|
51
52
|
this.revalidatedPaths.delete(MessageDeduplicationId);
|
|
53
|
+
// Cancel the stream when it has not been consumed
|
|
54
|
+
try {
|
|
55
|
+
await response?.body?.cancel();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Ignore errors when the stream was actually consumed
|
|
59
|
+
}
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
62
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
|
|
2
2
|
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
|
|
3
3
|
export declare function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOptions): Plugin;
|
|
4
|
+
export declare function getNext154Rule(builtInstrumentationPath: string | null): string;
|
|
4
5
|
export declare function getNext15Rule(builtInstrumentationPath: string | null): string;
|
|
5
6
|
export declare function getNext14Rule(builtInstrumentationPath: string | null): string;
|
|
@@ -2,9 +2,21 @@ import { existsSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
4
4
|
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
|
|
5
|
+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
|
|
5
6
|
import { normalizePath } from "../../utils/normalize-path.js";
|
|
6
7
|
export function patchInstrumentation(updater, buildOpts) {
|
|
7
8
|
const builtInstrumentationPath = getBuiltInstrumentationPath(buildOpts);
|
|
9
|
+
updater.updateContent("patch-instrumentation-next15-4", [
|
|
10
|
+
{
|
|
11
|
+
field: {
|
|
12
|
+
filter: getCrossPlatformPathRegex(String.raw `/server/lib/router-utils/instrumentation-globals.external\.js$`, {
|
|
13
|
+
escape: false,
|
|
14
|
+
}),
|
|
15
|
+
contentFilter: /getInstrumentationModule\(/,
|
|
16
|
+
callback: ({ contents }) => patchCode(contents, getNext154Rule(builtInstrumentationPath)),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
]);
|
|
8
20
|
updater.updateContent("patch-instrumentation-next15", [
|
|
9
21
|
{
|
|
10
22
|
field: {
|
|
@@ -28,6 +40,27 @@ export function patchInstrumentation(updater, buildOpts) {
|
|
|
28
40
|
setup() { },
|
|
29
41
|
};
|
|
30
42
|
}
|
|
43
|
+
export function getNext154Rule(builtInstrumentationPath) {
|
|
44
|
+
return `
|
|
45
|
+
rule:
|
|
46
|
+
kind: expression_statement
|
|
47
|
+
has:
|
|
48
|
+
kind: assignment_expression
|
|
49
|
+
all:
|
|
50
|
+
- has: { pattern: "cachedInstrumentationModule" }
|
|
51
|
+
- has: { kind: call_expression, regex: "INSTRUMENTATION_HOOK_FILENAME"}
|
|
52
|
+
inside:
|
|
53
|
+
kind: try_statement
|
|
54
|
+
stopBy: end
|
|
55
|
+
has: { regex: "return cachedInstrumentationModule" }
|
|
56
|
+
inside:
|
|
57
|
+
kind: function_declaration
|
|
58
|
+
stopBy: end
|
|
59
|
+
has: { field: name, pattern: getInstrumentationModule }
|
|
60
|
+
fix: |-
|
|
61
|
+
cachedInstrumentationModule = ${builtInstrumentationPath ? `require('${builtInstrumentationPath}')` : "null"};
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
31
64
|
export function getNext15Rule(builtInstrumentationPath) {
|
|
32
65
|
return `
|
|
33
66
|
rule:
|
|
@@ -31,7 +31,9 @@ async function getLoadManifestRule(buildOpts) {
|
|
|
31
31
|
const { outputDir } = buildOpts;
|
|
32
32
|
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
|
|
33
33
|
const dotNextDir = join(baseDir, ".next");
|
|
34
|
-
const manifests = await glob(join(dotNextDir, "
|
|
34
|
+
const manifests = await glob(join(dotNextDir, "**/{*-manifest,required-server-files}.json"), {
|
|
35
|
+
windowsPathsNoEscape: true,
|
|
36
|
+
});
|
|
35
37
|
const returnManifests = (await Promise.all(manifests.map(async (manifest) => `
|
|
36
38
|
if ($PATH.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
|
|
37
39
|
return ${await readFile(manifest, "utf-8")};
|
|
@@ -46,6 +48,9 @@ function loadManifest($PATH, $$$ARGS) {
|
|
|
46
48
|
fix: `
|
|
47
49
|
function loadManifest($PATH, $$$ARGS) {
|
|
48
50
|
$PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
|
|
51
|
+
if ($PATH === "/.next/BUILD_ID") {
|
|
52
|
+
return process.env.NEXT_BUILD_ID;
|
|
53
|
+
}
|
|
49
54
|
${returnManifests}
|
|
50
55
|
throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`);
|
|
51
56
|
}`,
|
|
@@ -53,10 +53,10 @@ export async function fetchImage(fetcher, imageUrl, ctx) {
|
|
|
53
53
|
const buffer = new ArrayBuffer(32);
|
|
54
54
|
try {
|
|
55
55
|
let contentType;
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
const [
|
|
59
|
-
const reader =
|
|
56
|
+
// respBody is eventually used for the response
|
|
57
|
+
// contentBody is used to detect the content type
|
|
58
|
+
const [respBody, contentBody] = imgResponse.body.tee();
|
|
59
|
+
const reader = contentBody.getReader({ mode: "byob" });
|
|
60
60
|
const { value } = await reader.read(new Uint8Array(buffer));
|
|
61
61
|
// Release resources by calling `reader.cancel()`
|
|
62
62
|
// `ctx.waitUntil` keeps the runtime running until the promise settles without having to wait here.
|
|
@@ -77,8 +77,10 @@ export async function fetchImage(fetcher, imageUrl, ctx) {
|
|
|
77
77
|
headers.set("content-type", contentType);
|
|
78
78
|
headers.set("content-disposition", __IMAGES_CONTENT_DISPOSITION__);
|
|
79
79
|
headers.set("content-security-policy", __IMAGES_CONTENT_SECURITY_POLICY__);
|
|
80
|
-
return new Response(
|
|
80
|
+
return new Response(respBody, { ...imgResponse, headers });
|
|
81
81
|
}
|
|
82
|
+
// Cancel the unused stream
|
|
83
|
+
ctx.waitUntil(respBody.cancel());
|
|
82
84
|
return new Response('"url" parameter is valid but image type is not allowed', {
|
|
83
85
|
status: 400,
|
|
84
86
|
});
|
|
@@ -52,6 +52,10 @@ export function runWrangler(options, args, wranglerOpts = {}) {
|
|
|
52
52
|
env: {
|
|
53
53
|
...process.env,
|
|
54
54
|
...(wranglerOpts.logging === "error" ? { WRANGLER_LOG: "error" } : undefined),
|
|
55
|
+
// `.env` files are handled by the adapter.
|
|
56
|
+
// Wrangler would load `.env.<wrangler env>` while we should load `.env.<process.env.NEXTJS_ENV>`
|
|
57
|
+
// See https://opennext.js.org/cloudflare/howtos/env-vars
|
|
58
|
+
CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false",
|
|
55
59
|
},
|
|
56
60
|
});
|
|
57
61
|
if (result.status !== 0) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opennextjs/cloudflare",
|
|
3
3
|
"description": "Cloudflare builder for next apps",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"opennextjs-cloudflare": "dist/cli/index.js"
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"vitest": "^2.1.1"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
|
-
"wrangler": "^4.
|
|
75
|
+
"wrangler": "^4.24.4"
|
|
76
76
|
},
|
|
77
77
|
"scripts": {
|
|
78
78
|
"clean": "rimraf dist",
|