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

Add future flag for Single Fetch #8773

Merged
merged 62 commits into from
Mar 12, 2024
Merged

Add future flag for Single Fetch #8773

merged 62 commits into from
Mar 12, 2024

Conversation

brophdawg11
Copy link
Contributor

@brophdawg11 brophdawg11 commented Feb 16, 2024

Closes: #7641
RFC: #7640

Todo:

  • Docs
  • Tests
    • Most of the integration tests pass as-is if we just enable to flag
    • Need to figure out a good way to enable tests for this while keeping the current tests enabled without this flag
    • Probably best to pick a handful of the most important "data sensitive" integration suites (loader-test, catch-boundary-test, error-boundary-test, form-test, etc.) and copy/paste all the tests so they run a second time with the flag
    • That gives us regression coverage of both approaches
    • Then when we prep to release v3 we just delete the original set of tests
  • Changeset

Copy link

changeset-bot bot commented Feb 16, 2024

🦋 Changeset detected

Latest commit: 8d4c012

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

This PR includes changesets to release 16 packages
Name Type
@remix-run/dev Minor
@remix-run/react Minor
@remix-run/server-runtime Minor
@remix-run/testing Minor
@remix-run/cloudflare Minor
@remix-run/deno Minor
@remix-run/node Minor
@remix-run/cloudflare-pages Minor
@remix-run/cloudflare-workers Minor
@remix-run/architect Minor
@remix-run/express Minor
@remix-run/serve Minor
create-remix Minor
remix Minor
@remix-run/css-bundle Minor
@remix-run/eslint-config Minor

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 276 to 277
v7_skipActionErrorRevalidation:
window.__remixContext.future.unstable_singleFetch === true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Single fetch opts into this behavior automatically

if (!route.hasLoader) return null;
return fetchServerHandler(request, route);
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 always hitting the server here, if single fetch is enabled we will have passed the singleFetch method down from dataStrategy so use that to ensure we make the right HTTP call to the server

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By proxying this all the way though and branching here as deep as possible - we avoid having to re-implement any of the logic around critical/lazy routes, revalidation, client data etc. in data
strategy and get to leverage all of the existing battle-tested flows.

Comment on lines 32 to 37
singleFetch = async (routeId) => {
let url = singleFetchUrl(request.url);
let init = await createRequestInit(request);
let result = await fetchAndDecode(url, init);
return unwrapSingleFetchResult(result as SingleFetchResult, routeId);
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Action implementation.

Below is the more complex loader implementation which lets all loaders latch onto a single promise for the call.

// `serverLoader` and we never read the response of that route from the
// single fetch call, and thus executing that `loader` on the server was
// unnecessary.
function addRevalidationParam(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method handles our fine-grained revalidation query param. This should only show up on .data requests if the app is has implemented shouldRevalidate on one or more of the matches. Otherwise, we don't include a query param and while we may run a few extra loaders, the cacheability of the request should improve greatly resulting in a net-positive impact on server load.

Comment on lines 154 to 156
let url = new URL(reqUrl);
url.pathname = `${url.pathname === "/" ? "_root" : url.pathname}.data`;
return url;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

single fetch URLs are /path/to/whatever.data, except for the root route. To avoid a super odd-looking (and potentially invalid?) URL like /.data, we use /_root.data

Copy link
Member

Choose a reason for hiding this comment

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

.data? I imagine it's not .json because with RSC the response can be more than just JSON?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even now it's not JSON - it's the streaming format used by turbo-stream - so .data is just a generic extension agnostic of the underlying streaming mechanism - which will eventually be RSC

@brophdawg11 brophdawg11 merged commit 9989381 into dev Mar 12, 2024
9 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/single-fetch branch March 12, 2024 18:19
@github-actions github-actions bot added the awaiting release This issue has been fixed and will be released soon label Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants