2

I'm currently working in a multi-language application with Next.js that I intend to host in an S3 bucket. In this site, I need to maintain an application state across routes, by doing client-side routing from one route to another.

Since I need to support 40+ languages and internationalization, the process to do so is the following:

  • In next.config.js, use exportPathMap to generate /[language] routes with a variable inside "query" that contains that particular locale for the language.

  • Load this locale in getInitialProps of _app and pass it down with a Provider, to be consumed in any part of the application using the context API.

  • To support client-side routing, I've wrapped the next/link component in a custom Link that passes down all props and sets the "as" prop to "/[language]/[route]".

This solution works for now, but ideally, I wouldn't need to "mock" the routing with the "as" prop. I have found that dynamic routing in next does not allow client-side routing in a way to avoid refreshing the page to a new .html file that matches the dynamic path. E.g: Using the following directory structure:

/pages
   index.tsx
   /[lang]/
       home.tsx
       dashboard.tsx

From index.tsx, clicking on a link from next/link that redirects to /en/dashboard will trigger a request to the server and refresh the page completely. Thus, losing the client state.

Is there a better solution for this? It seems like a very common problem to solve, yet I cannot find an elegant solution using Next.js.

0 Answers0