feat: expose event.route and event.url to remote functions - #14606
Conversation
🦋 Changeset detectedLatest commit: 1edecc4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Co-authored-by: Patrick <Patrick@ShowYou.us>
|
Should we add a note in the docs that you can not "trust" |
| <script> | ||
| import { get_event } from './data.remote.js'; | ||
|
|
||
| const event = await get_event(); |
There was a problem hiding this comment.
This is the line causing the lint failure:
packages/kit/test/apps/basics check: /home/runner/work/kit/kit/packages/kit/test/apps/basics/src/routes/remote/event/+page.svelte:4:16
packages/kit/test/apps/basics check: Error: 'await' expressions are only allowed within async functions and at the top levels of modules. (js)
There was a problem hiding this comment.
uhhhhh how do we fix that? does svelte-check need to be updated?
There was a problem hiding this comment.
(I also get red squigglies in my editor, which I assume is because the VS Code extension is attempting to load config from the project root, rather than the nearest svelte.config.js to the file in question. Is this a bug @dummdidumm?)
|
Ok, so it turns out enabling async rendering breaks a few tests, which we'll have to look into. Disabled it for now |
Yeah, just pushed something. I do almost wonder if we should just automatically re-run queries that depend on these values when they change, because I can see people doing that regardless |
|
I know there were a lot of discussions on this topic in the Remote Actions RFC, and I just wanted to express my gratitude for being open to feedback on this one. I love that I can now access parameters in remote functions and easily phase out load functions. 🥰 |
…ejs#14606) * feat: expose `event.route` and `event.url` to remote functions * Update packages/kit/src/runtime/server/respond.js Co-authored-by: Patrick <Patrick@ShowYou.us> * Update packages/kit/test/apps/basics/test/test.js * fix, hopefully * update docs * gah * fix * fix * fix --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Patrick <Patrick@ShowYou.us>
…ss inside queries (#16452) Closes #16416 with the error option which seems like the right approach to me. No hard feelings if it goes. - `event.url`, `event.params` and `event.route` now throw on access inside `query`, `query.batch` and `query.live`, during SSR as well, matching how prerendering already treats `url.search`. The router runs on the client-sent `x-sveltekit-pathname` header to produce `event.params` and `event.route.id`, so hiding only `url` would leave the same footgun readable through `params`. - The error message points at the migration, pass the value as a query argument. Reading trusted context from `locals` via `handle` still works, but since query requests no longer carry the page URL, `handle` sees the URL of the remote endpoint, so that context has to come from cookies or headers. - `command` and `form` are unchanged when called directly. Remote functions called while a query is running inherit the restriction, since their results feed the same cached response. - `handleValidationError` receives an event it can read `url` on, and prerendered data lookups inside queries resolve against the request URL rather than `event.url`. - Query clients stop sending the `x-sveltekit-pathname` and `x-sveltekit-search` headers, and the server only applies them when they are present, which also fixes the query half of #15749. - The test from #14606 asserting queries can read the url is inverted, and the remote-functions docs bullet now documents the error. --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] 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 - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. ### Tests - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check` ### Changesets - [x] 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 - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed. --------- Co-authored-by: Rich Harris <hello@rich-harris.dev>
We originally chose to omit
event.routeandevent.urlfrom remote functions, because we worried that it could create confusion — if a query's return value is based onevent.url, it could become incorrect later when the URL changes and the query result doesn't.But it's too restrictive. It's often necessary to know which page a remote function was called from. For example if a query redirects to a
/loginpage, it's nice to be able to include aredirectToparameter, and for that you need to know the page the user was trying to access:This PR makes that possible. Closes #14543.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits