Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initial redirect breaks reactivity in static mode
When we've detected browser language and need to redirect we need to do it from the middleware for the page to transition correctly. The problem is Nuxt doesn't trigger middleware for initial navigation in static mode (the idea is that it already ran when generating page on the server), so we can't do it properly. Fixed by hooking into the VueRouter "beforeEach" navigation hook so that we can handle it from the right place. Still, as Nuxt doesn't know that we are redirecting, it would throw some errors trying to load previous page. So use "location.assign()" to "hard" redirect. Resolves #737
- Loading branch information
Showing
5 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import middleware from '../middleware' | ||
|
||
/** @type {import('@nuxt/types').Middleware} */ | ||
middleware.nuxti18n = async (context) => { | ||
const { app, isHMR } = context | ||
|
||
if (isHMR) { | ||
return | ||
} | ||
|
||
await app.i18n.__onNavigate() | ||
const [status, redirectPath] = await app.i18n.__onNavigate(context.route) | ||
if (status && redirectPath) { | ||
context.redirect(status, redirectPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters