Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 23 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,31 @@ 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$/, '');
// TODO: Currently this function directly checks for a single route
// injected in packages/astro/src/assets/internal.ts Ideally this
// should be done dynamically
const isInjectedRoute = (route: RouteData) =>
route.pathname === '/_image';
ematipico marked this conversation as resolved.
Show resolved Hide resolved

const getInjectedFuncName = (entryFile: URL) =>
basename(entryFile.toString())
.replace(/^entry\./, '')
.replace(/\.mjs$/, '');
ematipico marked this conversation as resolved.
Show resolved Hide resolved

const getFuncName = (route: RouteData) =>
route.component.replace(/^src\/pages\//, '')

for (const [route, entryFile] of _entryPoints) {
const func = isInjectedRoute(route)
? getInjectedFuncName(entryFile)
: getFuncName(route)

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.

Loading