Skip to content

fix: allow query().current and other data properties to work in non-reactive contexts#15699

Merged
elliott-with-the-longest-name-on-github merged 6 commits intosveltejs:mainfrom
ottomated:current-non-reactive
Apr 17, 2026
Merged

fix: allow query().current and other data properties to work in non-reactive contexts#15699
elliott-with-the-longest-name-on-github merged 6 commits intosveltejs:mainfrom
ottomated:current-non-reactive

Conversation

@ottomated
Copy link
Copy Markdown
Contributor

As of #15533, query().current no longer works in non-reactive contexts. I believe this is unwanted behavior -- take the following scenario:

<script>
  import { list_todos } from '$lib/todos.remote';
  $effect(() => {
    const events = subscribe_to_something_external();
    events.on('new-todo', (item) => {
      // I don't want to call `.run()` here, because if `list_todos`
      // is not currently used, I don't care about the result
      // (it would just waste a network request)
      const current = list_todos().current;
      if (!current) return;
      list_todos().set([data.item, ...current]);
    });

  });
</script>

This PR fixes it by simply restoring the old behavior of .current returning undefined if the query has not been called yet.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 10, 2026

🦋 Changeset detected

Latest commit: e117e75

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

@svelte-docs-bot
Copy link
Copy Markdown

@elliott-with-the-longest-name-on-github
Copy link
Copy Markdown
Contributor

This is intentional. There's a pretty good explanation here: https://svelte.dev/docs/kit/remote-functions#query-Deduplication

Basically, because .current (and the other data properties) interact with data that's in the remote functions cache, they need an "anchor" in the reactive context (otherwise we have no way of managing the cached data). I think the correct solution here might just be to give .set a callback overload so that you can access the current value (if any), but only within the context of setting it:

query.set((current) => [new, ...current]);

Since set is already a noop if there's nothing listening for it, it'd achieve what you want. (It's also weird that, as written in this PR, you can access .current but none of the other data-related props.)

@ottomated
Copy link
Copy Markdown
Contributor Author

@elliott-with-the-longest-name-on-github I'm not sure I understand fully – is it not possible to just get the cached value if it exists and return undefined if it doesn't? This patch is working as expected for me, so is there a hidden bug I'm not seeing?

A callback form of .set would definitely also work, yes.

@elliott-with-the-longest-name-on-github
Copy link
Copy Markdown
Contributor

Ok we've discussed this at length and I think this is the right way to go, but if we're doing it with current we need to do it with error/loading/ready as well. Mind making that update? loading and ready should both fall back to false, for obvious reasons

@ottomated
Copy link
Copy Markdown
Contributor Author

I've added the other properties. I'll make a new PR for the callback form of set tomorrow.

@elliott-with-the-longest-name-on-github
Copy link
Copy Markdown
Contributor

@ottomated we can leave the new .set API off for now -- I think accessing the properties on uncached instances provides all of the benefits with additional flexibility. We can always add the .set API later if there's demand for it.

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github changed the title fix: allow query().current to work in non-reactive contexts fix: allow query().current and other data properties to work in non-reactive contexts Apr 16, 2026
@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github merged commit b77f00d into sveltejs:main Apr 17, 2026
26 checks passed
@ottomated ottomated deleted the current-non-reactive branch April 17, 2026 21:44
This was referenced Apr 17, 2026
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.

2 participants