@aziontech/opennextjs-azion
Version:
Azion builder for next apps
56 lines (55 loc) • 2.06 kB
JavaScript
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
// This rule rewrites the `invokeHeaders` function to use the `routingResult` object
// send headers from the next.js server
export function inlinePatchRewriteInvokeHeaders(updater) {
return updater.updateContent("patch-rewrite-invoke-headers", [
{
field: {
filter: getCrossPlatformPathRegex(String.raw `/server-functions/default/index\.mjs$`, {
escape: false,
}),
contentFilter: /async function processRequest\s*\(/,
callback: ({ contents }) => patchCode(contents, ruleRewriteInvokeHeaders),
},
},
]);
}
// Rule to rewrite the `invokeHeaders` function
export const ruleRewriteInvokeHeaders = `
rule:
inside:
pattern: function processRequest($$$) { $$$ }
stopBy: end
field: body
pattern: |
const requestMetadata = $$$META;
fix: |-
req.headers['x-invoke-path'] = routingResult.internalEvent.rawPath;
req.headers['x-invoke-query'] = JSON.stringify(routingResult.internalEvent.query);
if (invokeStatus) {
req.headers['x-invoke-status'] = invokeStatus;
}
const requestMetadata = $$$META;
`;
// send initial URL to next.js server
export function inlinePatchRewriteURLSource(updater) {
return updater.updateContent("patch-rewrite-url-source", [
{
field: {
filter: getCrossPlatformPathRegex(String.raw `/server-functions/default/index\.mjs$`, {
escape: false,
}),
contentFilter: /async function openNextHandler\s*\(/,
callback: ({ contents }) => patchCode(contents, ruleRewriteURLSource),
},
},
]);
}
export const ruleRewriteURLSource = `
rule:
pattern: |
const { search, pathname, hash } = new URL(preprocessedEvent.url);
fix: |-
const { search, pathname, hash } = new URL(routingResult.initialURL);
`;