Skip to content

Commit 8bf61d9

Browse files
committed
fix: redirects to wrong route after SPA navigation
Since 'route' object can be different in a plugin and in a middleware (as plugin runs once and on further SPA navigations only middleware runs), we need to use 'route' object from middleware when calling setLocale() API. Save the route in internal property so that it's always available to the API. Before latest refactorings, plugin and middleware has handled it separately with duplicated code so it wasn't a problem.
1 parent d8db5b1 commit 8bf61d9

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/plugins/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export default async (context) => {
161161
await syncVuex(newLocale, app.i18n.getLocaleMessage(newLocale))
162162

163163
if (!initialSetup && STRATEGY !== STRATEGIES.NO_PREFIX) {
164+
const route = app.i18n.__route
164165
const routeName = route && route.name ? app.getRouteBaseName(route) : 'index'
165166

166167
redirect(app.localePath(Object.assign({}, route , {
@@ -181,6 +182,9 @@ export default async (context) => {
181182
app.i18n.getLocaleCookie = getLocaleCookie
182183
app.i18n.setLocale = (locale) => loadAndSetLocale(locale)
183184

185+
// Current route. Updated from middleware also.
186+
app.i18n.__route = route
187+
184188
// Extension of Vue
185189
if (!app.$t) {
186190
app.$t = app.i18n.t

src/templates/middleware.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ middleware['i18n'] = async (context) => {
3434
return
3535
}
3636

37+
// Update for setLocale to have up to date route
38+
app.i18n.__route = route
39+
3740
// Handle browser language detection
3841
const detectBrowserLanguage = <%= JSON.stringify(options.detectBrowserLanguage) %>
3942
const routeLocale = getLocaleFromRoute(route, routesNameSeparator, defaultLocaleRouteNameSuffix, locales)

0 commit comments

Comments
 (0)