'Redirects doesn't work on netlify with nextjs
I simply followed the doc by adding this to my next.config.js
module.exports = {
reactStrictMode: true,
async redirects() {
return [
{
source: "/",
destination: "/coming-soon",
permanent: false,
},
];
},
}
It does work on my machin even when I build the app but on netlify there is no redirect at all for some reason
Solution 1:[1]
According to the official Netlify docs:
Redirects and rewrites using
next.config.js
aren't currently supported for Next.js sites on Netlify. If you have these specified in anext.config.js
file for your project, check out our redirects and rewrites docs to learn how to set them up in a_redirects
ornetlify.toml
file instead.
So you basically need to create a _redirects
file at the top level of your project with the following contents:
/ /coming-soon 302
The 302 status code is equivalent to permanent: false
that you've done in your config.
If you have a netlify.toml
, then you can add something like this to make your stuff work:
[[redirects]]
from = "/"
to = "/coming-soon"
status = 302
force = false
References:
Solution 2:[2]
When we deploy a Next.js project on Netlify it automatically gets all the necessary dependencies including the Essential Next.js plugin (If it is not installed in the plugin tab, we have to install it manually). This plugin configures your Netlify site to allow essential Next.js capabilities. Version 4 of the Essential Next.js plugin adds support for native Next.js rewrites and redirects.
Basically, you can use native Next.js redirects/rewrites (configured in next.config.js) in Netlify by installing the Essential Next.js plugin version 4 or higher to your Next.js site deployed in Netlify.
Refer this to learn more about using Next.js and Netlify redirects/rewrites rules.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Osusara Kammalawatta |