Skip to content

fix: ensure remote function queries update when using schema transfor…#15738

Closed
machadinhos wants to merge 4 commits into
sveltejs:mainfrom
machadinhos:fix-remote-function-query-not-updating
Closed

fix: ensure remote function queries update when using schema transfor…#15738
machadinhos wants to merge 4 commits into
sveltejs:mainfrom
machadinhos:fix-remote-function-query-not-updating

Conversation

@machadinhos

Copy link
Copy Markdown
Contributor

Closes #15696

If a remote function query has a validator that returns a value different from its original input, the generated cache key will use the validator’s output instead of the original input when refreshing the query. This can lead to queries not being refreshed correctly even when the same argument is passed.

I was not able to test these changes. I followed the CONTRIBUTING.md steps to add pnpm.overrides to the package.json of a test app but I got this error when trying to run it:

20:33:30 [vite] (ssr) Error when evaluating SSR module /@fs/D:/projects/kit/packages/kit/src/runtime/server/index.js: D:/projects/test/.svelte-kit/generated/root.svelte Invalid compiler option: runes should be true or false, if specified
https://svelte.dev/e/options_invalid_value
  Plugin: vite-plugin-svelte:compile
  File: D:/projects/test/.svelte-kit/generated/root.svelte

This is my first PR here so please be gentle :D

Thank you!


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.

…ms (#15696)

Closes #15696

`query` remote functions should use the original input to generate the
cache key instead of the output of the validate function.
@changeset-bot

changeset-bot Bot commented Apr 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bd5c924

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

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional Suggestion:

The set() method uses the validated argument instead of the raw argument for cache key generation, causing cache key mismatch when schemas transform input

Fix on Vercel

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great, thank you! For the test, add a new page in kit/test/apps/async/src/routes/remote/form and add a corresponding test in the related test/test.js. cc @elliott-with-the-longest-name-on-github for a second pair of eyes since you did the original work here

@machadinhos

Copy link
Copy Markdown
Contributor Author

couldn't check if the test was passing because when I ran the tests I only saw errors from other tests even if I introduced an error by hand.

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

Copy link
Copy Markdown
Contributor

Opened an alternative PR. This is actually a good PR, but while reviewing it I realized our approach is more fundamentally flawed in a way that patching like this can't fix: #15739

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

Copy link
Copy Markdown
Contributor

(also your commits are still included in the other PR, so you'll get credit for your work 😁)

@machadinhos

Copy link
Copy Markdown
Contributor Author

Thank you so much!
You guys are the best and I hope this PR was helpful anyway!

elliott-with-the-longest-name-on-github added a commit that referenced this pull request Apr 23, 2026
Alternative to #15738
Closes #15696

I realized while reviewing that there's a fundamental problem: If the
query's validator lossily changes the input, there's no way to map the
validated input back to the correct argument. Consider a validator that
calls `Math.floor` on its input number, then consider the following:

```ts
// from the client, we request `query(1.2)` and `query(1.8)`,
// but both `arg`s are `1`
for await (const arg of requested(my_query, 5)) {
  // there is no way to map `1` back to the original inputs;
  // only 1.8 would actually be refreshed because it wrote
  // to the `validated` cache last
  void my_query(arg).refresh();
}
```

This realization in hand, I further realized the only way to make this
work is to somehow preserve the mapping of input arguments to validated
arguments. The best way to do that is to return a bound instance of the
query from `requested`:

```ts
for await (const { arg, query } of requested(my_query, 5)) {
  void query.refresh();
}
```

Problem solved; the `query` internally maintains the map of input
argument => validated argument. The `arg` is still the validated
argument in case you need to do something like `if (arg.id === id)`.

---------

Co-authored-by: machadinhos <pbmachado2002@gmail.com>
Co-authored-by: Simon H <5968653+dummdidumm@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.

Remote function query does not update when using schema transforms

3 participants