Skip to content

Commit

Permalink
fix: handle hydration case for SSR { notFound: true }
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Dec 12, 2022
1 parent e6b7cde commit 9924328
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1224,7 +1224,7 @@ export default class Router implements BaseRouter {
// WARNING: `_h` is an internal option for handing Next.js client-side
// hydration. Your app should _never_ use this property. It may change at
// any time without notice.
const isQueryUpdating = (options as any)._h
const isQueryUpdating = (options as any)._h === 1
let shouldResolveHref =
isQueryUpdating ||
(options as any)._shouldResolveHref ||
Expand Down Expand Up @@ -1746,21 +1746,20 @@ export default class Router implements BaseRouter {

if (
isQueryUpdating &&
pathname === '/_error' &&
this.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
}

// shallow routing is only allowed for same page URL changes.
const isValidShallowRoute =
options.shallow && nextState.route === (routeInfo.route ?? route)

const shouldScroll =
options.scroll ?? (!(options as any)._h && !isValidShallowRoute)
options.scroll ?? (!isQueryUpdating && !isValidShallowRoute)
const resetScroll = shouldScroll ? { x: 0, y: 0 } : null
const upcomingScrollState = forcedScroll ?? resetScroll

Expand All @@ -1783,6 +1782,21 @@ export default class Router implements BaseRouter {
isQueryUpdating &&
(this.pathname === '/404' || this.pathname === '/_error')
) {
routeInfo = await this.getRouteInfo({
route: this.pathname,
pathname: this.pathname,
query,
as,
resolvedAs,
routeProps: { shallow: false },
locale: nextState.locale,
isPreview: nextState.isPreview,
})

if ('type' in routeInfo) {
throw new Error(`Unexpected middleware effect on ${this.pathname}`)
}

try {
await this.set(upcomingRouterState, routeInfo, upcomingScrollState)
} catch (err) {
Expand All @@ -1802,7 +1816,7 @@ export default class Router implements BaseRouter {
// need to scroll
// https://github.com/vercel/next.js/issues/37139
const canSkipUpdating =
(options as any)._h &&
isQueryUpdating &&
!upcomingScrollState &&
!readyStateChange &&
!localeChange &&
Expand Down Expand Up @@ -2058,8 +2072,13 @@ export default class Router implements BaseRouter {
})

if (isQueryUpdating) {
data = { json: self.__NEXT_DATA__.props }
if (!data) {
data = { json: self.__NEXT_DATA__.props }
} else {
data.json = self.__NEXT_DATA__.props
}
}

handleCancelled()

if (
Expand Down Expand Up @@ -2411,7 +2430,7 @@ export default class Router implements BaseRouter {

const data =
process.env.__NEXT_MIDDLEWARE_PREFETCH === 'strict'
? ({} as any)
? null
: await withMiddlewareEffects({
fetchData: () =>
fetchNextData({
Expand Down

0 comments on commit 9924328

Please sign in to comment.