Skip to content

fix: fixes specifying / as a path for v2 api functions #6045

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

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/lib/functions/netlify-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export default class NetlifyFunction {
async matchURLPath(rawPath, method) {
await this.buildQueue

const path = (rawPath.endsWith('/') ? rawPath.slice(0, -1) : rawPath).toLowerCase()
let path = rawPath !== '/' && rawPath.endsWith('/') ? rawPath.slice(0, -1) : rawPath
path = path.toLowerCase()
const { routes = [] } = this.buildData
return routes.find(({ expression, literal, methods }) => {
if (methods.length !== 0 && !methods.includes(method)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async (req) => new Response(`Catchall Path`)

export const config = {
path: '/*',
method: 'PATCH',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default async (req) => new Response(`With literal path: ${req.url}`)

export const config = {
path: '/',
method: 'GET'
}
15 changes: 15 additions & 0 deletions tests/integration/commands/dev/v2-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ describe.runIf(gte(version, '18.13.0'))('v2 api', () => {
expect(await response.text()).toBe(`With expression path: {"sku":"netlify"}`)
})

test<FixtureTestContext>('should serve the custom path ath the / route as specified in the in source config', async ({
devServer,
}) => {
const url = `http://localhost:${devServer.port}/`
const response = await fetch(url)
expect(response.status).toBe(200)
expect(await response.text()).toBe(`With literal path: http://localhost:${devServer.port}/`)
})

test<FixtureTestContext>('catchall path applies to root path', async ({ devServer }) => {
const response = await fetch( `http://localhost:${devServer.port}/`, { method:"PATCH"})
expect(response.status).toBe(200)
expect(await response.text()).toBe(`Catchall Path`)
})

test<FixtureTestContext>('returns 404 when using the default function URL to access a function with custom routes', async ({
devServer,
}) => {
Expand Down