|
| 1 | +import { createPatchCode } from "../astCodePatcher.js"; |
| 2 | +import type { CodePatcher } from "../codePatcher.js"; |
| 3 | + |
| 4 | +// This rule will replace the `NEXT_MINIMAL` env variable with true in multiple places to avoid executing unwanted path (i.e. next middleware, edge functions and image optimization) |
| 5 | +export const minimalRule = ` |
| 6 | +rule: |
| 7 | + kind: member_expression |
| 8 | + pattern: process.env.NEXT_MINIMAL |
| 9 | + any: |
| 10 | + - inside: |
| 11 | + kind: parenthesized_expression |
| 12 | + stopBy: end |
| 13 | + inside: |
| 14 | + kind: if_statement |
| 15 | + any: |
| 16 | + - inside: |
| 17 | + kind: statement_block |
| 18 | + inside: |
| 19 | + kind: method_definition |
| 20 | + any: |
| 21 | + - has: {kind: property_identifier, field: name, regex: runEdgeFunction} |
| 22 | + - has: {kind: property_identifier, field: name, regex: runMiddleware} |
| 23 | + - has: {kind: property_identifier, field: name, regex: imageOptimizer} |
| 24 | + - has: |
| 25 | + kind: statement_block |
| 26 | + has: |
| 27 | + kind: expression_statement |
| 28 | + pattern: res.statusCode = 400; |
| 29 | +fix: |
| 30 | + 'true' |
| 31 | +`; |
| 32 | + |
| 33 | +// This rule will disable the background preloading of route done by NextServer by default during the creation of NextServer |
| 34 | +export const disablePreloadingRule = ` |
| 35 | +rule: |
| 36 | + kind: statement_block |
| 37 | + inside: |
| 38 | + kind: if_statement |
| 39 | + any: |
| 40 | + - has: |
| 41 | + kind: member_expression |
| 42 | + pattern: this.nextConfig.experimental.preloadEntriesOnStart |
| 43 | + stopBy: end |
| 44 | + - has: |
| 45 | + kind: binary_expression |
| 46 | + pattern: appDocumentPreloading === true |
| 47 | + stopBy: end |
| 48 | +fix: |
| 49 | + '{}' |
| 50 | +`; |
| 51 | + |
| 52 | +// This rule is mostly for splitted edge functions so that we don't try to match them on the other non edge functions |
| 53 | +export const removeMiddlewareManifestRule = ` |
| 54 | +rule: |
| 55 | + kind: statement_block |
| 56 | + inside: |
| 57 | + kind: method_definition |
| 58 | + has: |
| 59 | + kind: property_identifier |
| 60 | + regex: getMiddlewareManifest |
| 61 | +fix: |
| 62 | + '{return null;}' |
| 63 | +`; |
| 64 | + |
| 65 | +export const patchNextServer: CodePatcher = { |
| 66 | + name: "patch-next-server", |
| 67 | + patches: [ |
| 68 | + // Skip executing next middleware, edge functions and image optimization inside NextServer |
| 69 | + { |
| 70 | + versions: ">=15.0.0", |
| 71 | + field: { |
| 72 | + pathFilter: /next-server\.(js)$/, |
| 73 | + contentFilter: /process\.env\.NEXT_MINIMAL/, |
| 74 | + patchCode: createPatchCode(minimalRule), |
| 75 | + }, |
| 76 | + }, |
| 77 | + // Disable Next background preloading done at creation of `NetxServer` |
| 78 | + { |
| 79 | + versions: ">=15.0.0", |
| 80 | + field: { |
| 81 | + pathFilter: /next-server\.(js)$/, |
| 82 | + contentFilter: /this\.nextConfig\.experimental\.preloadEntriesOnStart/, |
| 83 | + patchCode: createPatchCode(disablePreloadingRule), |
| 84 | + }, |
| 85 | + }, |
| 86 | + // Don't match edge functions in `NextServer` |
| 87 | + { |
| 88 | + versions: ">=15.0.0", |
| 89 | + field: { |
| 90 | + pathFilter: /next-server\.(js)$/, |
| 91 | + contentFilter: /getMiddlewareManifest/, |
| 92 | + patchCode: createPatchCode(removeMiddlewareManifestRule), |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | +}; |
0 commit comments