Skip to content

feat: expose event.route and event.url to remote functions - #14606

Merged
Rich-Harris merged 9 commits into
mainfrom
gh-14543
Oct 5, 2025
Merged

feat: expose event.route and event.url to remote functions#14606
Rich-Harris merged 9 commits into
mainfrom
gh-14543

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

We originally chose to omit event.route and event.url from remote functions, because we worried that it could create confusion — if a query's return value is based on event.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 /login page, it's nice to be able to include a redirectTo parameter, and for that you need to know the page the user was trying to access:

if (!user) {
  redirect(307, `/login?redirectTo=${encodeURIComponent(event.url.pathname)}`);
}

This PR makes that possible. Closes #14543.


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 Oct 5, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1edecc4

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 Minor

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

Comment thread packages/kit/src/runtime/server/respond.js Outdated
Co-authored-by: Patrick <Patrick@ShowYou.us>
Comment thread packages/kit/test/apps/basics/test/test.js Outdated
@PatrickG

PatrickG commented Oct 5, 2025

Copy link
Copy Markdown
Member

Should we add a note in the docs that you can not "trust" event.url/event.route if event.isRemoteRequest is true?
Otherwise I can see people trying to secure their remote functions in the handle hook with it.

<script>
import { get_event } from './data.remote.js';

const event = await get_event();

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 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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

uhhhhh how do we fix that? does svelte-check need to be updated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(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?)

@Rich-Harris

Copy link
Copy Markdown
Member Author

Ok, so it turns out enabling async rendering breaks a few tests, which we'll have to look into. Disabled it for now

@svelte-docs-bot

Copy link
Copy Markdown

@Rich-Harris

Copy link
Copy Markdown
Member Author

Should we add a note in the docs

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

@Rich-Harris
Rich-Harris merged commit 313ee5e into main Oct 5, 2025
22 checks passed
@Rich-Harris
Rich-Harris deleted the gh-14543 branch October 5, 2025 19:05
@github-actions github-actions Bot mentioned this pull request Oct 5, 2025
@tehnrd

tehnrd commented Oct 15, 2025

Copy link
Copy Markdown

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. 🥰

Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
…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>
Rich-Harris added a commit that referenced this pull request Jul 22, 2026
…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>
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.

Simultaneously isRemoteRequest: false and route.id: null in remote function during SSR

5 participants