Skip to content

Commit

Permalink
Fix URL being updated with basePath for 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jun 30, 2020
1 parent cbb1c5c commit 40e4977
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/next/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ envConfig.setConfig({
publicRuntimeConfig: runtimeConfig || {},
})

const asPath = delBasePath(getURL())
let asPath = getURL()

// make sure not to attempt stripping basePath for 404s
if (
page !== '/404' &&
!(
page === '/_error' &&
hydrateProps &&
hydrateProps.pageProps.statusCode === '404'
)
) {
asPath = delBasePath(asPath)
}

const pageLoader = new PageLoader(buildId, prefix, page)
const register = ([r, f]) => pageLoader.registerPage(r, f)
Expand Down
3 changes: 2 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,11 @@ export default class Router implements BaseRouter {
if (as.substr(0, 2) !== '//') {
// in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization

this.changeState(
'replaceState',
formatWithValidation({ pathname: addBasePath(pathname), query }),
addBasePath(as)
getURL()
)
}

Expand Down
6 changes: 6 additions & 0 deletions test/integration/basepath/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ const runTests = (context, dev = false) => {
})
}

it('should not update URL for a 404', async () => {
const browser = await webdriver(context.appPort, '/missing')
const pathname = await browser.eval(() => window.location.pathname)
expect(pathname).toBe('/missing')
})

it('should update dynamic params after mount correctly', async () => {
const browser = await webdriver(context.appPort, '/docs/hello-dynamic')
const text = await browser.elementByCss('#slug').text()
Expand Down

0 comments on commit 40e4977

Please sign in to comment.