0

I have a nuxt generated JAMStack website with i18n. Generated pages are located in en and ru folders. I'm calculating user's locale and serving content from the corresponding folder:

map $http_accept_language $fallback_lang {
    ~en en;
    ~ru ru
    default en;
}

map $cookie_i18n_redirected $lang {
    en en;
    ru ru;
    default $fallback_lang;
}

server {
    location / {
        root /var/www/html;
        rewrite ^/(.*)/$ /$1 permanent;
        try_files /$lang/$uri /$lang/$uri/index.html =404;
    }
}

Russian frontpage lies in /ru/index.html, but English version has no such page and I would like to serve /en/archive/index.html in response for GET / if $lang is 'en'.

How would I do that?

kyrsquir
  • 51
  • 6

1 Answers1

0

You might be able to leverage the built in extendRoutes in the nuxt.config.js.

In your nuxt.config.js file:

// nuxt.config.js
  ...

  router: {
    ...
    extendRoutes(routes, resolve) {
      routes.push({
        path: '/',
        component: resolve(__dirname, 'pages/en/archive/index.vue')
      });
    }
    ...
  },

That would allow you at the application level to route to a specfic nuxt page depending on the incoming route.

Tony M
  • 682
  • 7
  • 15
  • 1
    This only might work if wrapped inside a condition like `if (process.env.GENERATE_LOCALE === 'en') routes.push ...` to limit execution to generation of certain locale. I'm trying to keep my Nuxt app functional in spa/universal mode as well. So what I have currently seems to work better: `async beforeRouteEnter(to, from, next): { await next((vm) => if (vm.$i18n.locale === 'en') vm.$router.replace('/archive') }`. Nginx approach, on the other hand, could possibly work in all modes as I am reverse proxying everything through it. That's why I am looking for such a solution. – kyrsquir Mar 26 '20 at 21:17