Unverified Commit dbac7552 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix resolve routes behavior when matching a dynamic segment (#54539)

When `fallback: false` is set and you visit a dynamic segment (e.g. `/[slug]`), the router server was getting stuck in a `x-no-fallback` loop and eventually would fail because it was matching the output at `check_fs` before attempting to resolve dynamic routes in the `check: true` block.

Closes NEXT-1557
parent 36b45c6f
...@@ -230,12 +230,12 @@ export async function makeResolver( ...@@ -230,12 +230,12 @@ export async function makeResolver(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse res: ServerResponse
): Promise<RouteResult | void> { ): Promise<RouteResult | void> {
const routeResult = await resolveRoutes( const routeResult = await resolveRoutes({
req, req,
new Set(), isUpgradeReq: false,
false, signal: signalFromNodeResponse(res),
signalFromNodeResponse(res) })
)
const { const {
matchedOutput, matchedOutput,
bodyStream, bodyStream,
......
...@@ -321,7 +321,7 @@ export async function initialize(opts: { ...@@ -321,7 +321,7 @@ export async function initialize(opts: {
// TODO: log socket errors? // TODO: log socket errors?
}) })
const matchedDynamicRoutes = new Set<string>() const invokedOutputs = new Set<string>()
async function invokeRender( async function invokeRender(
parsedUrl: NextUrlWithParsedQuery, parsedUrl: NextUrlWithParsedQuery,
...@@ -476,12 +476,12 @@ export async function initialize(opts: { ...@@ -476,12 +476,12 @@ export async function initialize(opts: {
resHeaders, resHeaders,
bodyStream, bodyStream,
matchedOutput, matchedOutput,
} = await resolveRoutes( } = await resolveRoutes({
req, req,
matchedDynamicRoutes, isUpgradeReq: false,
false, signal: signalFromNodeResponse(res),
signalFromNodeResponse(res) invokedOutputs,
) })
if (devInstance && matchedOutput?.type === 'devVirtualFsItem') { if (devInstance && matchedOutput?.type === 'devVirtualFsItem') {
const origUrl = req.url || '/' const origUrl = req.url || '/'
...@@ -658,6 +658,8 @@ export async function initialize(opts: { ...@@ -658,6 +658,8 @@ export async function initialize(opts: {
} }
if (matchedOutput) { if (matchedOutput) {
invokedOutputs.add(matchedOutput.itemPath)
return await invokeRender( return await invokeRender(
parsedUrl, parsedUrl,
matchedOutput.type === 'appFile' ? 'app' : 'pages', matchedOutput.type === 'appFile' ? 'app' : 'pages',
...@@ -753,12 +755,11 @@ export async function initialize(opts: { ...@@ -753,12 +755,11 @@ export async function initialize(opts: {
} }
} }
const { matchedOutput, parsedUrl } = await resolveRoutes( const { matchedOutput, parsedUrl } = await resolveRoutes({
req, req,
new Set(), isUpgradeReq: true,
true, signal: signalFromNodeResponse(socket),
signalFromNodeResponse(socket) })
)
// TODO: allow upgrade requests to pages/app paths? // TODO: allow upgrade requests to pages/app paths?
// this was not previously supported // this was not previously supported
......
...@@ -92,12 +92,17 @@ export function getResolveRoutes( ...@@ -92,12 +92,17 @@ export function getResolveRoutes(
...(opts.minimalMode ? [] : fsChecker.rewrites.fallback), ...(opts.minimalMode ? [] : fsChecker.rewrites.fallback),
] ]
async function resolveRoutes( async function resolveRoutes({
req: IncomingMessage, req,
matchedDynamicRoutes: Set<string>, isUpgradeReq,
isUpgradeReq: boolean, signal,
invokedOutputs,
}: {
req: IncomingMessage
isUpgradeReq: boolean
signal: AbortSignal signal: AbortSignal
): Promise<{ invokedOutputs?: Set<string>
}): Promise<{
finished: boolean finished: boolean
statusCode?: number statusCode?: number
bodyStream?: ReadableStream | null bodyStream?: ReadableStream | null
...@@ -223,18 +228,22 @@ export function getResolveRoutes( ...@@ -223,18 +228,22 @@ export function getResolveRoutes(
} }
async function checkTrue() { async function checkTrue() {
if (checkLocaleApi(parsedUrl.pathname || '')) { const pathname = parsedUrl.pathname || ''
if (checkLocaleApi(pathname)) {
return return
} }
const output = await fsChecker.getItem(parsedUrl.pathname || '') if (!invokedOutputs?.has(pathname)) {
const output = await fsChecker.getItem(pathname)
if (output) { if (output) {
if ( if (
config.useFileSystemPublicRoutes || config.useFileSystemPublicRoutes ||
didRewrite || didRewrite ||
(output.type !== 'appFile' && output.type !== 'pageFile') (output.type !== 'appFile' && output.type !== 'pageFile')
) { ) {
return output return output
}
} }
} }
const dynamicRoutes = fsChecker.getDynamicRoutes() const dynamicRoutes = fsChecker.getDynamicRoutes()
...@@ -249,12 +258,12 @@ export function getResolveRoutes( ...@@ -249,12 +258,12 @@ export function getResolveRoutes(
const localeResult = fsChecker.handleLocale(curPathname || '') const localeResult = fsChecker.handleLocale(curPathname || '')
for (const route of dynamicRoutes) { for (const route of dynamicRoutes) {
// when resolving fallback: false we attempt to // when resolving fallback: false the
// render worker may return a no-fallback response // render worker may return a no-fallback response
// which signals we need to continue resolving. // which signals we need to continue resolving.
// TODO: optimize this to collect static paths // TODO: optimize this to collect static paths
// to use at the routing layer // to use at the routing layer
if (matchedDynamicRoutes.has(route.page)) { if (invokedOutputs?.has(route.page)) {
continue continue
} }
const params = route.match(localeResult.pathname) const params = route.match(localeResult.pathname)
...@@ -275,7 +284,6 @@ export function getResolveRoutes( ...@@ -275,7 +284,6 @@ export function getResolveRoutes(
if (pageOutput && curPathname?.startsWith('/_next/data')) { if (pageOutput && curPathname?.startsWith('/_next/data')) {
parsedUrl.query.__nextDataReq = '1' parsedUrl.query.__nextDataReq = '1'
} }
matchedDynamicRoutes.add(route.page)
if (config.useFileSystemPublicRoutes || didRewrite) { if (config.useFileSystemPublicRoutes || didRewrite) {
return pageOutput return pageOutput
...@@ -381,17 +389,19 @@ export function getResolveRoutes( ...@@ -381,17 +389,19 @@ export function getResolveRoutes(
} }
if (route.name === 'check_fs') { if (route.name === 'check_fs') {
if (checkLocaleApi(parsedUrl.pathname || '')) { const pathname = parsedUrl.pathname || ''
if (invokedOutputs?.has(pathname) || checkLocaleApi(pathname)) {
return return
} }
const output = await fsChecker.getItem(parsedUrl.pathname || '') const output = await fsChecker.getItem(pathname)
if ( if (
output && output &&
!( !(
config.i18n && config.i18n &&
initialLocaleResult?.detectedLocale && initialLocaleResult?.detectedLocale &&
pathHasPrefix(parsedUrl.pathname || '', '/api') pathHasPrefix(pathname, '/api')
) )
) { ) {
if ( if (
......
...@@ -89,6 +89,21 @@ const runTests = () => { ...@@ -89,6 +89,21 @@ const runTests = () => {
slug: 'second', slug: 'second',
}) })
}) })
it('should behave properly when accessing the dynamic param directly', async () => {
const res = await fetchViaHTTP(appPort, '/[slug]', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#another').text()).toBe('another')
expect(JSON.parse($('#query').text())).toEqual({
path: ['[slug]'],
})
})
} }
describe('fallback: false rewrite', () => { describe('fallback: false rewrite', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment