@opennextjs/cloudflare 0.5.12 → 0.6.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.
Files changed (110) hide show
  1. package/README.md +8 -8
  2. package/dist/api/cloudflare-context.d.ts +16 -5
  3. package/dist/api/config.d.ts +16 -43
  4. package/dist/api/config.js +21 -19
  5. package/dist/api/durable-objects/queue.d.ts +32 -0
  6. package/dist/api/durable-objects/queue.js +234 -0
  7. package/dist/api/durable-objects/queue.spec.js +290 -0
  8. package/dist/api/durable-objects/sharded-tag-cache.d.ts +7 -0
  9. package/dist/api/durable-objects/sharded-tag-cache.js +22 -0
  10. package/dist/api/durable-objects/sharded-tag-cache.spec.js +37 -0
  11. package/dist/api/overrides/incremental-cache/internal.d.ts +5 -0
  12. package/dist/api/{kv-cache.d.ts → overrides/incremental-cache/kv-incremental-cache.d.ts} +1 -1
  13. package/dist/api/{kv-cache.js → overrides/incremental-cache/kv-incremental-cache.js} +5 -5
  14. package/dist/api/overrides/incremental-cache/r2-incremental-cache.d.ts +17 -0
  15. package/dist/api/overrides/incremental-cache/r2-incremental-cache.js +61 -0
  16. package/dist/api/overrides/incremental-cache/regional-cache.d.ts +51 -0
  17. package/dist/api/overrides/incremental-cache/regional-cache.js +111 -0
  18. package/dist/api/overrides/queue/do-queue.d.ts +6 -0
  19. package/dist/api/overrides/queue/do-queue.js +15 -0
  20. package/dist/api/{memory-queue.d.ts → overrides/queue/memory-queue.d.ts} +3 -3
  21. package/dist/api/{memory-queue.js → overrides/queue/memory-queue.js} +18 -14
  22. package/dist/api/overrides/queue/memory-queue.spec.d.ts +1 -0
  23. package/dist/api/{memory-queue.spec.js → overrides/queue/memory-queue.spec.js} +20 -14
  24. package/dist/api/overrides/tag-cache/d1-next-tag-cache.d.ts +13 -0
  25. package/dist/api/overrides/tag-cache/d1-next-tag-cache.js +61 -0
  26. package/dist/api/{d1-tag-cache.d.ts → overrides/tag-cache/d1-tag-cache.d.ts} +3 -5
  27. package/dist/api/{d1-tag-cache.js → overrides/tag-cache/d1-tag-cache.js} +22 -29
  28. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.d.ts +122 -0
  29. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.js +247 -0
  30. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.spec.d.ts +1 -0
  31. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.spec.js +322 -0
  32. package/dist/cli/args.d.ts +13 -2
  33. package/dist/cli/args.js +44 -29
  34. package/dist/cli/build/build.d.ts +5 -1
  35. package/dist/cli/build/build.js +9 -19
  36. package/dist/cli/build/bundle-server.js +5 -13
  37. package/dist/cli/build/open-next/compile-cache-assets-manifest.d.ts +1 -1
  38. package/dist/cli/build/open-next/compile-cache-assets-manifest.js +4 -6
  39. package/dist/cli/build/open-next/compileDurableObjects.d.ts +2 -0
  40. package/dist/cli/build/open-next/compileDurableObjects.js +30 -0
  41. package/dist/cli/build/open-next/copyCacheAssets.js +1 -1
  42. package/dist/cli/build/open-next/createServerBundle.d.ts +9 -1
  43. package/dist/cli/build/open-next/createServerBundle.js +28 -9
  44. package/dist/cli/build/patches/ast/patch-vercel-og-library.js +1 -1
  45. package/dist/cli/build/patches/ast/vercel-og.d.ts +5 -5
  46. package/dist/cli/build/patches/ast/vercel-og.js +1 -1
  47. package/dist/cli/build/patches/ast/vercel-og.spec.js +1 -1
  48. package/dist/cli/build/patches/ast/webpack-runtime.js +1 -1
  49. package/dist/cli/build/patches/ast/webpack-runtime.spec.js +1 -1
  50. package/dist/cli/build/patches/plugins/build-id.d.ts +2 -2
  51. package/dist/cli/build/patches/plugins/build-id.js +12 -5
  52. package/dist/cli/build/patches/plugins/build-id.spec.js +1 -1
  53. package/dist/cli/build/patches/plugins/dynamic-requires.d.ts +1 -2
  54. package/dist/cli/build/patches/plugins/dynamic-requires.js +21 -11
  55. package/dist/cli/build/patches/plugins/eval-manifest.d.ts +2 -2
  56. package/dist/cli/build/patches/plugins/eval-manifest.js +12 -5
  57. package/dist/cli/build/patches/plugins/find-dir.d.ts +2 -2
  58. package/dist/cli/build/patches/plugins/find-dir.js +10 -5
  59. package/dist/cli/build/patches/plugins/instrumentation.d.ts +2 -5
  60. package/dist/cli/build/patches/plugins/instrumentation.js +19 -3
  61. package/dist/cli/build/patches/plugins/instrumentation.spec.js +1 -1
  62. package/dist/cli/build/patches/plugins/load-manifest.d.ts +2 -2
  63. package/dist/cli/build/patches/plugins/load-manifest.js +12 -5
  64. package/dist/cli/build/patches/plugins/next-minimal.d.ts +4 -7
  65. package/dist/cli/build/patches/plugins/next-minimal.js +31 -15
  66. package/dist/cli/build/patches/plugins/next-minimal.spec.js +1 -1
  67. package/dist/cli/build/patches/plugins/patch-depd-deprecations.d.ts +2 -2
  68. package/dist/cli/build/patches/plugins/patch-depd-deprecations.js +10 -2
  69. package/dist/cli/build/patches/plugins/patch-depd-deprecations.spec.js +1 -1
  70. package/dist/cli/build/patches/plugins/require.d.ts +2 -2
  71. package/dist/cli/build/patches/plugins/require.js +43 -35
  72. package/dist/cli/build/patches/plugins/res-revalidate.d.ts +3 -0
  73. package/dist/cli/build/patches/plugins/res-revalidate.js +77 -0
  74. package/dist/cli/build/patches/plugins/res-revalidate.spec.d.ts +1 -0
  75. package/dist/cli/build/patches/plugins/res-revalidate.spec.js +141 -0
  76. package/dist/cli/build/utils/create-config-files.d.ts +2 -2
  77. package/dist/cli/build/utils/create-config-files.js +3 -3
  78. package/dist/cli/build/utils/ensure-cf-config.js +3 -13
  79. package/dist/cli/commands/deploy.d.ts +5 -0
  80. package/dist/cli/commands/deploy.js +9 -0
  81. package/dist/cli/commands/populate-cache.d.ts +7 -0
  82. package/dist/cli/commands/populate-cache.js +78 -0
  83. package/dist/cli/commands/preview.d.ts +5 -0
  84. package/dist/cli/commands/preview.js +9 -0
  85. package/dist/cli/index.js +36 -9
  86. package/dist/cli/project-options.d.ts +5 -1
  87. package/dist/cli/templates/worker.d.ts +3 -4
  88. package/dist/cli/templates/worker.js +30 -18
  89. package/dist/cli/utils/run-wrangler.d.ts +18 -0
  90. package/dist/cli/utils/run-wrangler.js +41 -0
  91. package/package.json +7 -9
  92. package/templates/open-next.config.ts +1 -1
  93. package/templates/wrangler.jsonc +2 -2
  94. package/dist/api/kvCache.d.ts +0 -5
  95. package/dist/api/kvCache.js +0 -5
  96. package/dist/cli/build/patches/ast/util.d.ts +0 -50
  97. package/dist/cli/build/patches/ast/util.js +0 -65
  98. package/dist/cli/build/patches/ast/util.spec.js +0 -43
  99. package/dist/cli/build/patches/plugins/content-updater.d.ts +0 -44
  100. package/dist/cli/build/patches/plugins/content-updater.js +0 -55
  101. package/dist/cli/build/patches/plugins/fetch-cache-wait-until.d.ts +0 -14
  102. package/dist/cli/build/patches/plugins/fetch-cache-wait-until.js +0 -40
  103. package/dist/cli/build/patches/plugins/fetch-cache-wait-until.spec.js +0 -453
  104. package/dist/cli/templates/shims/node-fs.d.ts +0 -17
  105. package/dist/cli/templates/shims/node-fs.js +0 -51
  106. package/dist/cli/templates/shims/throw.d.ts +0 -0
  107. package/dist/cli/templates/shims/throw.js +0 -2
  108. /package/dist/api/{memory-queue.spec.d.ts → durable-objects/queue.spec.d.ts} +0 -0
  109. /package/dist/{cli/build/patches/ast/util.spec.d.ts → api/durable-objects/sharded-tag-cache.spec.d.ts} +0 -0
  110. /package/dist/{cli/build/patches/plugins/fetch-cache-wait-until.spec.d.ts → api/overrides/incremental-cache/internal.js} +0 -0
@@ -1,11 +1,27 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
4
- import { patchCode } from "../ast/util.js";
4
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
5
5
  export function patchInstrumentation(updater, buildOpts) {
6
6
  const builtInstrumentationPath = getBuiltInstrumentationPath(buildOpts);
7
- updater.updateContent("patch-instrumentation-next15", { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /async loadInstrumentationModule\(/ }, async ({ contents }) => patchCode(contents, getNext15Rule(builtInstrumentationPath)));
8
- updater.updateContent("patch-instrumentation-next14", { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /async prepareImpl\(/ }, async ({ contents }) => patchCode(contents, getNext14Rule(builtInstrumentationPath)));
7
+ updater.updateContent("patch-instrumentation-next15", [
8
+ {
9
+ field: {
10
+ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
11
+ contentFilter: /async loadInstrumentationModule\(/,
12
+ callback: ({ contents }) => patchCode(contents, getNext15Rule(builtInstrumentationPath)),
13
+ },
14
+ },
15
+ ]);
16
+ updater.updateContent("patch-instrumentation-next14", [
17
+ {
18
+ field: {
19
+ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
20
+ contentFilter: /async prepareImpl\(/,
21
+ callback: ({ contents }) => patchCode(contents, getNext14Rule(builtInstrumentationPath)),
22
+ },
23
+ },
24
+ ]);
9
25
  return {
10
26
  name: "patch-instrumentation",
11
27
  setup() { },
@@ -1,5 +1,5 @@
1
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
1
2
  import { describe, expect, test } from "vitest";
2
- import { patchCode } from "../ast/util.js";
3
3
  import { getNext14Rule, getNext15Rule } from "./instrumentation.js";
4
4
  describe("LoadInstrumentationModule (Next15)", () => {
5
5
  const code = `
@@ -2,5 +2,5 @@
2
2
  * Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd.
3
3
  */
4
4
  import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
5
- import type { ContentUpdater } from "./content-updater.js";
6
- export declare function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOptions): import("esbuild").Plugin;
5
+ import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
6
+ export declare function inlineLoadManifest(updater: ContentUpdater, buildOpts: BuildOptions): Plugin;
@@ -4,15 +4,22 @@
4
4
  import { readFile } from "node:fs/promises";
5
5
  import { join, posix, relative, sep } from "node:path";
6
6
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
7
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
7
8
  import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
8
9
  import { glob } from "glob";
9
10
  import { normalizePath } from "../../utils/normalize-path.js";
10
- import { patchCode } from "../ast/util.js";
11
11
  export function inlineLoadManifest(updater, buildOpts) {
12
- return updater.updateContent("inline-load-manifest", {
13
- filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/load-manifest\.js$`, { escape: false }),
14
- contentFilter: /function loadManifest\(/,
15
- }, async ({ contents }) => patchCode(contents, await getRule(buildOpts)));
12
+ return updater.updateContent("inline-load-manifest", [
13
+ {
14
+ field: {
15
+ filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/load-manifest\.js$`, {
16
+ escape: false,
17
+ }),
18
+ contentFilter: /function loadManifest\(/,
19
+ callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)),
20
+ },
21
+ },
22
+ ]);
16
23
  }
17
24
  async function getRule(buildOpts) {
18
25
  const { outputDir } = buildOpts;
@@ -1,7 +1,4 @@
1
- import { ContentUpdater } from "./content-updater.js";
2
- export declare const abortControllerRule = "\nrule:\n all: \n - kind: lexical_declaration\n pattern: let $VAR = new AbortController\n - precedes:\n kind: function_declaration\n stopBy: end\n has:\n kind: statement_block\n has:\n kind: try_statement\n has:\n kind: catch_clause\n has:\n kind: statement_block\n has: \n kind: return_statement\n all: \n - has: \n stopBy: end\n kind: member_expression\n pattern: $VAR.signal.aborted\n - has:\n stopBy: end\n kind: call_expression\n regex: console.error\\(\"Failed to fetch RSC payload for\n \nfix:\n 'let $VAR = {signal:{aborted: false}};'\n";
3
- export declare const nextMinimalRule = "\nrule:\n kind: member_expression\n pattern: process.env.NEXT_MINIMAL\n any: \n - inside:\n kind: parenthesized_expression\n stopBy: end\n inside:\n kind: if_statement\n any:\n - inside:\n kind: statement_block\n inside:\n kind: method_definition\n any:\n - has: {kind: property_identifier, field: name, regex: runEdgeFunction}\n - has: {kind: property_identifier, field: name, regex: runMiddleware}\n - has: {kind: property_identifier, field: name, regex: imageOptimizer}\n - has:\n kind: statement_block\n has:\n kind: expression_statement\n pattern: res.statusCode = 400;\nfix:\n 'true' \n";
4
- export declare function patchNextMinimal(updater: ContentUpdater): {
5
- name: string;
6
- setup(): void;
7
- };
1
+ import { ContentUpdater, type Plugin } from "@opennextjs/aws/plugins/content-updater.js";
2
+ export declare const abortControllerRule = "\nrule:\n all:\n - kind: lexical_declaration\n pattern: let $VAR = new AbortController\n - precedes:\n kind: function_declaration\n stopBy: end\n has:\n kind: statement_block\n has:\n kind: try_statement\n has:\n kind: catch_clause\n has:\n kind: statement_block\n has:\n kind: return_statement\n all:\n - has:\n stopBy: end\n kind: member_expression\n pattern: $VAR.signal.aborted\n - has:\n stopBy: end\n kind: call_expression\n regex: console.error\\(\"Failed to fetch RSC payload for\n\nfix:\n 'let $VAR = {signal:{aborted: false}};'\n";
3
+ export declare const nextMinimalRule = "\nrule:\n kind: member_expression\n pattern: process.env.NEXT_MINIMAL\n any:\n - inside:\n kind: parenthesized_expression\n stopBy: end\n inside:\n kind: if_statement\n any:\n - inside:\n kind: statement_block\n inside:\n kind: method_definition\n any:\n - has: {kind: property_identifier, field: name, regex: runEdgeFunction}\n - has: {kind: property_identifier, field: name, regex: runMiddleware}\n - has: {kind: property_identifier, field: name, regex: imageOptimizer}\n - has:\n kind: statement_block\n has:\n kind: expression_statement\n pattern: res.statusCode = 400;\nfix:\n 'true'\n";
4
+ export declare function patchNextMinimal(updater: ContentUpdater): Plugin;
@@ -1,11 +1,11 @@
1
- import { patchCode } from "../ast/util.js";
1
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2
2
  // We try to be as specific as possible to avoid patching the wrong thing here
3
3
  // It seems that there is a bug in the worker runtime. When the AbortController is created outside of the request context it throws an error (not sure if it's expected or not) except in this case. https://github.com/cloudflare/workerd/issues/3657
4
4
  // It fails while requiring the `app-page.runtime.prod.js` file, but instead of throwing an error, it just return an empty object for the `require('app-page.runtime.prod.js')` call which makes every request to an app router page fail.
5
5
  // If it's a bug in workerd and it's not expected to throw an error, we can remove this patch.
6
6
  export const abortControllerRule = `
7
7
  rule:
8
- all:
8
+ all:
9
9
  - kind: lexical_declaration
10
10
  pattern: let $VAR = new AbortController
11
11
  - precedes:
@@ -19,10 +19,10 @@ rule:
19
19
  kind: catch_clause
20
20
  has:
21
21
  kind: statement_block
22
- has:
22
+ has:
23
23
  kind: return_statement
24
- all:
25
- - has:
24
+ all:
25
+ - has:
26
26
  stopBy: end
27
27
  kind: member_expression
28
28
  pattern: $VAR.signal.aborted
@@ -30,18 +30,18 @@ rule:
30
30
  stopBy: end
31
31
  kind: call_expression
32
32
  regex: console.error\\("Failed to fetch RSC payload for
33
-
33
+
34
34
  fix:
35
35
  'let $VAR = {signal:{aborted: false}};'
36
36
  `;
37
37
  // This rule is used instead of defining `process.env.NEXT_MINIMAL` in the `esbuild config.
38
38
  // Do we want to entirely replace these functions to reduce the bundle size?
39
- // In next `renderHTML` is used as a fallback in case of errors, but in minimal mode it just throws the error and the responsability of handling it is on the infra.
39
+ // In next `renderHTML` is used as a fallback in case of errors, but in minimal mode it just throws the error and the responsibility of handling it is on the infra.
40
40
  export const nextMinimalRule = `
41
41
  rule:
42
42
  kind: member_expression
43
43
  pattern: process.env.NEXT_MINIMAL
44
- any:
44
+ any:
45
45
  - inside:
46
46
  kind: parenthesized_expression
47
47
  stopBy: end
@@ -62,15 +62,31 @@ rule:
62
62
  kind: expression_statement
63
63
  pattern: res.statusCode = 400;
64
64
  fix:
65
- 'true'
65
+ 'true'
66
66
  `;
67
67
  export function patchNextMinimal(updater) {
68
- updater.updateContent("patch-abortController-next15.2", { filter: /app-page(-experimental)?\.runtime\.prod\.js$/, contentFilter: /new AbortController/ }, async ({ contents }) => {
69
- return patchCode(contents, abortControllerRule);
70
- });
71
- updater.updateContent("patch-next-minimal", { filter: /next-server\.(js)$/, contentFilter: /.*/ }, async ({ contents }) => {
72
- return patchCode(contents, nextMinimalRule);
73
- });
68
+ updater.updateContent("patch-abortController-next15.2", [
69
+ {
70
+ field: {
71
+ filter: /app-page(-experimental)?\.runtime\.prod\.js$/,
72
+ contentFilter: /new AbortController/,
73
+ callback: ({ contents }) => {
74
+ return patchCode(contents, abortControllerRule);
75
+ },
76
+ },
77
+ },
78
+ ]);
79
+ updater.updateContent("patch-next-minimal", [
80
+ {
81
+ field: {
82
+ filter: /next-server\.(js)$/,
83
+ contentFilter: /.*/,
84
+ callback: ({ contents }) => {
85
+ return patchCode(contents, nextMinimalRule);
86
+ },
87
+ },
88
+ },
89
+ ]);
74
90
  return {
75
91
  name: "patch-abortController",
76
92
  setup() { },
@@ -1,5 +1,5 @@
1
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
1
2
  import { describe, expect, test } from "vitest";
2
- import { patchCode } from "../ast/util";
3
3
  import { abortControllerRule } from "./next-minimal";
4
4
  const appPageRuntimeProdJs = `let p = new AbortController;
5
5
  async function h(e3, t3) {
@@ -1,9 +1,9 @@
1
- import type { ContentUpdater } from "./content-updater.js";
1
+ import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
2
2
  /**
3
3
  * Some dependencies of Next.js use depd to deprecate some of their functions, depd uses `eval` to generate
4
4
  * a deprecated version of such functions, this causes `eval` warnings in the terminal even if these functions
5
5
  * are never called, this function fixes that by patching the depd `wrapfunction` function so that it still
6
6
  * retains the same type of behavior but without using `eval`
7
7
  */
8
- export declare function patchDepdDeprecations(updater: ContentUpdater): import("esbuild").Plugin;
8
+ export declare function patchDepdDeprecations(updater: ContentUpdater): Plugin;
9
9
  export declare const rule = "\nrule:\n kind: function_declaration\n pattern: function wrapfunction($FN, $MESSAGE) { $$$ }\n all:\n - has:\n kind: variable_declarator\n stopBy: end\n has:\n field: name\n pattern: deprecatedfn\n - has:\n kind: call_expression\n stopBy: end\n has:\n kind: identifier\n pattern: eval\nfix:\n function wrapfunction($FN, $MESSAGE) {\n if(typeof $FN !== 'function') throw new Error(\"argument fn must be a function\");\n return function deprecated_$FN(...args) {\n console.warn($MESSAGE);\n return $FN(...args);\n }\n }\n";
@@ -1,4 +1,4 @@
1
- import { patchCode } from "../ast/util.js";
1
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2
2
  /**
3
3
  * Some dependencies of Next.js use depd to deprecate some of their functions, depd uses `eval` to generate
4
4
  * a deprecated version of such functions, this causes `eval` warnings in the terminal even if these functions
@@ -6,7 +6,15 @@ import { patchCode } from "../ast/util.js";
6
6
  * retains the same type of behavior but without using `eval`
7
7
  */
8
8
  export function patchDepdDeprecations(updater) {
9
- return updater.updateContent("patch-depd-deprecations", { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /argument fn must be a function/ }, ({ contents }) => patchCode(contents, rule));
9
+ return updater.updateContent("patch-depd-deprecations", [
10
+ {
11
+ field: {
12
+ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
13
+ contentFilter: /argument fn must be a function/,
14
+ callback: ({ contents }) => patchCode(contents, rule),
15
+ },
16
+ },
17
+ ]);
10
18
  }
11
19
  export const rule = `
12
20
  rule:
@@ -1,5 +1,5 @@
1
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
1
2
  import { describe, expect, test } from "vitest";
2
- import { patchCode } from "../ast/util.js";
3
3
  import { rule } from "./patch-depd-deprecations.js";
4
4
  describe("patchDepdDeprecations", () => {
5
5
  test("patch", () => {
@@ -1,2 +1,2 @@
1
- import type { ContentUpdater } from "./content-updater";
2
- export declare function fixRequire(updater: ContentUpdater): import("esbuild").Plugin;
1
+ import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
2
+ export declare function fixRequire(updater: ContentUpdater): Plugin;
@@ -1,37 +1,45 @@
1
1
  export function fixRequire(updater) {
2
- return updater.updateContent("fix-require", { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/, contentFilter: /.*/ }, ({ contents }) => {
3
- // `eval(...)` is not supported by workerd.
4
- contents = contents.replaceAll(`eval("require")`, "require");
5
- // `@opentelemetry` has a few issues.
6
- //
7
- // Next.js has the following code in `next/dist/server/lib/trace/tracer.js`:
8
- //
9
- // try {
10
- // api = require('@opentelemetry/api');
11
- // } catch (err) {
12
- // api = require('next/dist/compiled/@opentelemetry/api');
13
- // }
14
- //
15
- // The intent is to allow users to install their own version of `@opentelemetry/api`.
16
- //
17
- // The problem is that even when users do not explicitly install `@opentelemetry/api`,
18
- // `require('@opentelemetry/api')` resolves to the package which is a dependency
19
- // of Next.
20
- //
21
- // The second problem is that when Next traces files, it would not copy the `api/build/esm`
22
- // folder (used by the `module` conditions in package.json) it would only copy `api/build/src`.
23
- // This could be solved by updating the next config:
24
- //
25
- // const nextConfig: NextConfig = {
26
- // // ...
27
- // outputFileTracingIncludes: {
28
- // "*": ["./node_modules/@opentelemetry/api/build/**/*"],
29
- // },
30
- // };
31
- //
32
- // We can consider doing that when we want to enable users to install their own version
33
- // of `@opentelemetry/api`. For now we simply use the pre-compiled version.
34
- contents = contents.replace(/require\(.@opentelemetry\/api.\)/g, `require("next/dist/compiled/@opentelemetry/api")`);
35
- return contents;
36
- });
2
+ return updater.updateContent("fix-require", [
3
+ {
4
+ field: {
5
+ filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
6
+ contentFilter: /.*/,
7
+ callback: ({ contents }) => {
8
+ // `eval(...)` is not supported by workerd.
9
+ contents = contents.replaceAll(`eval("require")`, "require");
10
+ // `@opentelemetry` has a few issues.
11
+ //
12
+ // Next.js has the following code in `next/dist/server/lib/trace/tracer.js`:
13
+ //
14
+ // try {
15
+ // api = require('@opentelemetry/api');
16
+ // } catch (err) {
17
+ // api = require('next/dist/compiled/@opentelemetry/api');
18
+ // }
19
+ //
20
+ // The intent is to allow users to install their own version of `@opentelemetry/api`.
21
+ //
22
+ // The problem is that even when users do not explicitly install `@opentelemetry/api`,
23
+ // `require('@opentelemetry/api')` resolves to the package which is a dependency
24
+ // of Next.
25
+ //
26
+ // The second problem is that when Next traces files, it would not copy the `api/build/esm`
27
+ // folder (used by the `module` conditions in package.json) it would only copy `api/build/src`.
28
+ // This could be solved by updating the next config:
29
+ //
30
+ // const nextConfig: NextConfig = {
31
+ // // ...
32
+ // outputFileTracingIncludes: {
33
+ // "*": ["./node_modules/@opentelemetry/api/build/**/*"],
34
+ // },
35
+ // };
36
+ //
37
+ // We can consider doing that when we want to enable users to install their own version
38
+ // of `@opentelemetry/api`. For now we simply use the pre-compiled version.
39
+ contents = contents.replace(/require\(.@opentelemetry\/api.\)/g, `require("next/dist/compiled/@opentelemetry/api")`);
40
+ return contents;
41
+ },
42
+ },
43
+ },
44
+ ]);
37
45
  }
@@ -0,0 +1,3 @@
1
+ import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
2
+ export declare const rule = "\nrule:\n kind: await_expression\n inside:\n kind: if_statement\n stopBy: end\n has:\n kind: parenthesized_expression\n has: { kind: property_identifier , stopBy: end, regex: trustHostHeader}\n has:\n kind: call_expression\n all:\n - has: {kind: identifier, pattern: fetch}\n - has:\n kind: arguments\n all:\n - has:\n kind: object\n all:\n - has:\n kind: pair\n all:\n - has: {kind: property_identifier, regex: method }\n - has: {kind: string, regex: 'HEAD'}\n - has:\n kind: pair\n all:\n - has: {kind: property_identifier, regex: headers}\n - has: {kind: identifier, pattern: $HEADERS}\n - has:\n kind: template_string\n all:\n - has:\n kind: string_fragment\n regex: https://\n - has:\n kind: template_substitution\n all:\n - has: { kind: identifier, stopBy: end, pattern: $REQ }\n - has:\n kind: property_identifier\n regex: headers\n stopBy: end\n - has:\n kind: property_identifier\n regex: host\n stopBy: end\n - has:\n kind: template_substitution\n pattern: $URL_PATH\n has:\n kind: identifier\n\nfix: await (await import(\"@opennextjs/cloudflare\")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(`${$REQ.headers.host.includes(\"localhost\") ? \"http\":\"https\" }://${$REQ.headers.host}$URL_PATH`,{method:'HEAD', headers:$HEADERS})\n";
3
+ export declare const patchResRevalidate: CodePatcher;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * This patch will replace code used for `res.revalidate` in page router
3
+ * Without the patch it uses `fetch` to make a call to itself, which doesn't work once deployed in cloudflare workers
4
+ * This patch will replace this fetch by a call to `WORKER_SELF_REFERENCE` service binding
5
+ */
6
+ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
7
+ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
8
+ export const rule = `
9
+ rule:
10
+ kind: await_expression
11
+ inside:
12
+ kind: if_statement
13
+ stopBy: end
14
+ has:
15
+ kind: parenthesized_expression
16
+ has: { kind: property_identifier , stopBy: end, regex: trustHostHeader}
17
+ has:
18
+ kind: call_expression
19
+ all:
20
+ - has: {kind: identifier, pattern: fetch}
21
+ - has:
22
+ kind: arguments
23
+ all:
24
+ - has:
25
+ kind: object
26
+ all:
27
+ - has:
28
+ kind: pair
29
+ all:
30
+ - has: {kind: property_identifier, regex: method }
31
+ - has: {kind: string, regex: 'HEAD'}
32
+ - has:
33
+ kind: pair
34
+ all:
35
+ - has: {kind: property_identifier, regex: headers}
36
+ - has: {kind: identifier, pattern: $HEADERS}
37
+ - has:
38
+ kind: template_string
39
+ all:
40
+ - has:
41
+ kind: string_fragment
42
+ regex: https://
43
+ - has:
44
+ kind: template_substitution
45
+ all:
46
+ - has: { kind: identifier, stopBy: end, pattern: $REQ }
47
+ - has:
48
+ kind: property_identifier
49
+ regex: headers
50
+ stopBy: end
51
+ - has:
52
+ kind: property_identifier
53
+ regex: host
54
+ stopBy: end
55
+ - has:
56
+ kind: template_substitution
57
+ pattern: $URL_PATH
58
+ has:
59
+ kind: identifier
60
+
61
+ fix: await (await import("@opennextjs/cloudflare")).getCloudflareContext().env.WORKER_SELF_REFERENCE.fetch(\`\${$REQ.headers.host.includes("localhost") ? "http":"https" }://\${$REQ.headers.host}$URL_PATH\`,{method:'HEAD', headers:$HEADERS})
62
+ `;
63
+ export const patchResRevalidate = {
64
+ name: "patch-res-revalidate",
65
+ patches: [
66
+ {
67
+ versions: ">=14.2.0",
68
+ field: {
69
+ pathFilter: getCrossPlatformPathRegex(String.raw `(pages-api\.runtime\.prod\.js|node/api-resolver\.js)$`, {
70
+ escape: false,
71
+ }),
72
+ contentFilter: /\.trustHostHeader/,
73
+ patchCode: async ({ code }) => patchCode(code, rule),
74
+ },
75
+ },
76
+ ],
77
+ };