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

docs(guide): Clarify how to handle 404s #3798

Merged
merged 1 commit into from
Jul 19, 2022

Conversation

runmoore
Copy link
Contributor

When discussing this topic with colleagues the wording 'you don't have to do anything' was interpreted in an ambiguous way.

This re-phrasing link's back to splat routes as a good option for handling 404s

@changeset-bot
Copy link

changeset-bot bot commented Jul 19, 2022

⚠️ No Changeset found

Latest commit: 8d48e74

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jul 19, 2022

Hi @runmoore,

Welcome, and thank you for contributing to Remix!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@runmoore runmoore force-pushed the clarify-404-documentation branch from fc301b4 to 69158ef Compare July 19, 2022 13:18
@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jul 19, 2022

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@machour machour added the docs label Jul 19, 2022
@machour machour changed the title Clarify documentation on how to handle 404s docs(guide): Clarify how to handle 404s Jul 19, 2022
When discussing this topic with colleagues the wording 'you don't have to do anything' was interpreted in an ambiguous way.

This re-phrasing linkis back to splat routes as a good option for handling 404s
@runmoore runmoore force-pushed the clarify-404-documentation branch from 69158ef to 8d48e74 Compare July 19, 2022 13:29
@tat-ss
Copy link

tat-ss commented Jul 19, 2022

To dear reviewer: I really keen to know if it is intentional to have 2 kind of 404 handlings (spat route and catch boundary) being used together to catch simple route mismatch and response throws from loader/action function. Don't you believe they should be unified into one? Your response to this would be very helpful.

@kiliman
Copy link
Collaborator

kiliman commented Jul 19, 2022

@tat-ss There are actually 3 ways to handle 404 responses.

Standard 404

  1. If you don't want to do anything but return 404 to the client, remove the <CatchBoundary> from root, and add this to handleRequest in entry.server
  if (responseStatusCode === 404) {
    return new Response("Not Found", { status: 404 });
  }

image

<CatchBoundary> for custom 404 page

  1. Add a <CatchBoundary> to root to render a custom 404 page.
export const CatchBoundary = () => {
  const caught = useCatch();
  const location = useLocation();
  return (
    <div>
      <h1>
        Catch {caught.status} {caught.statusText}
      </h1>
      <h2>Location: {`${location.pathname}${location.search}`}</h2>
    </div>
  );
};

image

Splat route for dynamic 404 handling

  1. Add a splat route. This gives you the ability to do something like check database if route has been moved and redirect.
// routes/$.tsx
export const loader: LoaderFunction = async ({ request }) => {
  const url = new URL(request.url);
  if (url.pathname === "/not-found") {
    return redirect("/found");
  }
  throw new Response("Not Found", { status: 404 });
};

image

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

I like this improved wording 👍

@kentcdodds kentcdodds merged commit 5536d40 into remix-run:main Jul 19, 2022
dvargas92495 pushed a commit to dvargas92495/remix that referenced this pull request Jul 22, 2022
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.

6 participants