In some of my pages I'm using getServerSideProps to make API calls and redirect the user if necessary. The web APP I'm building is multilingual and I need to always show the user the correct language.
The homepage / uses getServerSideProps and redirects the user either to the profile or the login page. For that I do the following:
return {
redirect: {
permanent: false,
destination: someLogic ? `${context.locale}/login` : `${context.locale}/profile`,
},
};
now the profile and user pages use getServerSideProps too, to check if there's a valid session and redirect the user if necessary. For instance the user will try access the profile page when he's session is expired, he will then be redirected to the login page. if I set the destination property to /login the locale property will be overriden and the user will get the default language and be redirected to domain/login. If I set it to ${context.locale}/login and the page that was originally called is domain/fr-FR/profile then the user will be redirected to domain/fr-FR/fr-FR/login
Client side redirecting with router.push or router.replace is working fine and returning the correct urls.
To my understanding I can't get the absolute URL from the context of the getServerSideProps to check if the locale is already set, how can I then solve this problem?
I'm currently using next 10.0.4 and this is my next.config.js :
module.exports = {
i18n: {
locales: ['de-DE', 'en-US', 'fr-FR', 'nl-NL', 'it-IT'],
defaultLocale: 'en-US',
localDetection: true,
}
}