|
| 1 | +import { patchCode } from "../ast/util.js"; |
| 2 | +import { ContentUpdater } from "./content-updater.js"; |
| 3 | + |
| 4 | +// We try to be as specific as possible to avoid patching the wrong thing here |
| 5 | +// 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. |
| 6 | +// 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. |
| 7 | +// If it's a bug in workerd and it's not expected to throw an error, we can remove this patch. |
| 8 | +export const abortControllerRule = ` |
| 9 | +rule: |
| 10 | + all: |
| 11 | + - kind: lexical_declaration |
| 12 | + pattern: let $VAR = new AbortController |
| 13 | + - precedes: |
| 14 | + kind: function_declaration |
| 15 | + stopBy: end |
| 16 | + has: |
| 17 | + kind: statement_block |
| 18 | + has: |
| 19 | + kind: try_statement |
| 20 | + has: |
| 21 | + kind: catch_clause |
| 22 | + has: |
| 23 | + kind: statement_block |
| 24 | + has: |
| 25 | + kind: return_statement |
| 26 | + all: |
| 27 | + - has: |
| 28 | + stopBy: end |
| 29 | + kind: member_expression |
| 30 | + pattern: $VAR.signal.aborted |
| 31 | + - has: |
| 32 | + stopBy: end |
| 33 | + kind: call_expression |
| 34 | + regex: console.error\\("Failed to fetch RSC payload for |
| 35 | + |
| 36 | +fix: |
| 37 | + 'let $VAR = {signal:{aborted: false}};' |
| 38 | +` |
| 39 | + |
| 40 | +// This rule is used instead of defining `process.env.NEXT_MINIMAL` in the `esbuild config. |
| 41 | +// Do we want to entirely replace these functions to reduce the bundle size? |
| 42 | +// 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. |
| 43 | +export const nextMinimalRule = ` |
| 44 | +rule: |
| 45 | + kind: member_expression |
| 46 | + pattern: process.env.NEXT_MINIMAL |
| 47 | + any: |
| 48 | + - inside: |
| 49 | + kind: parenthesized_expression |
| 50 | + stopBy: end |
| 51 | + inside: |
| 52 | + kind: if_statement |
| 53 | + any: |
| 54 | + - inside: |
| 55 | + kind: statement_block |
| 56 | + inside: |
| 57 | + kind: method_definition |
| 58 | + any: |
| 59 | + - has: {kind: property_identifier, field: name, regex: runEdgeFunction} |
| 60 | + - has: {kind: property_identifier, field: name, regex: runMiddleware} |
| 61 | + - has: {kind: property_identifier, field: name, regex: imageOptimizer} |
| 62 | + - has: |
| 63 | + kind: statement_block |
| 64 | + has: |
| 65 | + kind: expression_statement |
| 66 | + pattern: res.statusCode = 400; |
| 67 | +fix: |
| 68 | + 'true' |
| 69 | +` |
| 70 | + |
| 71 | +export function patchNextMinimal(updater: ContentUpdater) { |
| 72 | + updater.updateContent("patch-abortController-next15.2", { filter: /app-page\.runtime\.prod\.(js)$/, contentFilter: /new AbortController/ }, async ({ contents }) => { |
| 73 | + return patchCode(contents, abortControllerRule) |
| 74 | + }); |
| 75 | + |
| 76 | + updater.updateContent("patch-next-minimal", { filter: /next-server\.(js)$/, contentFilter: /.*/ }, async ({ contents }) => { |
| 77 | + return patchCode(contents, nextMinimalRule) |
| 78 | + }); |
| 79 | + |
| 80 | + return { |
| 81 | + name: "patch-abortController", |
| 82 | + setup() { }, |
| 83 | + }; |
| 84 | +} |
0 commit comments