Manager Redirects in Next.js

Load manager controlled redirects from zesty.io in next.js

Zesty provides a function, fetchZestyRedirection, as seen here https://github.com/zesty-io/nextjs-starter/blob/main/lib/zesty/fetchRedirects.js, load this function inside of next.config.js and place it as the return results next async redirects() function.

// next.config.js
const { fetchZestyRedirects } = require('./lib/zesty/fetchRedirects');

module.exports = {
  async redirects() {
    return await fetchZestyRedirects()
  },
  ...
}

If you wish to combine redirects from multiple sources, fetch the await fetchZestyRedirects() into a separate variable and join the array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

Example of joining zesty redirects to your customer redirects.

// next.config.js
const { fetchZestyRedirects } = require('./lib/zesty/fetchRedirects');

module.exports = {
  async redirects() {
    let myRedirects = [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ];
    const zestyRedirects = await fetchZestyRedirects();
    
    myRedirects.concat(zestyRedirects );
    return myRedirects
  },
  ...
}

Last updated