Skip to content

Commit

Permalink
fix: redirects to wrong route after SPA navigation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rchl committed Aug 28, 2019
1 parent d8db5b1 commit 8bf61d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export default async (context) => {
await syncVuex(newLocale, app.i18n.getLocaleMessage(newLocale))

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

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

// Current route. Updated from middleware also.
app.i18n.__route = route

// Extension of Vue
if (!app.$t) {
app.$t = app.i18n.t
Expand Down
3 changes: 3 additions & 0 deletions src/templates/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ middleware['i18n'] = async (context) => {
return
}

// Update for setLocale to have up to date route
app.i18n.__route = route

// Handle browser language detection
const detectBrowserLanguage = <%= JSON.stringify(options.detectBrowserLanguage) %>
const routeLocale = getLocaleFromRoute(route, routesNameSeparator, defaultLocaleRouteNameSuffix, locales)
Expand Down

0 comments on commit 8bf61d9

Please sign in to comment.