Skip to content

Commit

Permalink
Fix resolving dynamic routes when on returns a redirect (#38079)
Browse files Browse the repository at this point in the history
* Fix resolving dynamic routes when on returns a redirect

* Update fix and add test case
  • Loading branch information
ijjk committed Jun 27, 2022
1 parent ca621ce commit 58033c4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/next/server/base-server.ts
Expand Up @@ -1815,7 +1815,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
}
}
return null
return false
}

private async renderToResponse(
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions test/production/required-server-files.test.ts
Expand Up @@ -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`)
Expand Down
@@ -0,0 +1,3 @@
export default function Page(props) {
return <p>/[slug]/[project]</p>
}
@@ -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,
}
}

0 comments on commit 58033c4

Please sign in to comment.