From 52f0837bdeca0b54e07cbf76a7570bd042b98922 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 28 Aug 2023 12:10:28 -0400 Subject: [PATCH] Vercel adapter default changes (#8239) * Vercel adapter default changes * Update .changeset/silly-dolphins-try.md Co-authored-by: Sarah Rainsberger * Fix tests * chore: correctly export `pageModule` when using `functionPerRoute` * Update .changeset/silly-dolphins-try.md Co-authored-by: Emanuele Stoppa * Update .changeset/silly-dolphins-try.md Co-authored-by: Emanuele Stoppa * Make throw be the entrypoint --------- Co-authored-by: Sarah Rainsberger Co-authored-by: Emanuele Stoppa --- .changeset/silly-dolphins-try.md | 9 +++++++++ packages/astro/src/core/build/generate.ts | 2 +- .../astro/src/core/build/plugins/plugin-ssr.ts | 2 ++ packages/integrations/vercel/README.md | 8 +++++--- packages/integrations/vercel/package.json | 4 ++-- packages/integrations/vercel/src/edge/throw.ts | 18 ++++++++++++++++++ .../vercel/src/serverless/adapter.ts | 2 +- .../serverless-prerender/astro.config.mjs | 2 +- 8 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 .changeset/silly-dolphins-try.md create mode 100644 packages/integrations/vercel/src/edge/throw.ts diff --git a/.changeset/silly-dolphins-try.md b/.changeset/silly-dolphins-try.md new file mode 100644 index 000000000..dcd15130d --- /dev/null +++ b/.changeset/silly-dolphins-try.md @@ -0,0 +1,9 @@ +--- +'@astrojs/vercel': major +--- + +Vercel adapter now defaults to `functionPerRoute`. + +With this change, `@astrojs/vercel/serverless` now splits each route into its own function. By doing this, the size of each function is reduced and startup time is faster. + +You can disable this option, which will cause the code to be bundled into a single function, by setting `functionPerRoute` to `false`. diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 0a3d80000..53a350342 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -163,7 +163,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn opts.settings.adapter?.adapterFeatures?.functionPerRoute ) { // forcing to use undefined, so we fail in an expected way if the module is not even there. - const ssrEntry = ssrEntryPage?.manifest?.pageModule; + const ssrEntry = ssrEntryPage?.pageModule; if (ssrEntry) { await generatePage(pageData, ssrEntry, builtPaths, pipeline); } else { diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 8650d5870..44fc31155 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -178,6 +178,8 @@ function vitePluginSSRSplit( imports.push(...ssrCode.imports); contents.push(...ssrCode.contents); + exports.push('export { pageModule }'); + return `${imports.join('\n')}${contents.join('\n')}${exports.join('\n')}`; } return void 0; diff --git a/packages/integrations/vercel/README.md b/packages/integrations/vercel/README.md index b01a7c716..e16d98390 100644 --- a/packages/integrations/vercel/README.md +++ b/packages/integrations/vercel/README.md @@ -210,9 +210,11 @@ export default defineConfig({ }); ``` -### Per-page functions +### Function bundling configuration -The Vercel adapter builds to a single function by default. Astro 2.7 added support for splitting your build into separate entry points per page. If you use this configuration the Vercel adapter will generate a separate function for each page. This can help reduce the size of each function so they are only bundling code used on that page. +The Vercel adapter splits builds into a separate function per route by default. This helps reduce the size of each function, as it only bundles code used on that page. + +You can disable this and build to a single function by setting the `functionPerRoute` configuration option to `false`: ```js // astro.config.mjs @@ -222,7 +224,7 @@ import vercel from '@astrojs/vercel/serverless'; export default defineConfig({ output: 'server', adapter: vercel({ - functionPerRoute: true, + functionPerRoute: false, }), }); ``` diff --git a/packages/integrations/vercel/package.json b/packages/integrations/vercel/package.json index fc622776c..726f6f9d4 100644 --- a/packages/integrations/vercel/package.json +++ b/packages/integrations/vercel/package.json @@ -17,8 +17,8 @@ "bugs": "https://github.com/withastro/astro/issues", "homepage": "https://docs.astro.build/en/guides/integrations-guide/vercel/", "exports": { - "./edge": "./dist/edge/adapter.js", - "./edge/entrypoint": "./dist/edge/entrypoint.js", + "./edge": "./dist/edge/throw.js", + "./edge/entrypoint": "./dist/edge/throw.js", "./serverless": "./dist/serverless/adapter.js", "./serverless/entrypoint": "./dist/serverless/entrypoint.js", "./static": "./dist/static/adapter.js", diff --git a/packages/integrations/vercel/src/edge/throw.ts b/packages/integrations/vercel/src/edge/throw.ts new file mode 100644 index 000000000..5041370e0 --- /dev/null +++ b/packages/integrations/vercel/src/edge/throw.ts @@ -0,0 +1,18 @@ +const msg = ` +The Astro Vercel Edge adapter has been removed. We recommend switching to @astrojs/vercel/serverless and enabling Edge middleware. + +import { defineConfig } from 'astro/config'; +import vercel from '@astrojs/vercel/serverless'; + +export default defineConfig({ + output: 'server', + adapter: vercel({ + edgeMiddleware: true, + }) +}) +`.trim(); + +throw new Error(msg); + +// Make sure bundlers treat this as ESM. +export default {}; diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index d5ac9c881..0bfdd31f7 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -68,7 +68,7 @@ export default function vercelServerless({ analytics, imageService, imagesConfig, - functionPerRoute = false, + functionPerRoute = true, edgeMiddleware = false, }: VercelServerlessConfig = {}): AstroIntegration { let _config: AstroConfig; diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs index 84b96c27d..f5a86e609 100644 --- a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs @@ -4,7 +4,7 @@ import vercel from '@astrojs/vercel/serverless'; export default defineConfig({ adapter: vercel({ // Pass some value to make sure it doesn't error out - includeFiles: ['included.js'] + includeFiles: ['included.js'], }), output: 'server' });