Skip to content

Comments

fix: properly track query instances inside and outside of reactive contexts#15353

Open
dummdidumm wants to merge 6 commits intomainfrom
query-weakref
Open

fix: properly track query instances inside and outside of reactive contexts#15353
dummdidumm wants to merge 6 commits intomainfrom
query-weakref

Conversation

@dummdidumm
Copy link
Member

This switches the approach we take to tracking the query/prerender instances across invocations. Instead of having a "are you in a reactive context"-counter, we use a WeakRef instead to keep a weak reference to the instance we create. This a) simplifies the code and b) allows people to create instances outside of reactive contexts, and have them share the same instance with other queries later invoked inside tracking contexts. Practically speaking this avoids surprises where your instance, created outside a reactive context, is not getting any updates from mutation refreshes. Fixes #14559

…ntexts

This switches the approach we take to tracking the query/prerender instances across invocations. Instead of having a "are you in a reactive context"-counter, we use a WeakRef instead to keep a weak reference to the instance we create. This a) simplifies the code and b) allows people to create instances outside of reactive contexts, and have them share the same instance with other queries later invoked inside tracking contexts. Practically speaking this avoids surprises where your instance, created outside a reactive context, is not getting any updates from mutation refreshes. Fixes #14559
@changeset-bot
Copy link

changeset-bot bot commented Feb 19, 2026

🦋 Changeset detected

Latest commit: 14428fb

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit 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

@dummdidumm dummdidumm marked this pull request as ready for review February 19, 2026 21:24
@svelte-docs-bot
Copy link

/**
* Use this to iterate the query_map. Will clean up dereferenced resources as a side effect.
*/
export function get_query_array() {

Choose a reason for hiding this comment

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

would it be worth having this be a generator function so that it doesn't have to populate an intermediate array? tradeoff being it's probably slower but with lower memory usage?

Copy link
Member Author

Choose a reason for hiding this comment

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

I doubt that it matters. I would guess we talk about a thousand entries at most

Comment on lines +59 to +64
// Delete the hydrated response from the cache at this point:
// If this instance is no longer referenced anywhere in the app,
// a new instance should not use the cached response, as it may be stale.
delete remote_responses[cache_key];

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Delete the hydrated response from the cache at this point:
// If this instance is no longer referenced anywhere in the app,
// a new instance should not use the cached response, as it may be stale.
delete remote_responses[cache_key];
// eslint-disable-next-line @typescript-eslint/no-floating-promises
// Delete the hydrated response from the cache after the resource has settled.
// We need to wait because prerender functions have an async initialization
// (`await prerender_cache_ready`) before they check `remote_responses`.
// If this instance is no longer referenced anywhere in the app,
// a new instance should not use the cached response, as it may be stale.
resource.finally(() => {
delete remote_responses[cache_key];
});

Prerender functions delete hydrated responses from remote_responses before they can be read, causing unnecessary network requests.

Fix on Vercel

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.

Remote Functions: Queries require explicit call in a method for Single Flight mutations to work in .svelte.ts/js files

3 participants