Skip to content

fix: handle v1 edge function definition #1903

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 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,27 @@ export const writeRscDataEdgeFunction = async ({
]
}

const getEdgeFunctionPatternForPage = ({
export const getEdgeFunctionPatternForPage = ({
edgeFunctionDefinition,
pageRegexMap,
appPathRoutesManifest,
}: {
edgeFunctionDefinition: EdgeFunctionDefinitionV2
edgeFunctionDefinition: EdgeFunctionDefinition
pageRegexMap: Map<string, string>
appPathRoutesManifest?: Record<string, string>
}): string => {
// We don't just use the matcher from the edge function definition, because it doesn't handle trailing slashes

// appDir functions have a name that _isn't_ the route name, but rather the route with `/page` appended
const regexp = pageRegexMap.get(appPathRoutesManifest?.[edgeFunctionDefinition.page] ?? edgeFunctionDefinition.page)
if (regexp) {
return regexp
}
if ('regexp' in edgeFunctionDefinition) {
return edgeFunctionDefinition.regexp.replace(/([^/])\$$/, '$1/?$')
}
// If we need to fall back to the matcher, we need to add an optional trailing slash
return regexp ?? edgeFunctionDefinition.matchers[0].regexp.replace(/([^/])\$$/, '$1/?$')
return edgeFunctionDefinition.matchers?.[0].regexp.replace(/([^/])\$$/, '$1/?$')
}

/**
Expand Down
108 changes: 107 additions & 1 deletion test/matchers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeLocaleOptional, stripLookahead } from '../packages/runtime/src/helpers/matchers'

import { getEdgeFunctionPatternForPage } from '../packages/runtime/src/helpers/edge'
const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json`

function checkPath(path: string, regex: string) {
Expand Down Expand Up @@ -68,3 +68,109 @@ describe('the middleware path matcher', () => {
expect(checkPath('/shows/888', stripped)).toBe(true)
})
})

const pageRegexMap = new Map([
['/api/shows/[id]', '^/api/shows/([^/]+?)(?:/)?$'],
['/api/shows/[...params]', '^/api/shows/(.+?)(?:/)?$'],
['/app-edge/[id]', '^/app\\-edge/([^/]+?)(?:/)?$'],
['/blog/[author]', '^/blog/([^/]+?)(?:/)?$'],
['/blog/[author]/[slug]', '^/blog/([^/]+?)/([^/]+?)(?:/)?$'],
['/getServerSideProps/all/[[...slug]]', '^/getServerSideProps/all(?:/(.+?))?(?:/)?$'],
['/getServerSideProps/[id]', '^/getServerSideProps/([^/]+?)(?:/)?$'],
])

const appPathRoutesManifest = {
'/app-edge/[id]/page': '/app-edge/[id]',
'/blog/[author]/[slug]/page': '/blog/[author]/[slug]',
'/blog/[author]/page': '/blog/[author]',
}

describe('the edge function matcher helpers', () => {
it('finds the correct regex for an edge API route', () => {
const regex = getEdgeFunctionPatternForPage({
pageRegexMap,
appPathRoutesManifest,
edgeFunctionDefinition: {
name: 'pages/api/og',
page: '/api/og',
env: [],
files: [],
matchers: [
{
regexp: '^/api/og$',
},
],
wasm: [],
assets: [],
},
})
expect(regex).toBe('^/api/og/?$')
expect('/api/og').toMatch(new RegExp(regex))
expect('/api/og/').toMatch(new RegExp(regex))
})

it('finds the correct regex for an appDir page with a dynamic route', () => {
const regex = getEdgeFunctionPatternForPage({
pageRegexMap,
appPathRoutesManifest,
edgeFunctionDefinition: {
env: [],
files: [],
name: 'app/app-edge/[id]/page',
page: '/app-edge/[id]/page',
matchers: [
{
regexp: '^/app\\-edge/(?<id>[^/]+?)$',
},
],
wasm: [],
assets: [],
},
})
expect(regex).toBe('^/app\\-edge/([^/]+?)(?:/)?$')
expect('/app-edge/1').toMatch(new RegExp(regex))
expect('/app-edge/1/').toMatch(new RegExp(regex))
})

it('finds the correct regex for a pages edge route', () => {
const regex = getEdgeFunctionPatternForPage({
pageRegexMap,
appPathRoutesManifest,
edgeFunctionDefinition: {
env: [],
files: [],
name: 'pages/edge/[id]',
page: '/edge/[id]',
matchers: [
{
regexp: '^/edge/(?<id>[^/]+?)$',
},
],
wasm: [],
assets: [],
},
})
expect(regex).toBe('^/edge/(?<id>[^/]+?)/?$')
expect('/edge/1').toMatch(new RegExp(regex))
expect('/edge/1/').toMatch(new RegExp(regex))
})

it('finds the correct regex for a pages edge route with a v1 definition', () => {
const regex = getEdgeFunctionPatternForPage({
pageRegexMap,
appPathRoutesManifest,
edgeFunctionDefinition: {
env: [],
files: [],
name: 'pages/edge/[id]',
page: '/edge/[id]',
regexp: '^/edge/(?<id>[^/]+?)$',
wasm: [],
assets: [],
},
})
expect(regex).toBe('^/edge/(?<id>[^/]+?)/?$')
expect('/edge/1').toMatch(new RegExp(regex))
expect('/edge/1/').toMatch(new RegExp(regex))
})
})