From 9ffa1a84e81f52d55ffe07826b8b1f10fc023ee9 Mon Sep 17 00:00:00 2001 From: Slawek Kolodziej Date: Wed, 6 Sep 2023 06:43:53 +0200 Subject: [PATCH] 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 --- .changeset/modern-guests-float.md | 5 ++++ .../vercel/src/serverless/adapter.ts | 23 ++++++++++++++----- .../astro.config.mjs | 10 ++++++++ .../included.js | 1 + .../package.json | 9 ++++++++ .../src/pages/[id]/index.astro | 12 ++++++++++ .../src/pages/api/[id].js | 7 ++++++ .../src/pages/index.astro | 12 ++++++++++ .../serverless-with-dynamic-routes.test.js | 22 ++++++++++++++++++ pnpm-lock.yaml | 9 ++++++++ 10 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 .changeset/modern-guests-float.md create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/astro.config.mjs create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/included.js create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/package.json create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/[id]/index.astro create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/api/[id].js create mode 100644 packages/integrations/vercel/test/fixtures/serverless-with-dynamic-routes/src/pages/index.astro create mode 100644 packages/integrations/vercel/test/serverless-with-dynamic-routes.test.js diff --git a/.changeset/modern-guests-float.md b/.changeset/modern-guests-float.md new file mode 100644 index 000000000000..cda74642aac0 --- /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 a7178b3e3c59..32735da03bd6 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 000000000000..f5a86e609939 --- /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 000000000000..4e64b2d616bf --- /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 000000000000..e22a7e932aea --- /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 000000000000..4eab5952d650 --- /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 000000000000..f54e673a8919 --- /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 000000000000..b6b833e5358f --- /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 000000000000..325f9c5b0460 --- /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 43b0c149e3ec..ff3a57a4ca44 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':