fix: include route prefix in vercel func names, fix #8401 (#8408)

* fix: include route prefix in vercel func names

* chore: add changeset

* chore: update pnpm lockfile

* refactor: simplify logic that generates vercel func names

* fix: properly remove entryFile prefix from func name

* refactor: change how vercel function names are generated

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Slawek Kolodziej 2023-09-06 06:43:53 +02:00 committed by GitHub
parent 61ad70fdc5
commit 9ffa1a84e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 104 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---
Fix serverless function naming conflicts for routes with identical filenames but different directory structures

View file

@ -207,8 +207,19 @@ You can set functionPerRoute: false to prevent surpassing the limit.`
// Multiple entrypoint support
if (_entryPoints.size) {
const getRouteFuncName = (route: RouteData) =>
route.component.replace('src/pages/', '')
const getFallbackFuncName = (entryFile: URL) =>
basename(entryFile.toString())
.replace('entry.', '')
.replace(/\.mjs$/, '');
for (const [route, entryFile] of _entryPoints) {
const func = basename(entryFile.toString()).replace(/\.mjs$/, '');
const func = route.component.startsWith('src/pages/')
? getRouteFuncName(route)
: getFallbackFuncName(entryFile)
await createFunctionFolder(func, entryFile, filesToInclude, logger);
routeDefinitions.push({
src: route.pattern.source,

View file

@ -0,0 +1,10 @@
import { defineConfig } from 'astro/config';
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'],
}),
output: 'server'
});

View file

@ -0,0 +1 @@
'works'

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-serverless-with-dynamic-routes",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,12 @@
---
export const prerender = false;
---
<html>
<head>
<title>testing {Astro.params.id}</title>
</head>
<body>
<h1>testing {Astro.params.id}</h1>
</body>
</html>

View file

@ -0,0 +1,7 @@
export const prerender = false;
export async function GET({ params }) {
return Response.json({
id: params.id
});
}

View file

@ -0,0 +1,12 @@
---
export const prerender = import.meta.env.PRERENDER;
---
<html>
<head>
<title>testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>

View file

@ -0,0 +1,22 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
describe('Serverless with dynamic routes', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
before(async () => {
process.env.PRERENDER = true;
fixture = await loadFixture({
root: './fixtures/serverless-with-dynamic-routes/',
output: 'hybrid',
});
await fixture.build();
});
it('build successful', async () => {
expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok;
expect(await fixture.readFile('../.vercel/output/functions/[id]/index.astro.func/.vc-config.json')).to.be.ok;
expect(await fixture.readFile('../.vercel/output/functions/api/[id].js.func/.vc-config.json')).to.be.ok;
});
});

View file

@ -4812,6 +4812,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes:
dependencies:
'@astrojs/vercel':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/vercel/test/fixtures/static-assets:
dependencies:
'@astrojs/vercel':