Giter Club home page Giter Club logo

next-redirect-examples's Introduction

next-redirect-examples's People

Contributors

nicosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

next-redirect-examples's Issues

Cookie OR header redirects via 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.