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

Add hard navigation guard and fix middleware rewrite cases #37815

Merged
merged 13 commits into from
Jun 20, 2022
Merged
58 changes: 31 additions & 27 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let headManager: {
updateHead: (head: JSX.Element[]) => void
getIsSsr?: () => boolean
}
let initialMatchesMiddleware = false

let lastRenderReject: (() => void) | null
let webpackHMR: any
Expand All @@ -104,28 +105,30 @@ class Container extends React.Component<{
// - the page was (auto) exported and has a query string or search (hash)
// - it was auto exported and is a dynamic route (to provide params)
// - if it is a client-side skeleton (fallback render)
const handleQueryUpdate = (matchesMiddleware = false) => {
if (
router.isSsr &&
// We don't update for 404 requests as this can modify
// the asPath unexpectedly e.g. adding basePath when
// it wasn't originally present
initialData.page !== '/404' &&
initialData.page !== '/_error' &&
(initialData.isFallback ||
(initialData.nextExport &&
(isDynamicRoute(router.pathname) ||
location.search ||
process.env.__NEXT_HAS_REWRITES ||
matchesMiddleware)) ||
(initialData.props &&
initialData.props.__N_SSG &&
(location.search ||
process.env.__NEXT_HAS_REWRITES ||
matchesMiddleware)))
) {
// update query on mount for exported pages
router.replace(
// - if middleware matches the current page (may have rewrite params)
// - if rewrites in next.config.js match (may have rewrite params)
if (
router.isSsr &&
// We don't update for 404 requests as this can modify
// the asPath unexpectedly e.g. adding basePath when
// it wasn't originally present
initialData.page !== '/404' &&
initialData.page !== '/_error' &&
(initialData.isFallback ||
(initialData.nextExport &&
(isDynamicRoute(router.pathname) ||
location.search ||
process.env.__NEXT_HAS_REWRITES ||
initialMatchesMiddleware)) ||
(initialData.props &&
initialData.props.__N_SSG &&
(location.search ||
process.env.__NEXT_HAS_REWRITES ||
initialMatchesMiddleware)))
) {
// update query on mount for exported pages
router
.replace(
router.pathname +
'?' +
String(
Expand All @@ -145,14 +148,13 @@ class Container extends React.Component<{
// not shallow.
// Other pages (strictly updating query) happens shallowly, as data
// requirements would already be present.
shallow: !initialData.isFallback && !matchesMiddleware,
shallow: !initialData.isFallback && !initialMatchesMiddleware,
}
)
}
.catch((err) => {
if (!err.cancelled) throw err
})
}
router._initialMatchesMiddlewarePromise
.then((matchesMiddleware) => handleQueryUpdate(matchesMiddleware))
.catch(() => handleQueryUpdate())
}

componentDidUpdate() {
Expand Down Expand Up @@ -420,6 +422,8 @@ export async function hydrate(opts?: { beforeRender?: () => Promise<void> }) {
isRsc: initialData.rsc,
})

initialMatchesMiddleware = await router._initialMatchesMiddlewarePromise

const renderCtx: RenderRouteInfo = {
App: CachedApp,
initial: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/next/client/next-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ initialize({ webpackHMR })
router.asPath,
{ scroll: false }
)
.catch(() => {
// trigger hard reload when failing to refresh data
// to show error overlay properly
location.reload()
})
.finally(clearIndicator)
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/next/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export async function adapter(params: {

const isDataReq = params.request.headers['x-nextjs-data']

if (isDataReq && requestUrl.pathname === '/index') {
requestUrl.pathname = '/'
}

// clean-up any internal query params
for (const key of [...requestUrl.searchParams.keys()]) {
if (key.startsWith('__next')) {
Expand Down
Loading