0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

[ci] format

This commit is contained in:
Arsh 2024-02-07 13:37:44 +00:00 committed by astrobot-houston
parent e2fe51c828
commit b07a3846f5
4 changed files with 49 additions and 49 deletions

View file

@ -77,7 +77,7 @@ function getRedirectStatus(route: RouteData): number {
}
export function escapeRegex(content: string) {
return `^${getMatchPattern([[{ content, dynamic: false, spread: false }]])}$`
return `^${getMatchPattern([[{ content, dynamic: false, spread: false }]])}$`;
}
export function getRedirects(routes: RouteData[], config: AstroConfig): VercelRoute[] {

View file

@ -139,23 +139,23 @@ interface VercelISRConfig {
* A secret random string that you create.
* Its presence in the `__prerender_bypass` cookie will result in fresh responses being served, bypassing the cache. See Vercels documentation on [Draft Mode](https://vercel.com/docs/build-output-api/v3/features#draft-mode) for more information.
* Its presence in the `x-prerender-revalidate` header will result in a fresh response which will then be cached for all future requests to be used. See Vercels documentation on [On-Demand Incremental Static Regeneration (ISR)](https://vercel.com/docs/build-output-api/v3/features#on-demand-incremental-static-regeneration-isr) for more information.
*
*
* @default `undefined`
*/
bypassToken?: string;
/**
* Expiration time (in seconds) before the pages will be re-generated.
*
*
* Setting to `false` means that the page will stay cached as long as the current deployment is in production.
*
*
* @default `false`
*/
expiration?: number | false;
/**
* Paths that will always be served by a serverless function instead of an ISR function.
*
*
* @default `[]`
*/
exclude?: string[];
@ -260,18 +260,18 @@ export default function vercelServerless({
);
}
},
'astro:server:setup' ({ server }) {
'astro:server:setup'({ server }) {
// isr functions do not have access to search params, this middleware removes them for the dev mode
if (isr) {
const exclude_ = typeof isr === "object" ? isr.exclude ?? [] : [];
const exclude_ = typeof isr === 'object' ? isr.exclude ?? [] : [];
// we create a regex to emulate vercel's production behavior
const exclude = exclude_.concat("/_image").map(ex => new RegExp(escapeRegex(ex)));
const exclude = exclude_.concat('/_image').map((ex) => new RegExp(escapeRegex(ex)));
server.middlewares.use(function removeIsrParams(req, _, next) {
const { pathname } = new URL(`https://example.com${req.url}`);
if (exclude.some(ex => ex.test(pathname))) return next();
if (exclude.some((ex) => ex.test(pathname))) return next();
req.url = pathname;
return next();
})
});
}
},
'astro:build:ssr': async ({ entryPoints, middlewareEntryPoint }) => {
@ -330,25 +330,24 @@ export default function vercelServerless({
});
}
} else {
const entryFile = new URL(_serverEntry, _buildTempFolder)
const entryFile = new URL(_serverEntry, _buildTempFolder);
if (isr) {
const isrConfig = typeof isr === "object" ? isr : {};
const isrConfig = typeof isr === 'object' ? isr : {};
await builder.buildServerlessFolder(entryFile, NODE_PATH);
if (isrConfig.exclude?.length) {
const dest = _middlewareEntryPoint ? MIDDLEWARE_PATH : NODE_PATH;
for (const route of isrConfig.exclude) {
// vercel interprets src as a regex pattern, so we need to escape it
routeDefinitions.push({ src: escapeRegex(route), dest })
routeDefinitions.push({ src: escapeRegex(route), dest });
}
}
await builder.buildISRFolder(entryFile, '_isr', isrConfig);
for (const route of routes) {
const src = route.pattern.source;
const dest = src.startsWith("^\\/_image") ? NODE_PATH : ISR_PATH;
const dest = src.startsWith('^\\/_image') ? NODE_PATH : ISR_PATH;
if (!route.prerender) routeDefinitions.push({ src, dest });
}
}
else {
} else {
await builder.buildServerlessFolder(entryFile, NODE_PATH);
const dest = _middlewareEntryPoint ? MIDDLEWARE_PATH : NODE_PATH;
for (const route of routes) {
@ -413,7 +412,7 @@ export default function vercelServerless({
type Runtime = `nodejs${string}.x`;
class VercelBuilder {
readonly NTF_CACHE = {}
readonly NTF_CACHE = {};
constructor(
readonly config: AstroConfig,
@ -460,13 +459,16 @@ class VercelBuilder {
async buildISRFolder(entry: URL, functionName: string, isr: VercelISRConfig) {
await this.buildServerlessFolder(entry, functionName);
const prerenderConfig = new URL(`./functions/${functionName}.prerender-config.json`, this.config.outDir)
const prerenderConfig = new URL(
`./functions/${functionName}.prerender-config.json`,
this.config.outDir
);
// https://vercel.com/docs/build-output-api/v3/primitives#prerender-configuration-file
await writeJson(prerenderConfig, {
expiration: isr.expiration ?? false,
bypassToken: isr.bypassToken,
allowQuery: [ASTRO_PATH_PARAM],
passQuery: true
passQuery: true,
});
}
@ -478,7 +480,7 @@ class VercelBuilder {
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, this.config.srcDir),
new URL('./middleware.mjs', functionFolder)
);
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge',
entrypoint: 'middleware.mjs',

View file

@ -8,7 +8,7 @@ applyPolyfills();
export const createExports = (manifest: SSRManifest) => {
const app = new NodeApp(manifest);
const handler = async (req: IncomingMessage, res: ServerResponse) => {
const url = new URL(`https://example.com${req.url}`)
const url = new URL(`https://example.com${req.url}`);
const clientAddress = req.headers['x-forwarded-for'] as string | undefined;
const localsHeader = req.headers[ASTRO_LOCALS_HEADER];
const realPath = req.headers[ASTRO_PATH_HEADER] ?? url.searchParams.get(ASTRO_PATH_PARAM);

View file

@ -1,51 +1,49 @@
import { loadFixture } from "./test-utils.js";
import { expect } from "chai";
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
describe("ISR", () => {
describe('ISR', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: "./fixtures/isr/",
root: './fixtures/isr/',
});
await fixture.build();
});
it("generates expected prerender config", async () => {
it('generates expected prerender config', async () => {
const vcConfig = JSON.parse(
await fixture.readFile("../.vercel/output/functions/_isr.prerender-config.json")
await fixture.readFile('../.vercel/output/functions/_isr.prerender-config.json')
);
expect(vcConfig).to.deep.include({
"expiration": 120,
"bypassToken": "1c9e601d-9943-4e7c-9575-005556d774a8",
"allowQuery": ["x_astro_path"],
"passQuery": true
})
})
expiration: 120,
bypassToken: '1c9e601d-9943-4e7c-9575-005556d774a8',
allowQuery: ['x_astro_path'],
passQuery: true,
});
});
it("generates expected routes", async () => {
const deploymentConfig = JSON.parse(
await fixture.readFile("../.vercel/output/config.json")
);
it('generates expected routes', async () => {
const deploymentConfig = JSON.parse(await fixture.readFile('../.vercel/output/config.json'));
// the first two are /_astro/*, and filesystem routes
expect(deploymentConfig.routes.slice(2)).to.deep.equal([
{
"src": "^/two$",
"dest": "_render"
src: '^/two$',
dest: '_render',
},
{
"src": "^\\/_image$",
"dest": "_render"
src: '^\\/_image$',
dest: '_render',
},
{
"src": "^\\/one\\/?$",
"dest": "/_isr?x_astro_path=$0"
src: '^\\/one\\/?$',
dest: '/_isr?x_astro_path=$0',
},
{
"src": "^\\/two\\/?$",
"dest": "/_isr?x_astro_path=$0"
}
])
})
})
src: '^\\/two\\/?$',
dest: '/_isr?x_astro_path=$0',
},
]);
});
});