diff --git a/.changeset/modern-guests-float.md b/.changeset/modern-guests-float.md new file mode 100644 index 000000000..cda74642a --- /dev/null +++ b/.changeset/modern-guests-float.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Fix serverless function naming conflicts for routes with identical filenames but different directory structures diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index a7178b3e3..32735da03 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -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', diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/astro.config.mjs new file mode 100644 index 000000000..f5a86e609 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/astro.config.mjs @@ -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' +}); diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/included.js b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/included.js new file mode 100644 index 000000000..4e64b2d61 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/included.js @@ -0,0 +1 @@ +'works' \ No newline at end of file diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/package.json b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/package.json new file mode 100644 index 000000000..e22a7e932 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-vercel-serverless-with-dynamic-routes", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/vercel": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/[id]/index.astro b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/[id]/index.astro new file mode 100644 index 000000000..4eab5952d --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/[id]/index.astro @@ -0,0 +1,12 @@ +--- +export const prerender = false; +--- + + + + testing {Astro.params.id} + + +

testing {Astro.params.id}

+ + diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/api/[id].js b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/api/[id].js new file mode 100644 index 000000000..f54e673a8 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/api/[id].js @@ -0,0 +1,7 @@ +export const prerender = false; + +export async function GET({ params }) { + return Response.json({ + id: params.id + }); +} diff --git a/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/index.astro new file mode 100644 index 000000000..b6b833e53 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/index.astro @@ -0,0 +1,12 @@ +--- +export const prerender = import.meta.env.PRERENDER; +--- + + + + testing + + +

testing

+ + diff --git a/packages/integrations/vercel/test/serverless-with-dynamic-routes.test.js b/packages/integrations/vercel/test/serverless-with-dynamic-routes.test.js new file mode 100644 index 000000000..325f9c5b0 --- /dev/null +++ b/packages/integrations/vercel/test/serverless-with-dynamic-routes.test.js @@ -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; + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43b0c149e..ff3a57a4c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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':