Skip to content

Gatsby SSR: 301 redirects don't work in dev mode or on Gatsby Cloud Preview  #35527

Open
@feedm3

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

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:

Screenshot

When build on localhost with yarn build and served with yarn serve, the redirect works as expected:

Screenshot Screenshot 2022-04-29 at 14 21 25

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

Metadata

Assignees

No one assigned

    Labels

    status: confirmedIssue with steps to reproduce the bug that’s been verified by at least one reviewer.topic: coreRelates to Gatsby's core (e.g. page loading, reporter, state machine)type: bugAn issue or pull request relating to a bug in Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions