My site has a few dynamic routes that result in thousands of hydrated pages. The pages follow a url structure like: /artists/[id]/[slug]
or /concerts/[year]/[month]/[day]/[slug]
. Currently, my page structure mimics those routes: /pages/artists/[id]/[slug].tsx
and /pages/concerts/[year]/[month]/[day]/[slug].tsx
.
The route with slug is preferred, but the site should gracefully redirect when the slug is not available. For example, I would like to have any request going to /artist/id
be redirected to /artist/id/slug
. And likewise, I'd like to do some redirecting for various parts of the concerts urls.
I originally tried specifying the redirects in next.config.js like:
module.exports = {
...
async redirects() {
const [allArtists, allConcerts] = await Promise.all([...]);
return [
...allArtists.map((artist) => {
return {
source: `/artists/${artist.id}`,
destination: `/artists/${artist.id}/${slugify(artist.name)}`,
permanent: false,
};
}),
...allConcerts.map((concert) => {
return {
source: `/concerts/${concert.year}`,
destination: `/tours/${concert.year}`,
permanent: false,
};
}),
...allConcerts.map((concert) => {
return {
source: `/concerts/${concert.year}/${concert.month}`,
destination: `/tours/${concert.year}`,
permanent: false,
};
}),
...allConcerts.map((concert) => {
return {
source: `/concerts/${concert.year}/${concert.month}/${concert.day}`,
destination: `/concerts/${concert.year}/${concert.month}/${concert.day}/${slugify(concert.name)}`,
permanent: false,
};
}),
],
},
};
However, as of today, vercel/next.js does not support more than 1,000 redirects specified in that file.
I tried to return redirect: pathWithSlug
from getStaticProps
, but get the following error:
Error: `redirect` can not be returned from getStaticProps during prerendering
Any ideas or approaches for how I can accomplish this behavior?