Skip to content

Commit

Permalink
Ensure header matches deploy and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jun 15, 2022
1 parent e082835 commit 7201e08
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
23 changes: 16 additions & 7 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,13 +1238,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
'x-nextjs-matched-path',
`${query.__nextLocale ? `/${query.__nextLocale}` : ''}${pathname}`
)
// return empty JSON when not an SSG/SSP page and not an error
if (!(isSSG || hasServerProps)) {
res.setHeader('content-type', 'application/json')
res.body('{}')
res.send()
return null
}
}

// Don't delete query.__flight__ yet, it still needs to be used in renderToHTML later
Expand Down Expand Up @@ -1878,6 +1871,22 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
return response
}

if (
!!ctx.req.headers['x-nextjs-data'] &&
(!res.statusCode || res.statusCode === 200 || res.statusCode === 404)
) {
res.setHeader(
'x-nextjs-matched-path',
`${query.__nextLocale ? `/${query.__nextLocale}` : ''}${pathname}`
)
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.body('{}')
res.send()
return null
}

res.statusCode = 404
return this.renderErrorToResponse(ctx, null)
}
Expand Down
9 changes: 8 additions & 1 deletion packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,14 @@ function getMiddlewareData<T extends FetchDataOutput>(
]).then(([pages, { __rewrites: rewrites }]: any) => {
let as = parsedRewriteTarget.pathname

if (!rewriteHeader || isDynamicRoute(as)) {
if (
isDynamicRoute(as) ||
(!rewriteHeader &&
pages.includes(
normalizeLocalePath(removeBasePath(as), options.router.locales)
.pathname
))
) {
const parsedSource = getNextPathnameInfo(
parseRelativeUrl(source).pathname,
{ parseData: true }
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ describe('Middleware Runtime', () => {
expect(await browser.elementByCss('#as-path').text()).toBe('/rewrite-3')
})

it('should have correct route params for rewrite from config non-dynamic route', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1')
await browser.eval('window.next.router.push("/rewrite-1")')

await check(() => browser.elementByCss('body').text(), /Hello World/)

expect(await browser.eval('window.next.router.query')).toEqual({
from: 'config',
})
})

it('should redirect the same for direct visit and client-transition', async () => {
const res = await fetchViaHTTP(
next.url,
Expand Down

0 comments on commit 7201e08

Please sign in to comment.