Vercel adapter default changes (#8239)

* Vercel adapter default changes

* Update .changeset/silly-dolphins-try.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Fix tests

* chore: correctly export `pageModule` when using `functionPerRoute`

* Update .changeset/silly-dolphins-try.md

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* Update .changeset/silly-dolphins-try.md

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* Make throw be the entrypoint

---------

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
This commit is contained in:
Matthew Phillips 2023-08-28 12:10:28 -04:00 committed by GitHub
parent ffc9e2d3de
commit 52f0837bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 8 deletions

View file

@ -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`.

View file

@ -163,7 +163,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
opts.settings.adapter?.adapterFeatures?.functionPerRoute opts.settings.adapter?.adapterFeatures?.functionPerRoute
) { ) {
// forcing to use undefined, so we fail in an expected way if the module is not even there. // 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) { if (ssrEntry) {
await generatePage(pageData, ssrEntry, builtPaths, pipeline); await generatePage(pageData, ssrEntry, builtPaths, pipeline);
} else { } else {

View file

@ -178,6 +178,8 @@ function vitePluginSSRSplit(
imports.push(...ssrCode.imports); imports.push(...ssrCode.imports);
contents.push(...ssrCode.contents); contents.push(...ssrCode.contents);
exports.push('export { pageModule }');
return `${imports.join('\n')}${contents.join('\n')}${exports.join('\n')}`; return `${imports.join('\n')}${contents.join('\n')}${exports.join('\n')}`;
} }
return void 0; return void 0;

View file

@ -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 ```js
// astro.config.mjs // astro.config.mjs
@ -222,7 +224,7 @@ import vercel from '@astrojs/vercel/serverless';
export default defineConfig({ export default defineConfig({
output: 'server', output: 'server',
adapter: vercel({ adapter: vercel({
functionPerRoute: true, functionPerRoute: false,
}), }),
}); });
``` ```

View file

@ -17,8 +17,8 @@
"bugs": "https://github.com/withastro/astro/issues", "bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://docs.astro.build/en/guides/integrations-guide/vercel/", "homepage": "https://docs.astro.build/en/guides/integrations-guide/vercel/",
"exports": { "exports": {
"./edge": "./dist/edge/adapter.js", "./edge": "./dist/edge/throw.js",
"./edge/entrypoint": "./dist/edge/entrypoint.js", "./edge/entrypoint": "./dist/edge/throw.js",
"./serverless": "./dist/serverless/adapter.js", "./serverless": "./dist/serverless/adapter.js",
"./serverless/entrypoint": "./dist/serverless/entrypoint.js", "./serverless/entrypoint": "./dist/serverless/entrypoint.js",
"./static": "./dist/static/adapter.js", "./static": "./dist/static/adapter.js",

View file

@ -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 {};

View file

@ -68,7 +68,7 @@ export default function vercelServerless({
analytics, analytics,
imageService, imageService,
imagesConfig, imagesConfig,
functionPerRoute = false, functionPerRoute = true,
edgeMiddleware = false, edgeMiddleware = false,
}: VercelServerlessConfig = {}): AstroIntegration { }: VercelServerlessConfig = {}): AstroIntegration {
let _config: AstroConfig; let _config: AstroConfig;

View file

@ -4,7 +4,7 @@ import vercel from '@astrojs/vercel/serverless';
export default defineConfig({ export default defineConfig({
adapter: vercel({ adapter: vercel({
// Pass some value to make sure it doesn't error out // Pass some value to make sure it doesn't error out
includeFiles: ['included.js'] includeFiles: ['included.js'],
}), }),
output: 'server' output: 'server'
}); });