@aziontech/opennextjs-azion
Version:
Azion builder for next apps
82 lines (80 loc) • 3.12 kB
JavaScript
/**
* This code was originally copied and modified from the @opennextjs/cloudflare repository.
* Significant changes have been made to adapt it for use with Azion.
*/
/**
* This patch will replace code used for `res.revalidate` in page router
* Without the patch it uses `fetch` to make a call to itself, which doesn't work once deployed in cloudflare workers
* This patch will replace this fetch by a call to `WORKER_SELF_REFERENCE` service binding
*/
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
export const rule = `
rule:
kind: await_expression
inside:
kind: if_statement
stopBy: end
has:
kind: parenthesized_expression
has: { kind: property_identifier , stopBy: end, regex: trustHostHeader}
has:
kind: call_expression
all:
- has: {kind: identifier, pattern: fetch}
- has:
kind: arguments
all:
- has:
kind: object
all:
- has:
kind: pair
all:
- has: {kind: property_identifier, regex: method }
- has: {kind: string, regex: 'HEAD'}
- has:
kind: pair
all:
- has: {kind: property_identifier, regex: headers}
- has: {kind: identifier, pattern: $HEADERS}
- has:
kind: template_string
all:
- has:
kind: string_fragment
regex: https://
- has:
kind: template_substitution
all:
- has: { kind: identifier, stopBy: end, pattern: $REQ }
- has:
kind: property_identifier
regex: headers
stopBy: end
- has:
kind: property_identifier
regex: host
stopBy: end
- has:
kind: template_substitution
pattern: $URL_PATH
has:
kind: identifier
fix: await (await import("@aziontech/opennextjs-azion")).getAzionContext().env.WORKER_SELF_REFERENCE.fetch(\`\${$REQ.headers.host.includes("localhost") ? "http":"https" }://\${$REQ.headers.host}$URL_PATH\`,{method:'HEAD', headers:$HEADERS})
`;
export const patchResRevalidate = {
name: "patch-res-revalidate",
patches: [
{
versions: ">=13.5.0",
field: {
pathFilter: getCrossPlatformPathRegex(String.raw `(pages-api\.runtime\.prod\.js|node/api-resolver\.js)$`, {
escape: false,
}),
contentFilter: /\.trustHostHeader/,
patchCode: async ({ code }) => patchCode(code, rule),
},
},
],
};