Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly handle redirects in prerendered pages #13365

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

brophdawg11
Copy link
Contributor

No description provided.

Copy link

changeset-bot bot commented Apr 4, 2025

🦋 Changeset detected

Latest commit: f163920

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@react-router/dev Patch
react-router Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
@react-router/architect Patch
@react-router/cloudflare Patch
react-router-dom Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch
create-react-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +2802 to +2823
if (redirectStatusCodes.has(response.status)) {
// This isn't ideal but gets the job done as a fallback if the user can't
// implement proper redirects via .htaccess or something else. This is the
// approach used by Astro as well so there's some precedent.
// https://github.com/withastro/roadmap/issues/466
// https://github.com/withastro/astro/blob/main/packages/astro/src/core/routing/3xx.ts
let location = response.headers.get("Location");
// A short delay causes Google to interpret the redirect as temporary.
// https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
let delay = response.status === 302 ? 2 : 0;
html = `<!doctype html>
<head>
<title>Redirecting to: ${location}</title>
<meta http-equiv="refresh" content="${delay};url=${location}">
<meta name="robots" content="noindex">
</head>
<body>
<a href="${location}">
Redirecting from <code>${normalizedPath}</code> to <code>${location}</code>
</a>
</body>
</html>`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a prerendered document redirects we fall back on an http-equiv redirect

@PetarMetodiev
Copy link

Heya, in an app started from the Cloudflare template I have an action for / which returns redirect, something like this:

export const action = async ({ request }: Route.ActionArgs) => {
  const formData = await request.formData();

  if (formData.get('param')) {
    return redirect(href('/some-page'));
  }

  return null;
};

In react-router.config.ts I have prerender: ['/']. This works fine on local but I get 405 when deployed on Cloudflare.

Does this PR fix this exact behavior, or is this something else?

If this is not the right place to ask, can you please point me in the right direction?

Thank you!

@MichaelDeBoey MichaelDeBoey changed the title Properly handle redirects in prerendered pages fix: properly handle redirects in prerendered pages Apr 7, 2025
@brophdawg11
Copy link
Contributor Author

@PetarMetodiev action is not be involved in prerendering, only loader - so I think you have a different issue. Could you please open a new issue with a minimal reproduction?

@brophdawg11 brophdawg11 merged commit c923ded into dev Apr 7, 2025
9 of 11 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/prerender-redirects branch April 7, 2025 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants