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(next-server): Fix priority for edge routes #39462

Merged
merged 14 commits into from Aug 16, 2022
19 changes: 4 additions & 15 deletions packages/next/server/next-server.ts
Expand Up @@ -1049,20 +1049,16 @@ export default class NextNodeServer extends BaseServer {
return manifest
}

/**
* Return a list of middleware routing items. This method exists to be later
* overridden by the development server in order to use a different source
* to get the list.
*/
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
/** Returns a middleware routing item. */
protected getMiddleware(): RoutingItem | undefined {
const manifest = this.getMiddlewareManifest()
const rootMiddleware = manifest?.middleware?.['/']
if (!rootMiddleware) {
const middleware = manifest?.middleware?.['/']
if (!middleware) {
return
}

return {
match: getMiddlewareMatcher(rootMiddleware),
match: getMiddlewareMatcher(middleware),
page: '/',
}
}
Expand All @@ -1079,13 +1075,6 @@ export default class NextNodeServer extends BaseServer {
}))
}

protected getEdgeRoutes(): RoutingItem[] {
const edgeFunctions = this.getEdgeFunctions()
const middleware = this.getMiddleware()

return edgeFunctions.concat(middleware ? [middleware] : [])
}

balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Get information for the edge function located in the provided page
* folder. If the edge function info can't be found it will throw
Expand Down
1 change: 0 additions & 1 deletion packages/next/server/router.ts
Expand Up @@ -242,7 +242,6 @@ export default class Router {
// disabled
...(this.useFileSystemPublicRoutes
? [
...(edgeSSRCatchAllRoute ? [edgeSSRCatchAllRoute] : []),
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
{
type: 'route',
name: 'page checker',
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/edge-vs.-non-edge-api-route-priority/index.test.ts
@@ -0,0 +1,33 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'

describe('Edge vs. non-Edge API route priority', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/api/user/login.js': `
export default async function handler(_, res) {
res.send('from login.js')
}
`,
'pages/api/user/[id].js': `
export const config = {
runtime: 'experimental-edge',
}
export default async function handler() {
return new Response('from [id].js')
}`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('more specific route should match', async () => {
const res = await fetchViaHTTP(next.url, '/api/user/login')
expect(await res.text()).toBe('from login.js')
})
})
1 change: 1 addition & 0 deletions test/lib/next-test-utils.js
Expand Up @@ -114,6 +114,7 @@ export function renderViaHTTP(appPort, pathname, query, opts) {
return fetchViaHTTP(appPort, pathname, query, opts).then((res) => res.text())
}

/** @return {Response} */
export function fetchViaHTTP(appPort, pathname, query, opts) {
const url = `${pathname}${
typeof query === 'string' ? query : query ? `?${qs.stringify(query)}` : ''
Expand Down