-
Hey there! I'm wondering what everyone's opinions are on how to properly generate page redirects that are defined outside of Next? For example, I have data coming from Sanity.io where a client would maintain a list of redirects (with My initial solution was to write these redirects to I feel this solution would be such a clean solution for setting these up, but with how Vercel deploys projects it seems unlikely this solution will work without a core update from the Vercel team. The goal is to avoid:
Thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 21 replies
-
I was just about to start testing this exact same approach, using a headless CMS on a separate domain from the site to manage redirect and then a pre-build step to update the vercel.json redirects via webhook and local script. We had questions about whether the vercel.json could even be modified (possibly not being allowed to write to fs at all) or if we could even manage to do it early enough in the process for vercel to pick up the new version rather than the repo version. Glad I found this before investing a bunch of time down this path. But we had another path to venture: Did you come up with a different/better solution? |
Beta Was this translation helpful? Give feedback.
-
@ArrayKnight since posting this there have been a number of updates to the Next platform and I was able to easily do this with the new way they handle redirects. You can read more about it in their docs, but I found it as simple as this for a Sanity powered site: // get redirects from Sanity for Vercel
async function fetchSanityRedirects() {
const data = await client.fetch(
`*[_type == "redirect"]{ from, to, isPermanent }`
)
const redirects = data.map((redirect) => {
return {
source: `/${redirect.from}`,
destination: `/${redirect.to}`,
permanent: redirect.isPermanent,
}
})
return redirects
}
module.exports = {
async redirects() {
const sanityRedirects = await fetchSanityRedirects()
return sanityRedirects
}
} |
Beta Was this translation helpful? Give feedback.
-
To be honest, this is such a bummer and kind of a dealbreaker for our company. Just a short headsup for everyone who stumbles over this issue just like me and is clueless. NextJS does only generate the redirects from next.config.js at build-time. Although it is an async function, I was under the impression, that this function would get called repeatedly in production, and was wondering how often that would happen. The answer is zero times. So you can make redirects kind of dynamic, as in load them from an external service, but you can not make them dynamic, as in they will update automatically in production. Triggering a new build for a new redirect per webhook is kind of a no-go for big sites who basically have hundreds or thousand of redirects. (don't ask me why, for a typo, for a rename of the url, for SEO purposes). This would lead to dozens of redundant and concurrent builds every week in my current project. What I really needed was an ISR-like method that gets called every XX seconds and repupulates the redirects. |
Beta Was this translation helpful? Give feedback.
-
Please check out our new docs here! https://nextjs.org/docs/app/building-your-application/routing/redirecting |
Beta Was this translation helpful? Give feedback.
Please check out our new docs here!
https://nextjs.org/docs/app/building-your-application/routing/redirecting