Gatsby SSR: 301 redirects don't work in dev mode or on Gatsby Cloud Preview #35527
Description
Description
When returning a redirect in the getServeData
response, the redirect is not returned in the Gatsby dev mode, but it works when you do a gatsby build && gatsby serve
. We also noticed that the redirects don't work for Gatsby Cloud Preview environments (the builds for the pull requests). They work on the Gatsby Cloud production environment which is pushed to Gatsby Cloud Hosting though.
The code we use to handle redirects looks like this:
export const getServerData = async (): GetServerDataReturn => {
return {
status: 301,
headers: {
'Location': '/'
}
}
}
Reproduction Link
- Repository: feat: make 404 page use ssr satellytes/satellytes.com#615
- Gatsby Cloud Preview: https://satellytescommain21751-featmake404ssr.gtsb.io/office/ returns an 404 error instead of a redirect to
/about-us/
Steps to Reproduce
The issue can be tested on the pull request here: satellytes/satellytes.com#615
When you run yarn start
, the redirect doesn't work. An HTML page is returned instead. Also the follow up page-data.json
request returns the correct redirect, but this doesn't lead to a redirect in the browser:
When build on localhost with yarn build
and served with yarn serve
, the redirect works as expected:
Expected Result
Correct redirects to the URL provided in the Location
header.
Actual Result
No redirect at all with gatsby develop
or on Gatsby Cloud Preview.
Environment
gatsby develop
- Gatsby Cloud Preview
- Gatsby v4.14
- React 18
Config Flags
None