@opennextjs/cloudflare 1.7.0 → 1.7.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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { error } from "@opennextjs/aws/adapters/logger.js";
|
|
1
2
|
import { getCloudflareContext } from "../../cloudflare-context.js";
|
|
2
3
|
import { debugCache, internalPurgeCacheByTags } from "../internal.js";
|
|
3
4
|
export const purgeCache = ({ type = "direct" }) => {
|
|
@@ -13,7 +14,7 @@ export const purgeCache = ({ type = "direct" }) => {
|
|
|
13
14
|
else {
|
|
14
15
|
const durableObject = env.NEXT_CACHE_DO_PURGE;
|
|
15
16
|
if (!durableObject) {
|
|
16
|
-
|
|
17
|
+
error("Purge cache: NEXT_CACHE_DO_PURGE not found. Skipping cache purge.");
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
20
|
const id = durableObject.idFromName("cache-purge");
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import { error } from "@opennextjs/aws/adapters/logger.js";
|
|
2
3
|
import { getCloudflareContext } from "../cloudflare-context.js";
|
|
3
4
|
export const debugCache = (name, ...args) => {
|
|
4
5
|
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
|
|
@@ -31,7 +32,7 @@ export async function purgeCacheByTags(tags) {
|
|
|
31
32
|
export async function internalPurgeCacheByTags(env, tags) {
|
|
32
33
|
if (!env.CACHE_PURGE_ZONE_ID && !env.CACHE_PURGE_API_TOKEN) {
|
|
33
34
|
// THIS IS A NO-OP
|
|
34
|
-
|
|
35
|
+
error("No cache zone ID or API token provided. Skipping cache purge.");
|
|
35
36
|
return "missing-credentials";
|
|
36
37
|
}
|
|
37
38
|
let response;
|
|
@@ -48,12 +49,12 @@ export async function internalPurgeCacheByTags(env, tags) {
|
|
|
48
49
|
});
|
|
49
50
|
if (response.status === 429) {
|
|
50
51
|
// Rate limit exceeded
|
|
51
|
-
|
|
52
|
+
error("purgeCacheByTags: Rate limit exceeded. Skipping cache purge.");
|
|
52
53
|
return "rate-limit-exceeded";
|
|
53
54
|
}
|
|
54
55
|
const bodyResponse = (await response.json());
|
|
55
56
|
if (!bodyResponse.success) {
|
|
56
|
-
|
|
57
|
+
error("purgeCacheByTags: Cache purge failed. Errors:", bodyResponse.errors.map((error) => `${error.code}: ${error.message}`));
|
|
57
58
|
return "purge-failed";
|
|
58
59
|
}
|
|
59
60
|
debugCache("purgeCacheByTags", "Cache purged successfully for tags:", tags);
|
|
@@ -22,3 +22,23 @@ export declare const buildIdRule = "\nrule:\n pattern:\n selector: method_de
|
|
|
22
22
|
*/
|
|
23
23
|
export declare function createCacheHandlerRule(handlerPath: string): string;
|
|
24
24
|
export declare function createComposableCacheHandlersRule(handlerPath: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* `attachRequestMeta` sets `initUrl` to always be with `https` cause this.fetchHostname && this.port is undefined in our case.
|
|
27
|
+
* this.nextConfig.experimental.trustHostHeader is also true.
|
|
28
|
+
*
|
|
29
|
+
* This patch checks if the original protocol was "http:" and rewrites the `initUrl` to reflect the actual host protocol.
|
|
30
|
+
* It will make `request.url` in route handlers end up with the correct protocol.
|
|
31
|
+
*
|
|
32
|
+
* Note: We cannot use the already defined `initURL` we passed in as requestMetaData to NextServer's request handler as pages router
|
|
33
|
+
* data routes would fail. It would miss the `_next/data` part in the path in that case.
|
|
34
|
+
*
|
|
35
|
+
* Therefor we just replace the protocol if necessary in the value from this template string:
|
|
36
|
+
* https://github.com/vercel/next.js/blob/ea08bf27/packages/next/src/server/next-server.ts#L1920
|
|
37
|
+
*
|
|
38
|
+
* Affected lines:
|
|
39
|
+
* https://github.com/vercel/next.js/blob/ea08bf27/packages/next/src/server/next-server.ts#L1916-L1923
|
|
40
|
+
*
|
|
41
|
+
* Callstack: handleRequest-> handleRequestImpl -> attachRequestMeta
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
export declare const attachRequestMetaRule = "\nrule:\n kind: identifier\n regex: ^initUrl$\n inside:\n kind: arguments\n all:\n - has: {kind: identifier, regex: ^req$}\n - has: {kind: string, regex: initURL}\n inside:\n kind: call_expression\n all:\n - has: {kind: parenthesized_expression, regex: '0'}\n - has: { regex: _requestmeta.addRequestMeta}\n inside:\n kind: expression_statement\n inside:\n kind: statement_block\n inside:\n kind: method_definition\n has:\n kind: property_identifier\n regex: ^attachRequestMeta$\nfix:\n 'req[Symbol.for(\"NextInternalRequestMeta\")]?.initProtocol === \"http:\" && initUrl.startsWith(\"https://\") ? `http://${initUrl.slice(8)}`: initUrl'";
|
|
@@ -28,6 +28,7 @@ export function patchNextServer(updater, buildOpts) {
|
|
|
28
28
|
contents = patchCode(contents, createComposableCacheHandlersRule(composableCacheHandler));
|
|
29
29
|
// Node middleware are not supported on Cloudflare yet
|
|
30
30
|
contents = patchCode(contents, disableNodeMiddlewareRule);
|
|
31
|
+
contents = patchCode(contents, attachRequestMetaRule);
|
|
31
32
|
return contents;
|
|
32
33
|
},
|
|
33
34
|
},
|
|
@@ -100,3 +101,47 @@ fix: |-
|
|
|
100
101
|
globalThis[handlersSetSymbol] = new Set(globalThis[handlersMapSymbol].values());
|
|
101
102
|
`;
|
|
102
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* `attachRequestMeta` sets `initUrl` to always be with `https` cause this.fetchHostname && this.port is undefined in our case.
|
|
106
|
+
* this.nextConfig.experimental.trustHostHeader is also true.
|
|
107
|
+
*
|
|
108
|
+
* This patch checks if the original protocol was "http:" and rewrites the `initUrl` to reflect the actual host protocol.
|
|
109
|
+
* It will make `request.url` in route handlers end up with the correct protocol.
|
|
110
|
+
*
|
|
111
|
+
* Note: We cannot use the already defined `initURL` we passed in as requestMetaData to NextServer's request handler as pages router
|
|
112
|
+
* data routes would fail. It would miss the `_next/data` part in the path in that case.
|
|
113
|
+
*
|
|
114
|
+
* Therefor we just replace the protocol if necessary in the value from this template string:
|
|
115
|
+
* https://github.com/vercel/next.js/blob/ea08bf27/packages/next/src/server/next-server.ts#L1920
|
|
116
|
+
*
|
|
117
|
+
* Affected lines:
|
|
118
|
+
* https://github.com/vercel/next.js/blob/ea08bf27/packages/next/src/server/next-server.ts#L1916-L1923
|
|
119
|
+
*
|
|
120
|
+
* Callstack: handleRequest-> handleRequestImpl -> attachRequestMeta
|
|
121
|
+
*
|
|
122
|
+
*/
|
|
123
|
+
export const attachRequestMetaRule = `
|
|
124
|
+
rule:
|
|
125
|
+
kind: identifier
|
|
126
|
+
regex: ^initUrl$
|
|
127
|
+
inside:
|
|
128
|
+
kind: arguments
|
|
129
|
+
all:
|
|
130
|
+
- has: {kind: identifier, regex: ^req$}
|
|
131
|
+
- has: {kind: string, regex: initURL}
|
|
132
|
+
inside:
|
|
133
|
+
kind: call_expression
|
|
134
|
+
all:
|
|
135
|
+
- has: {kind: parenthesized_expression, regex: '0'}
|
|
136
|
+
- has: { regex: _requestmeta.addRequestMeta}
|
|
137
|
+
inside:
|
|
138
|
+
kind: expression_statement
|
|
139
|
+
inside:
|
|
140
|
+
kind: statement_block
|
|
141
|
+
inside:
|
|
142
|
+
kind: method_definition
|
|
143
|
+
has:
|
|
144
|
+
kind: property_identifier
|
|
145
|
+
regex: ^attachRequestMeta$
|
|
146
|
+
fix:
|
|
147
|
+
'req[Symbol.for("NextInternalRequestMeta")]?.initProtocol === "http:" && initUrl.startsWith("https://") ? \`http://\${initUrl.slice(8)}\`: initUrl'`;
|