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; +--- + + +
+