Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vercel adapter default changes #8239

Merged
merged 9 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/silly-dolphins-try.md
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions packages/integrations/vercel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -222,7 +224,7 @@ import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
output: 'server',
adapter: vercel({
functionPerRoute: true,
functionPerRoute: false,
}),
});
```
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions packages/integrations/vercel/src/edge/throw.ts
Original file line number Diff line number Diff line change
@@ -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 {};
2 changes: 1 addition & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function vercelServerless({
analytics,
imageService,
imagesConfig,
functionPerRoute = false,
functionPerRoute = true,
edgeMiddleware = false,
}: VercelServerlessConfig = {}): AstroIntegration {
let _config: AstroConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});