Skip to content

Commit

Permalink
wip: try middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentHardouin committed Jan 3, 2023
1 parent 4c4a278 commit 121e39a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
21 changes: 21 additions & 0 deletions middleware/locale-redirection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function (context) {
if (process.client) {
const { route, redirect } = context
if (route.path !== '/') {
return
}

const localeCookie = _getLocaleFromCookie()
return localeCookie
? redirect(`/${localeCookie}`)
: redirect('/locale-choice')
}
}

function _getLocaleFromCookie() {
const localeCookie = document.cookie
.split('; ')
.find((item) => item.startsWith('locale'))
if (!localeCookie) return null
return localeCookie.split('=')[1]
}
6 changes: 3 additions & 3 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ const i18nConfigurationForFrenchDomain = {
}

const i18nConfigurationForInternationalDomain = {
defaultLocale: 'fr',
defaultLocale: null,
strategy: 'prefix',
vueI18n: {
fallbackLocale: 'fr',
},
rootRedirect: config.isPixSite && !config.isFrenchDomain && 'locale-choice',
}

const nuxtConfig = {
Expand All @@ -28,6 +27,7 @@ const nuxtConfig = {
fallback: '404.html',
crawler: false,
},
ssr: false,
target: 'static',
publicRuntimeConfig: {
formKeysToMap: process.env.FORM_KEYS_TO_MAP || {},
Expand Down Expand Up @@ -179,7 +179,7 @@ const nuxtConfig = {
},

router: {
middleware: ['current-page-path'],
middleware: ['current-page-path', 'locale-redirection'],
linkExactActiveClass: 'current-active-link',
},

Expand Down
6 changes: 0 additions & 6 deletions servers.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,4 @@ server {
add_header Cache-Control public;
add_header ETag "";
}

location = / {
if ($cookie_locale ~ ^[a-zA-Z-]+$) {
return 302 /$cookie_locale/;
}
}
}

0 comments on commit 121e39a

Please sign in to comment.