Unverified Commit 5cf16ece authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure static 500 hydrates correctly with query (#22468)

Follow-up to https://github.com/vercel/next.js/pull/22139 this ensures the default 500 error page hydrates with the correct `statusCode` prop when updating query values on the client since currently it will switch the 404 `statusCode` when one isn't present. An additional test case has been added to ensure this is handled correctly. 
parent f8cd0758
......@@ -1110,6 +1110,18 @@ export default class Router implements BaseRouter {
// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute = options.shallow && this.route === route
if (
(options as any)._h &&
pathname === '/_error' &&
self.__NEXT_DATA__.props?.pageProps?.statusCode === 500 &&
props?.pageProps
) {
// ensure statusCode is still correct for static 500 page
// when updating query information
props.pageProps.statusCode = 500
}
await this.set(
route,
pathname!,
......
/* eslint-env jest */
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import { join } from 'path'
import {
killApp,
......@@ -12,6 +13,9 @@ import {
fetchViaHTTP,
waitFor,
getPageFileFromPagesManifest,
getPagesManifest,
updatePagesManifest,
check,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
......@@ -152,6 +156,36 @@ describe('500 Page Support', () => {
expect(
await fs.pathExists(join(appDir, '.next/server/pages/500.html'))
).toBe(true)
const pagesManifest = await getPagesManifest(appDir)
await updatePagesManifest(
appDir,
JSON.stringify({
...pagesManifest,
'/500': pagesManifest['/404'].replace('/404', '/500'),
})
)
// ensure static 500 hydrates correctly
const appPort = await findPort()
const app = await nextStart(appDir, appPort)
try {
const browser = await webdriver(appPort, '/err?hello=world')
const initialTitle = await browser.eval('document.title')
await check(async () => {
const query = await browser.eval(`window.next.router.query`)
return query.hello === 'world' ? 'success' : 'not yet'
}, 'success')
const currentTitle = await browser.eval('document.title')
expect(initialTitle).toBe(currentTitle)
expect(initialTitle).toBe('500: Internal Server Error')
} finally {
await killApp(app)
}
})
it('builds 500 statically by default with no pages/500 and custom _error', async () => {
......
......@@ -95,7 +95,7 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 63.9 kb
expect(parseFloat(indexFirstLoad)).toBeCloseTo(63.9, 1)
expect(parseFloat(indexFirstLoad)).toBeCloseTo(64, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
......@@ -168,7 +168,7 @@ describe('Build Output', () => {
expect(parseFloat(indexSize)).toBeGreaterThanOrEqual(2)
expect(indexSize.endsWith('kB')).toBe(true)
expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(66.7)
expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(66.8)
expect(parseFloat(indexFirstLoad)).toBeGreaterThanOrEqual(60)
expect(indexFirstLoad.endsWith('kB')).toBe(true)
})
......
import spawn from 'cross-spawn'
import express from 'express'
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
import { writeFile } from 'fs-extra'
import getPort from 'get-port'
import http from 'http'
// `next` here is the symlink in `test/node_modules/next` which points to the root directory.
......@@ -586,6 +587,18 @@ export function getPagesManifest(dir) {
return readJson(path.join(dir, '.next/serverless/pages-manifest.json'))
}
export function updatePagesManifest(dir, content) {
const serverFile = path.join(dir, '.next/server/pages-manifest.json')
if (existsSync(serverFile)) {
return writeFile(serverFile, content)
}
return writeFile(
path.join(dir, '.next/serverless/pages-manifest.json'),
content
)
}
export function getPageFileFromPagesManifest(dir, page) {
const pagesManifest = getPagesManifest(dir)
const pageFile = pagesManifest[page]
......
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