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

Properly handle in-flight network errors on _data requests #6783

Merged
merged 2 commits into from Jul 11, 2023

Conversation

brophdawg11
Copy link
Contributor

If we make a ?_data fetch and it never actually reaches the Remix server, and instead we get a 4xx/5xx response back from a CDN/Proxy/Load Balancer, etc., then we currently incorrectly think it's a returned response from a loader/action since it's perfectly valid to:

export function loader() {
  return json({ isTeapot: true }, { status: 418 });
}

This PR adds an X-Remix-Response: yes header to successful responses for ?_data requests so we can differentiate responses that reached the Remix server from those that did not - and we can treat CDN/Proxy errors as real errors and surface them to the ErrorBoundary.

Closes #5418

@changeset-bot
Copy link

changeset-bot bot commented Jul 6, 2023

🦋 Changeset detected

Latest commit: 4321b36

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

This PR includes changesets to release 18 packages
Name Type
@remix-run/react Patch
@remix-run/server-runtime Patch
@remix-run/testing Patch
@remix-run/cloudflare Patch
@remix-run/deno Patch
@remix-run/dev Patch
@remix-run/node Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
create-remix Patch
@remix-run/architect Patch
@remix-run/express Patch
@remix-run/netlify Patch
@remix-run/serve Patch
@remix-run/vercel Patch
remix Patch
@remix-run/css-bundle Patch
@remix-run/eslint-config 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

init.headers = headers;
return new Response(body, init);
}

// Mark all successful responses with a header so we can identify in-flight
// network errors that are missing this header
response.headers.set("X-Remix-Response", "yes");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of having to remember to do this in each success code path,. should we instead do this higher up? I kept it here to colocate it with the setting of the error/catch headers but it feels potentially error-prone in the future:

function requestHandler() {
  ...
  if (url.searchParams.has("_data")) {
    response = await handleDataRequestRR(...);

    if (!response.headers.has('X-Remix-Error') && !response.headers.has('X-Remix-Catch')) {
      response.headers.set('X-Remix-Response', 'yes');
    }
    ...
  }
  ...
}

@brophdawg11
Copy link
Contributor Author

@jacob-ebey raised a good question around whether a CDN would cache this header or not.

I think it probably depends on the CDN - I would hope CDN's aren't caching 4xx/5xx responses. But technically, yeah if a if a CDN cached a user-returned 4xx/5xx response and did not cache the X-Remix-Response header along with it - then this change would surface that served cached response to the ErrorBoundary 😕

@brophdawg11
Copy link
Contributor Author

Spoke with @mjackson and we agreed that CDN's (1) should not generally be caching 4xx/5xx responses, but (2) if they are configured to - they should be caching the headers along with it, so we do not expect the above scenario to be an issue.

@brophdawg11 brophdawg11 merged commit 4e8e188 into dev Jul 11, 2023
9 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/handle-network-errors branch July 11, 2023 21:00
@github-actions github-actions bot added the awaiting release This issue has been fixed and will be released soon label Jul 11, 2023
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version v0.0.0-nightly-c8936ac-20230712 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.19.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.19.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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.

Unexpected HTTP errors on _data calls cause blank routes without any caught errors
2 participants