Skip to content

Commit

Permalink
fix: include route prefix in vercel func names, fix #8401 (#8408)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
slawekkolodziej and natemoo-re authored Sep 6, 2023
1 parent 61ad70f commit 9ffa1a8
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-guests-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fix serverless function naming conflicts for routes with identical filenames but different directory structures
23 changes: 17 additions & 6 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,25 @@ You can set functionPerRoute: false to prevent surpassing the limit.`

// Multiple entrypoint support
if (_entryPoints.size) {
for (const [route, entryFile] of _entryPoints) {
const func = basename(entryFile.toString()).replace(/\.mjs$/, '');
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 = route.component.startsWith('src/pages/')
? getRouteFuncName(route)
: getFallbackFuncName(entryFile)

await createFunctionFolder(func, entryFile, filesToInclude, logger);
routeDefinitions.push({
src: route.pattern.source,
dest: func,
});
}
src: route.pattern.source,
dest: func,
});
}
} else {
await createFunctionFolder(
'render',
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'works'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-serverless-with-dynamic-routes",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const prerender = false;

export async function GET({ params }) {
return Response.json({
id: params.id
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
export const prerender = import.meta.env.PRERENDER;
---

<html>
<head>
<title>testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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;
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ffa1a8

Please sign in to comment.