Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: status code for 404 props queries to avoid client side navigation with empty props #60968

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export async function initialize(opts: {
removePathPrefix(invokePath, config.basePath) === '/404'
) {
res.setHeader('x-nextjs-matched-path', parsedUrl.pathname || '')
res.statusCode = 200
res.statusCode = 404
res.setHeader('content-type', 'application/json')
res.end('{}')
return null
Expand Down
33 changes: 10 additions & 23 deletions packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,30 +323,17 @@ async function withMiddlewareEffects<T extends FetchDataOutput>(
return null
}

try {
const data = await options.fetchData()
const data = await options.fetchData()

const effect = await getMiddlewareData(
data.dataHref,
data.response,
options
)
const effect = await getMiddlewareData(data.dataHref, data.response, options)

return {
dataHref: data.dataHref,
json: data.json,
response: data.response,
text: data.text,
cacheKey: data.cacheKey,
effect,
}
} catch {
/**
* TODO: Revisit this in the future.
* For now we will not consider middleware data errors to be fatal.
* maybe we should revisit in the future.
*/
return null
return {
dataHref: data.dataHref,
json: data.json,
response: data.response,
text: data.text,
cacheKey: data.cacheKey,
effect,
}
}

Expand Down Expand Up @@ -2403,7 +2390,7 @@ export default class Router implements BaseRouter {
locale,
}),
hasMiddleware: true,
isServerRender: this.isSsr,
isServerRender: false,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !this.isPreview,
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ describe('Middleware Runtime', () => {
function runTests({ i18n }: { i18n?: boolean }) {
it('should work with notFound: true correctly', async () => {
const browser = await next.browser('/ssr-page')
await browser.eval('window.beforeNav = 1')
await browser.eval('window.next.router.push("/ssg/not-found-1")')

await check(
() => browser.eval('document.documentElement.innerHTML'),
/This page could not be found/
)
expect(await browser.eval('window.beforeNav')).toBe(1)

await browser.refresh()
await check(
Expand Down