Fix resolving dynamic routes when on returns a redirect (#38079)

* Fix resolving dynamic routes when on returns a redirect

* Update fix and add test case
This commit is contained in:
JJ Kasper 2022-06-27 18:47:32 -05:00 committed by GitHub
parent ca621ced66
commit 58033c4a3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 3 deletions

View file

@ -1815,7 +1815,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
}
}
return null
return false
}
private async renderToResponse(
@ -1831,7 +1831,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// route correctly and not loaded immediately without parsing params.
if (!isDynamicRoute(page)) {
const result = await this.renderPageComponent(ctx, bubbleNoFallback)
if (result) return result
if (result !== false) return result
}
if (this.dynamicRoutes) {
@ -1852,7 +1852,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
},
bubbleNoFallback
)
if (result) return result
if (result !== false) return result
}
}
} catch (error) {

View file

@ -138,6 +138,33 @@ describe('should set-up next', () => {
if (server) await killApp(server)
})
it('should resolve correctly when a redirect is returned', async () => {
const toRename = `standalone/.next/server/pages/route-resolving/[slug]/[project].html`
await next.renameFile(toRename, `${toRename}.bak`)
try {
const res = await fetchViaHTTP(
appPort,
'/route-resolving/import/first',
undefined,
{
redirect: 'manual',
headers: {
'x-matched-path': '/route-resolving/import/[slug]',
},
}
)
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
'/somewhere'
)
await waitFor(3000)
expect(stderr).not.toContain('ENOENT')
} finally {
await next.renameFile(`${toRename}.bak`, toRename)
}
})
it('should show invariant when an automatic static page is requested', async () => {
const toRename = `standalone/.next/server/pages/auto-static.html`
await next.renameFile(toRename, `${toRename}.bak`)

View file

@ -0,0 +1,3 @@
export default function Page(props) {
return <p>/[slug]/[project]</p>
}

View file

@ -0,0 +1,19 @@
export default function Page(props) {
return <p>/import/[slug]</p>
}
export function getStaticProps() {
return {
redirect: {
destination: '/somewhere',
permanent: false,
},
}
}
export function getStaticPaths() {
return {
paths: [],
fallback: true,
}
}