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

change: pass Scope into Resource::read() and Resource::with() #542

Merged
merged 8 commits into from
Feb 20, 2023

Conversation

gbj
Copy link
Collaborator

@gbj gbj commented Feb 18, 2023

Calling Resource::read() or Resource::with() crawls up the Scope tree, looking for the nearest <Suspense/> component, if any. This is used for streaming resource and suspense data from the server to the client during initial load, which is a significant optimization over waiting to start loading data until Wasm has loaded on the client.

Previously, a Scope was given to the Resource when it was created, and it carried this Scope around with it. Unfortunately, this meant that a Resource created in a "higher" scope couldn't be read correctly under a "lower" <Suspense/>. For example, if you created a resource in the root of your application, reading it under a <Suspense/> in one of your routes would not correctly register with the suspense, so would not properly stream.

This PR corrects that behavior for the hopefully-small cost of passing the omnipresent cx as the first argument to Resource::read() and Resource::with(). Now, you can create a single Resource somewhere in your app, pass it to multiple parts of the app via provide_context, and read it under multiple streaming <Suspense/> components while only actually loading the resource once.

@gbj gbj merged commit 4ff08f0 into main Feb 20, 2023
@gbj gbj deleted the resource-scopes branch February 20, 2023 00:52
gbj added a commit that referenced this pull request Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant