0

I have a lot of reference to my site I'm migrating to NextJS that start with capital letters like this:

https://svcc.mobi/Presenter/2019/llewellyn-falco-3133 (notice the capital P in Presenter)

I want them all to rewrite to

https://svcc.mobi/presenter/2019/llewellyn-falco-3133

I can update my next.config.js as below to take care of this for a single entry, but I want to have a few thousand of these entries in my next.config.js file. Can I somehow load this array programmatically at build time? (like getStaticPaths kind of).

module.exports = {
  async rewrites() {
    return [
      {
        source: '/(P|p)resenter/2019/llewellyn-falco-3133',
        destination: '/presenter/2019/llewellyn-falco-3133',
      },
      {
        source: '/(a|A)(b|B)(o|O)(u|U)(t|T)',
        destination: '/about',
      },
      {
        source: '/(s|S)ession',
        destination: '/session',
      },
      {
        source: '/(p|P)resenter',
        destination: '/presenter',
      },
    ];
  },
  images: {
    domains: ['ddrt7tzfkdwdf.cloudfront.net', 'www.siliconvalley-codecamp.com'],
  },
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188

1 Answers1

0

The rewrites entry in next.config.js accepts an async function, that must return an array of objects with the expected format. This means you can add any dynamic logic you want to it, and even execute asynchronous operations like fetching data.

module.exports = {
    async rewrites() {
        // Add logic to generate dynamic rewrites array:
        // Fetch data from external source, call functions, read files, etc

        return dynamicRewritesArray;
    }
};
juliomalves
  • 42,130
  • 20
  • 150
  • 146