From 56658ceda0564aa9a97a257f184e09f028fa0b7f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sat, 26 Aug 2023 09:09:05 -0400 Subject: [PATCH 1/7] Vercel adapter default changes --- .changeset/silly-dolphins-try.md | 9 +++++++++ packages/integrations/vercel/README.md | 8 +++++--- .../integrations/vercel/src/edge/adapter.ts | 1 + .../integrations/vercel/src/edge/entrypoint.ts | 1 + packages/integrations/vercel/src/edge/throw.ts | 18 ++++++++++++++++++ .../vercel/src/serverless/adapter.ts | 2 +- 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .changeset/silly-dolphins-try.md create mode 100644 packages/integrations/vercel/src/edge/adapter.ts create mode 100644 packages/integrations/vercel/src/edge/entrypoint.ts 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 000000000000..0d8fdef0dcee --- /dev/null +++ b/.changeset/silly-dolphins-try.md @@ -0,0 +1,9 @@ +--- +'@astrojs/vercel': major +--- + +Vercel adapter now defaults to a function per route + +With this change, in `@astrojs/vercel/serverless`, now each route is split 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/integrations/vercel/README.md b/packages/integrations/vercel/README.md index b01a7c7162ab..e16d98390ebe 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/src/edge/adapter.ts b/packages/integrations/vercel/src/edge/adapter.ts new file mode 100644 index 000000000000..080554cf8d27 --- /dev/null +++ b/packages/integrations/vercel/src/edge/adapter.ts @@ -0,0 +1 @@ +import './throw.js'; diff --git a/packages/integrations/vercel/src/edge/entrypoint.ts b/packages/integrations/vercel/src/edge/entrypoint.ts new file mode 100644 index 000000000000..080554cf8d27 --- /dev/null +++ b/packages/integrations/vercel/src/edge/entrypoint.ts @@ -0,0 +1 @@ +import './throw.js'; diff --git a/packages/integrations/vercel/src/edge/throw.ts b/packages/integrations/vercel/src/edge/throw.ts new file mode 100644 index 000000000000..5041370e06bb --- /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 d5ac9c881063..0bfdd31f7617 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; From 23e3eb2f3b403d898cb4bfbb487083958d6490fc Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sat, 26 Aug 2023 11:00:58 -0400 Subject: [PATCH 2/7] Update .changeset/silly-dolphins-try.md Co-authored-by: Sarah Rainsberger --- .changeset/silly-dolphins-try.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/silly-dolphins-try.md b/.changeset/silly-dolphins-try.md index 0d8fdef0dcee..aaff4a129e79 100644 --- a/.changeset/silly-dolphins-try.md +++ b/.changeset/silly-dolphins-try.md @@ -4,6 +4,6 @@ Vercel adapter now defaults to a function per route -With this change, in `@astrojs/vercel/serverless`, now each route is split into its own function. By doing this the size of each function is reduced and startup time is faster. +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. From 19dbc027c922c70552df85810d5ad4b17d206e7c Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sat, 26 Aug 2023 11:24:32 -0400 Subject: [PATCH 3/7] Fix tests --- .../vercel/test/fixtures/serverless-prerender/astro.config.mjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 84b96c27d409..b241c7916546 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,8 @@ 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'], + functionPerRoute: false, }), output: 'server' }); From 476cb73c94f75764653a8c70e15ba70d7a190000 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 28 Aug 2023 10:19:12 +0100 Subject: [PATCH 4/7] chore: correctly export `pageModule` when using `functionPerRoute` --- packages/astro/src/core/build/generate.ts | 2 +- packages/astro/src/core/build/plugins/plugin-ssr.ts | 2 ++ .../vercel/test/fixtures/serverless-prerender/astro.config.mjs | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 0a3d800007aa..53a3503421a5 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 8650d5870fe1..44fc31155c21 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/test/fixtures/serverless-prerender/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs index b241c7916546..f5a86e609939 100644 --- a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs @@ -5,7 +5,6 @@ export default defineConfig({ adapter: vercel({ // Pass some value to make sure it doesn't error out includeFiles: ['included.js'], - functionPerRoute: false, }), output: 'server' }); From 6791853693e45fb2dd41fc7c026a0005b793018d Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 28 Aug 2023 08:21:53 -0400 Subject: [PATCH 5/7] Update .changeset/silly-dolphins-try.md Co-authored-by: Emanuele Stoppa --- .changeset/silly-dolphins-try.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/silly-dolphins-try.md b/.changeset/silly-dolphins-try.md index aaff4a129e79..6bbea676b5d0 100644 --- a/.changeset/silly-dolphins-try.md +++ b/.changeset/silly-dolphins-try.md @@ -2,7 +2,7 @@ '@astrojs/vercel': major --- -Vercel adapter now defaults to a function per route +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. From a719631a5a19860d1d214b4e492e4ac475b16c03 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 28 Aug 2023 08:22:02 -0400 Subject: [PATCH 6/7] Update .changeset/silly-dolphins-try.md Co-authored-by: Emanuele Stoppa --- .changeset/silly-dolphins-try.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/silly-dolphins-try.md b/.changeset/silly-dolphins-try.md index 6bbea676b5d0..dcd15130dc14 100644 --- a/.changeset/silly-dolphins-try.md +++ b/.changeset/silly-dolphins-try.md @@ -6,4 +6,4 @@ 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. +You can disable this option, which will cause the code to be bundled into a single function, by setting `functionPerRoute` to `false`. From 281fdad6ebf79b8e43f2338c0626c4e343eb1aab Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 28 Aug 2023 08:27:42 -0400 Subject: [PATCH 7/7] Make throw be the entrypoint --- packages/integrations/vercel/package.json | 4 ++-- packages/integrations/vercel/src/edge/adapter.ts | 1 - packages/integrations/vercel/src/edge/entrypoint.ts | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) delete mode 100644 packages/integrations/vercel/src/edge/adapter.ts delete mode 100644 packages/integrations/vercel/src/edge/entrypoint.ts diff --git a/packages/integrations/vercel/package.json b/packages/integrations/vercel/package.json index fc622776cc86..726f6f9d42f2 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/adapter.ts b/packages/integrations/vercel/src/edge/adapter.ts deleted file mode 100644 index 080554cf8d27..000000000000 --- a/packages/integrations/vercel/src/edge/adapter.ts +++ /dev/null @@ -1 +0,0 @@ -import './throw.js'; diff --git a/packages/integrations/vercel/src/edge/entrypoint.ts b/packages/integrations/vercel/src/edge/entrypoint.ts deleted file mode 100644 index 080554cf8d27..000000000000 --- a/packages/integrations/vercel/src/edge/entrypoint.ts +++ /dev/null @@ -1 +0,0 @@ -import './throw.js';