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

Feature/hydrate promises #7481

Merged
merged 27 commits into from
May 27, 2024
Merged

Feature/hydrate promises #7481

merged 27 commits into from
May 27, 2024

Conversation

TkDodo
Copy link
Collaborator

@TkDodo TkDodo commented May 26, 2024

No description provided.

Copy link

vercel bot commented May 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview May 27, 2024 5:59pm

Copy link

nx-cloud bot commented May 26, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 70f7f71. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link

codesandbox-ci bot commented May 26, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 70f7f71:

Sandbox Source
@tanstack/query-example-angular-basic Configuration
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@github-actions github-actions bot added the documentation Improvements or additions to documentation label May 26, 2024
@TkDodo TkDodo marked this pull request as ready for review May 26, 2024 18:42
@@ -65,6 +66,7 @@ function dehydrateQuery(query: Query): DehydratedQuery {
state: query.state,
queryKey: query.queryKey,
queryHash: query.queryHash,
...(query.state.status === 'pending' && { promise: query.promise }),
Copy link
Contributor

@phryneas phryneas May 26, 2024

Choose a reason for hiding this comment

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

I'd recommend to "hide" the error here to prevent leaking server secrets.

Suggested change
...(query.state.status === 'pending' && { promise: query.promise }),
...(query.state.status === 'pending' && { promise: query.promise.catch(() => { throw new Error("redacted") }) }),

You might also want to consider different behaviour on rehydration of errors, depending of environment. I'd probably throw an error in SSR so the suspense boundary would just retry in the browser - and in the browser, I wouldn't throw at all, but immediately retry the query if it failed in RSC.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks. How does this look?

03676dd

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me :)

Copy link

Choose a reason for hiding this comment

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

I think in Next.js (or maybe React does that), the error message and stack are already hidden when crossing the RSC boundary. This is also the case for Server Actions.

However, there will be a special digest field attached to the error which is a random ID. So developers can log the actual error on the server and use that ID from client logs to match it, without leaking anything.

https://nextjs.org/docs/app/building-your-application/routing/error-handling#securing-sensitive-error-information

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if react does this, it means we could remove this error handling. if it's next specific, I don't think we can rely on it because other frameworks like waku might implement this differently.

So can we figure out if this is next or react who does this ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought that was only for errors thrown during render, not for transported values, but I might be wrong - double-checking might definitely be a good idea here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Either way, this part is in the query core, so might make sense to redact it by default if entirely other frameworks handle it differently? Would be nice to support the digest too though. 🤔

Easier to go from redacted -> more details in the future than the other way around so I think current version is fine to ship.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

true, I haven't even thought about other frameworks 👍

Copy link

Choose a reason for hiding this comment

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

@TkDodo @Ephem I confirmed that it's a React behavior (which makes sense, as it's done on the RSC encoding/decoding layer that frameworks can't inject into): https://github.com/facebook/react/blob/ea6e05912aa43a0bbfbee381752caa1817a41a86/packages/react-client/src/ReactFlightClient.js#L1477-L1484

If we redact it here, the client code (e.g. error boundaries, alert boxes, trackers) can't receive that digest value and get it displayed/logged.

@TkDodo TkDodo requested a review from Ephem May 27, 2024 08:24
Copy link
Collaborator

@Ephem Ephem left a comment

Choose a reason for hiding this comment

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

I think this looks great! I left mostly NITs.

The one thing I'm a bit unsure of, but don't know if it affects this PR, is exactly under which circumstances NextJs makes the shell static. In the current example, I think the shell is supposed to be static with or without await because the fetch is cached by default?

When I add { cache: "no-store" } to that fetch, it turns the page dynamic both with and without await. This seems to happen in the swr example too though, so not sure what the expected behaviour is.

docs/framework/react/guides/advanced-ssr.md Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Outdated Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Outdated Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Outdated Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Outdated Show resolved Hide resolved
docs/framework/react/guides/advanced-ssr.md Outdated Show resolved Hide resolved
packages/query-core/src/hydration.ts Outdated Show resolved Hide resolved
@@ -1,4 +1,5 @@
import { describe, expect, test, vi } from 'vitest'
import { waitFor } from '@testing-library/react'

Choose a reason for hiding this comment

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

Vitest supports vi.waitFor 👀

@TkDodo TkDodo merged commit 721730a into main May 27, 2024
6 checks passed
@TkDodo TkDodo deleted the feature/hydrate-promises branch May 27, 2024 18:13
@TmLev
Copy link

TmLev commented May 27, 2024

This is awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation package: query-core package: react-query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants