nicosh / next-redirect-examples Goto Github PK
View Code? Open in Web Editor NEWHow do redirects with next.js
How do redirects with next.js
I followed your link from the Stack Overflow answer to the next.config.js redirect solution.
We need to check for a cookie, and if it's not there, redirect based on browser header. We're using ISG so limited to next.config.js redirects serverside.
We've tried this solution and it does not work (we get infinite redirects as both cookie AND header match):
const { i18n } = require('./next-i18next.config');
const withTM = require('next-transpile-modules')(['fitty', 'react-svg']); // pass the modules you would like to see transpiled
const handleLocaleRedirects = path => {
const result = []
i18n.locales.forEach(locale => {
i18n.locales.forEach(loc => {
if (loc !== locale) {
result.push({
source: `/${locale}${path}`,
has: [{
type: 'header',
key: 'accept-language',
value: `^${loc}(.*)`
}],
permanent: false,
locale: false,
destination: `/${loc}${path}`
})
result.push({
source: `/${locale}${path}`,
has: [{
type: 'cookie',
key: 'NEXT_LOCALE',
value: loc
}],
permanent: true,
locale: false,
destination: `/${loc}${path}`
})
}
})
})
return result
}
module.exports = withTM({
i18n,
reactStrictMode: true,
images: {
domains: ['dxjnh2froe2ec.cloudfront.net', 'starsona-stb-usea1.s3.amazonaws.com']
},
eslint: {
// Warning: Dangerously allow production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
async redirects() {
return [...handleLocaleRedirects('/:celebrityId')]
}
})
Any help would be appreciated
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.