Skip to content

Commit

Permalink
fix: Use asPath for dynamic client side routing (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaachinman authored Jul 28, 2019
1 parent a2ccb45 commit 0b314a6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions __tests__/hocs/app-with-translation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,23 @@ describe('appWithTranslation', () => {
it('will call router events in a browser context', async () => {
const { i18n } = await createApp()
process.browser = true
i18n.initializedLanguageOnce = true
await i18n.changeLanguage('en')
expect(mockRouterFn).toHaveBeenCalledTimes(1)
})

it('will not call router events in a server context', async () => {
const { i18n } = await createApp()
process.browser = false
i18n.initializedLanguageOnce = true
await i18n.changeLanguage('en')
expect(mockRouterFn).toHaveBeenCalledTimes(0)
})

it('will not call router events if initializedLanguageOnce is false', async () => {
const { i18n } = await createApp()
process.browser = true
i18n.initializedLanguageOnce = false
await i18n.changeLanguage('en')
expect(mockRouterFn).toHaveBeenCalledTimes(0)
})
Expand Down
4 changes: 2 additions & 2 deletions src/hocs/app-with-translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export default function (WrappedComponent) {
const { pathname, asPath, query } = router
const routeInfo = { pathname, query }

if (process.browser) {
if (process.browser && i18n.initializedLanguageOnce) {
if (config.localeSubpaths !== localeSubpathOptions.NONE) {
const { as, href } = lngPathCorrector(config, { as: asPath, href: routeInfo }, lng)
if (as !== asPath) {
router.replace(href, as)
}
} else {
router.replace({ pathname, query })
router.replace({ pathname, query }, asPath)
}
}
})
Expand Down

0 comments on commit 0b314a6

Please sign in to comment.