0

I'm facing the 404 issue when trying to access any page of my Next.js application.

App works fine when no localeSubpaths config is provided.

After implementing localeSubpaths config (next.config.js):

const localeSubpaths = {
  en: "en",
  "de-DE": "de-DE",
  fr: "fr"
};

app is not working any more.

Every time I'm trying to access main root / or any of the locale pages fr, de-DE browser displays the 404 Error Page (Customized Next.js error page). This page contains some translated content which works fine.

Page contains also a link to the / Home Page. When I'm navigation to the Home Page by clicking on the link -> Home Page loads correctly (including related translations)

i18n.tsx config file:

import NextI18Next from "next-i18next";
import getConfig from "next/config";
import path from "path";

const { publicRuntimeConfig } = getConfig();
const { localeSubpaths } = publicRuntimeConfig;

const NextI18NextInstance = new NextI18Next({
  defaultLanguage: "en",
  ns: ["common"],
  defaultNS: "common",
  otherLanguages: ["de-DE", "de-CH", "fr"],
  strictMode: false,
  localeSubpaths,
  localePath: path.resolve("./public/static/locales")
});

export default NextI18NextInstance;

const {
  appWithTranslation,
  i18n,
  Link,
  withTranslation,
  Router
} = NextI18NextInstance;

export { appWithTranslation, i18n, Link, withTranslation, Router };
Piotr O
  • 427
  • 1
  • 5
  • 16

1 Answers1

0

Make sure you don't have defined any i18n entry into the next.config.js file, as this will overlap with the redirect entry.

next.config.js

const localeSubpaths = {
  en: 'en',
  es: 'es'
}
[...]
//remove any reference to this entry
//i18n: [...]
rewrites: async () => nextI18NextRewrites(localeSubpaths),
  publicRuntimeConfig: {
    localeSubpaths,
  } 

i18n.ts

import NextI18Next from 'next-i18next'
import path from 'path'
 
const localeSubpaths = {
  en: 'en',
  es: 'es'
}

const NextI18NextInstance = new NextI18Next({
  browserLanguageDetection: false,
  ns: ["common"],
  defaultNS: "common",
  fallbackLng: 'en',
  otherLanguages: ['es'],
  localeSubpaths: localeSubpaths,
  defaultLanguage: 'en',
  localePath: path.resolve('./public/locales'),
})

export const { appWithTranslation, useTranslation, withTranslation, Link, i18n} = NextI18NextInstance;
export default NextI18NextInstance;

makgyverzx
  • 39
  • 6