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 into
sveltejs: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 into
sveltejs: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

changeset-bot Bot commented Apr 10, 2026

Copy link
Copy Markdown

🦋 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
@zhihengGet

zhihengGet commented Apr 19, 2026

Copy link
Copy Markdown

can we update doc to indicate this ? also does this mean i can do .run with cache outside of .svelte ?

it'd be helpful to allow run and use cache if it is there

// b.svelte.ts

import a from "a.remote"
async function cached(){
   let a = a()
  if(a.current is nil ) {  return a.run()}
  return a.current 

}

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

@zhihengGet please don't revive merged PRs, if you have an issue open an issue! We can discuss there

elliott-with-the-longest-name-on-github pushed a commit that referenced this pull request Apr 23, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @sveltejs/kit@2.58.0

### Minor Changes

- breaking: require `limit` in `requested` (as originally intended)
([#15739](#15739))


- feat: `RemoteQueryFunction` gains an optional third generic parameter
`Validated` (defaulting to `Input`) that represents the argument type
after schema validation/transformation
([#15739](#15739))


- breaking: `requested` now yields `{ arg, query }` entries instead of
the validated argument
([#15739](#15739))

### Patch Changes

- fix: allow `query().current`, `.error`, `.loading`, and `.ready` to
work in non-reactive contexts
([#15699](#15699))


- fix: prevent `deep_set` crash on nullish nested values
([#15600](#15600))


- fix: restore correct `RemoteFormFields` typing for nullable array
fields (e.g. when a schema uses `.default([])`), so `.as('checkbox')`
and friends work again
([#15723](#15723))


- fix: don't warn about removed SSI comments in `transformPageChunk`
([#15695](#15695))

Server-side include (SSI) directives like `<!--#include virtual="..."
-->` are HTML comments that are replaced by servers such as nginx.
Previously, removing them in `transformPageChunk` would trigger a false
positive warning about breaking Svelte's hydration. Since SSI comments
always start with `<!--#` and Svelte's hydration comments never do, they
can be safely excluded from the check.

- Change enhance function return type from void to MaybePromise<void>.
([#15710](#15710))


- fix: throw an error when `resolve` is called with an external URL
([#15733](#15733))


- fix: avoid FOUC for CSR-only pages by loading styles and fonts before
CSR starts ([#15718](#15718))


- fix: reset form result on redirect
([#15724](#15724))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

3 participants