From a050e59a81ffbf88327f22a083919f4f6a16f16d Mon Sep 17 00:00:00 2001 From: "svelte-docs-bot[bot]" <196124396+svelte-docs-bot[bot]@users.noreply.github.com> Date: Thu, 13 Aug 2026 00:26:05 +0000 Subject: [PATCH] sync Kit next docs --- .../docs/kit/20-core-concepts/10-routing.md | 2 +- .../docs/kit/20-core-concepts/20-load.md | 2 +- .../20-core-concepts/60-remote-functions.md | 2 +- .../25-build-and-deploy/40-adapter-node.md | 2 +- .../docs/kit/40-best-practices/03-auth.md | 2 +- .../kit/40-best-practices/10-accessibility.md | 2 +- .../35-migrating-to-sveltekit-3.md | 6 +- .../docs/kit/98-reference/10-@sveltejs-kit.md | 451 +++++++++++++++++- .../docs/kit/98-reference/20-$app-server.md | 449 ----------------- 9 files changed, 457 insertions(+), 461 deletions(-) diff --git a/apps/svelte.dev/content/docs/kit/20-core-concepts/10-routing.md b/apps/svelte.dev/content/docs/kit/20-core-concepts/10-routing.md index 123c0075d0..f20d07d9af 100644 --- a/apps/svelte.dev/content/docs/kit/20-core-concepts/10-routing.md +++ b/apps/svelte.dev/content/docs/kit/20-core-concepts/10-routing.md @@ -294,7 +294,7 @@ Like `+layout.js`, `+layout.server.js` can export [page options](page-options) ## +server -As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, `OPTIONS`, and `HEAD` that take a [`RequestEvent`]($app-server#RequestEvent) argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. +As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, `OPTIONS`, and `HEAD` that take a [`RequestEvent`](@sveltejs-kit#RequestEvent) argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. For example we could create an `/api/random-number` route with a `GET` handler: diff --git a/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md b/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md index dff6d45c8d..9f02a3b11e 100644 --- a/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md +++ b/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md @@ -293,7 +293,7 @@ export async function load({ fetch, params }) { ## Cookies -A server `load` function can get [`cookies`]($app-server#Cookies) as shown below. When setting cookies, SvelteKit provides default values for `httpOnly`, `secure`, and `path` — as described in [the API documentation]($app-server#Cookies) — in order to improve security and developer experience. +A server `load` function can get [`cookies`](@sveltejs-kit#Cookies) as shown below. When setting cookies, SvelteKit provides default values for `httpOnly`, `secure`, and `path` — as described in [the API documentation](@sveltejs-kit#Cookies) — in order to improve security and developer experience. ```js /// file: src/routes/+layout.server.js diff --git a/apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md b/apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md index 687c11d316..75d019c5cb 100644 --- a/apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md +++ b/apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md @@ -1268,7 +1268,7 @@ export const getStuff = query('unchecked', async ({ id }: { id: string }) => { ## Using `getRequestEvent` -Inside `query`, `form` and `command` you can use [`getRequestEvent`]($app-server#getRequestEvent) to get the current [`RequestEvent`]($app-server#RequestEvent) object. This makes it easy to build abstractions for interacting with cookies, for example: +Inside `query`, `form` and `command` you can use [`getRequestEvent`]($app-server#getRequestEvent) to get the current [`RequestEvent`](@sveltejs-kit#RequestEvent) object. This makes it easy to build abstractions for interacting with cookies, for example: ```ts /// file: user.remote.js diff --git a/apps/svelte.dev/content/docs/kit/25-build-and-deploy/40-adapter-node.md b/apps/svelte.dev/content/docs/kit/25-build-and-deploy/40-adapter-node.md index 55f262c694..69192ef348 100644 --- a/apps/svelte.dev/content/docs/kit/25-build-and-deploy/40-adapter-node.md +++ b/apps/svelte.dev/content/docs/kit/25-build-and-deploy/40-adapter-node.md @@ -122,7 +122,7 @@ If `adapter-node` can't correctly determine the URL of your deployment, you may ### `ADDRESS_HEADER` and `XFF_DEPTH` -The [`RequestEvent`]($app-server#RequestEvent) object passed to hooks and endpoints includes an `event.getClientAddress()` function that returns the client's IP address. By default this is the connecting `remoteAddress`. If your server is behind one or more proxies (such as a load balancer), this value will contain the innermost proxy's IP address rather than the client's, so we need to specify an `ADDRESS_HEADER` to read the address from: +The [`RequestEvent`](@sveltejs-kit#RequestEvent) object passed to hooks and endpoints includes an `event.getClientAddress()` function that returns the client's IP address. By default this is the connecting `remoteAddress`. If your server is behind one or more proxies (such as a load balancer), this value will contain the innermost proxy's IP address rather than the client's, so we need to specify an `ADDRESS_HEADER` to read the address from: ```sh ADDRESS_HEADER=True-Client-IP node build diff --git a/apps/svelte.dev/content/docs/kit/40-best-practices/03-auth.md b/apps/svelte.dev/content/docs/kit/40-best-practices/03-auth.md index 68c97ba567..395a07c6aa 100644 --- a/apps/svelte.dev/content/docs/kit/40-best-practices/03-auth.md +++ b/apps/svelte.dev/content/docs/kit/40-best-practices/03-auth.md @@ -15,7 +15,7 @@ In contrast, JWT generally are not checked against a datastore, which means they ## Integration points -Auth [cookies]($app-server#Cookies) can be checked inside the [`handle`](hooks#handle) hook. If a user is found matching the provided credentials, the user information can be stored in [`locals`](hooks#handle-locals). +Auth [cookies](@sveltejs-kit#Cookies) can be checked inside the [`handle`](hooks#handle) hook. If a user is found matching the provided credentials, the user information can be stored in [`locals`](hooks#handle-locals). ## Libraries diff --git a/apps/svelte.dev/content/docs/kit/40-best-practices/10-accessibility.md b/apps/svelte.dev/content/docs/kit/40-best-practices/10-accessibility.md index a76f54281e..1d8dd3d135 100644 --- a/apps/svelte.dev/content/docs/kit/40-best-practices/10-accessibility.md +++ b/apps/svelte.dev/content/docs/kit/40-best-practices/10-accessibility.md @@ -67,7 +67,7 @@ If your content is available in multiple languages, you should set the `lang` at ```js /// file: src/hooks.server.js // @filename: utils.ts -export function get_lang(event: import('$app/server').RequestEvent) { +export function get_lang(event: import('@sveltejs/kit').RequestEvent) { return 'en'; } diff --git a/apps/svelte.dev/content/docs/kit/60-appendix/35-migrating-to-sveltekit-3.md b/apps/svelte.dev/content/docs/kit/60-appendix/35-migrating-to-sveltekit-3.md index 57fddb9d3f..300e75880b 100644 --- a/apps/svelte.dev/content/docs/kit/60-appendix/35-migrating-to-sveltekit-3.md +++ b/apps/svelte.dev/content/docs/kit/60-appendix/35-migrating-to-sveltekit-3.md @@ -292,10 +292,6 @@ Types like `Handle`, which defines the type of your [`handle`](hooks#handle) hoo Types describing remote functions, such as `RemoteQuery`, `RemoteForm` and `RemoteCommand`, now live in `@sveltejs/kit/remote`. The `isValidationError` function has also moved there. -### Request-related types moved to `$app/server` - -Types like `RequestEvent` and `Cookies`, used by [`getRequestEvent`]($app-server#getRequestEvent), now live in `$app/server`. - ## `@sveltejs/kit/hooks` The `defineEnvVars` function has moved from `@sveltejs/kit/hooks` to `@sveltejs/kit/env`. @@ -365,7 +361,7 @@ SvelteKit now uses [`cookie`](https://npmx.dev/package/cookie) v2, which involve When setting a cookie without an explicit `path` (which was previously forbidden), the path defaults to `'/'` rather than the current request path, meaning the cookie applies to the entire site. This matches what most developers expect. You can pass an explicit `path` if necessary: ```ts -declare const cookies: import('$app/server').Cookies; +declare const cookies: import('@sveltejs/kit').Cookies; declare const name: string; declare const value: string; // ---cut--- diff --git a/apps/svelte.dev/content/docs/kit/98-reference/10-@sveltejs-kit.md b/apps/svelte.dev/content/docs/kit/98-reference/10-@sveltejs-kit.md index fe88e81865..64e31b16cc 100644 --- a/apps/svelte.dev/content/docs/kit/98-reference/10-@sveltejs-kit.md +++ b/apps/svelte.dev/content/docs/kit/98-reference/10-@sveltejs-kit.md @@ -990,6 +990,160 @@ Compress files in `directory` with gzip and brotli, where appropriate. Generates +## Cookies + +
+ +```dts +interface Cookies {/*…*/} +``` + +
+ +```dts +get: (name: string, opts?: import('cookie').ParseOptions) => string | undefined; +``` + +
+ +
+ +- `name` the name of the cookie +- `opts` the options, passed directly to `cookie.parseCookie`. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookieparsecookiestr-options) + +
+ +Gets a cookie that was previously set with `cookies.set`, or from the request headers. + +
+
+ +
+ +```dts +getAll: (opts?: import('cookie').ParseOptions) => Array<{ name: string; value: string }>; +``` + +
+ +
+ +- `opts` the options, passed directly to `cookie.parseCookie`. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookieparsecookiestr-options) + +
+ +Gets all cookies that were previously set with `cookies.set`, or from the request headers. + +
+
+ +
+ +```dts +set: (name: string, value: string, opts: import('cookie').SerializeOptions) => void; +``` + +
+ +
+ +- `name` the name of the cookie +- `value` the cookie value +- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) + +
+ +Sets a cookie. This will add a `set-cookie` header to the response, but also make the cookie available via `cookies.get` or `cookies.getAll` during the current request. + +The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. + +The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. + +
+
+ +
+ +```dts +delete: (name: string, opts: import('cookie').SerializeOptions) => void; +``` + +
+ +
+ +- `name` the name of the cookie +- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) + +
+ +Deletes a cookie by setting its value to an empty string and setting the expiry date in the past. + +The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. + +The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. + +
+
+ +
+ +```dts +parse: typeof import('cookie').parseSetCookie; +``` + +
+ +Parses a single `Set-Cookie` header. This allows you to apply cookies received from an external source: + +```js +// @errors: 7031 +import { getRequestEvent } from '$app/server'; + +export async function GET() { + const { cookies } = getRequestEvent(); + + const response = await fetch('...'); + + for (const str of response.headers.getSetCookie()) { + const { name, value, ...options } = cookies.parse(str); + cookies.set(name, value, { ...options, path: '/' }); + } + + // ... +} +``` + +Note the use of `headers.getSetCookie()`, which returns an array of cookie headers, _not_ `headers.get('set-cookie')` which returns a single comma-separated string. + +
+
+ +
+ +```dts +serialize: (name: string, value: string, opts: import('cookie').SerializeOptions) => string; +``` + +
+ +
+ +- `name` the name of the cookie +- `value` the cookie value +- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) + +
+ +Serialize a cookie name-value pair into a `Set-Cookie` header string, but don't apply it to the response. + +The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. + +The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. + +
+
+ ## Emulator A collection of functions that influence the environment during dev, build and prerendering @@ -1164,7 +1318,7 @@ export async function load({ fetch, setHeaders }) { Setting the same header multiple times (even in separate `load` functions) is an error — you can only set a given header once. -You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](/docs/kit/$app-server#Cookies) API in a server-only `load` function instead. +You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](/docs/kit/@sveltejs-kit#Cookies) API in a server-only `load` function instead. `setHeaders` has no effect when a `load` function runs in the browser. @@ -1441,6 +1595,301 @@ The location to redirect to. +## RequestEvent + +
+ +```dts +interface RequestEvent< + Params extends AppLayoutParams<'/'> = + AppLayoutParams<'/'>, + RouteId extends AppRouteId | null = AppRouteId | null +> {/*…*/} +``` + +
+ +```dts +readonly cookies: Cookies; +``` + +
+ +Get or set cookies related to the current request + +
+
+ +
+ +```dts +readonly fetch: typeof fetch; +``` + +
+ +`fetch` is equivalent to the [native `fetch` web API](https://developer.mozilla.org/en-US/docs/Web/API/fetch), with a few additional features: + +- It can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers for the page request. +- It can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context). +- Internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call. +- During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](/docs/kit/hooks#handle) +- During hydration, the response will be read from the HTML, guaranteeing consistency and preventing an additional network request. + +You can learn more about making credentialed requests with cookies [here](/docs/kit/load#Cookies). + +
+
+ +
+ +```dts +readonly getClientAddress: () => string; +``` + +
+ +The client's IP address, set by the adapter. + +
+
+ +
+ +```dts +readonly locals: App.Locals; +``` + +
+ +Contains custom data that was added to the request within the [`server handle hook`](/docs/kit/hooks#handle). + +
+
+ +
+ +```dts +readonly params: Params; +``` + +
+ +The parameters of the current route - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object. + +Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. +Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page +the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it +to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. + +
+
+ +
+ +```dts +readonly platform: Readonly | undefined; +``` + +
+ +Additional data made available through the adapter. + +
+
+ +
+ +```dts +readonly request: Request; +``` + +
+ +The original request object. + +
+
+ +
+ +```dts +readonly route: {/*…*/} +``` + +
+ +Info about the current route. + +
+ +```dts +id: RouteId; +``` + +
+ +The ID of the current route - e.g. for `src/routes/blog/[slug]`, it would be `/blog/[slug]`. It is `null` when no route is matched. + +Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. +Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page +the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it +to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. + +
+
+ +
+
+ +
+ +```dts +readonly setHeaders: (headers: Record) => void; +``` + +
+ +If you need to set headers for the response, you can do so using the this method. This is useful if you want the page to be cached, for example: + +```js +// @errors: 7031 +/// file: src/routes/blog/+page.js +export async function load({ fetch, setHeaders }) { + const url = `https://cms.example.com/articles.json`; + const response = await fetch(url); + + setHeaders({ + age: response.headers.get('age'), + 'cache-control': response.headers.get('cache-control') + }); + + return response.json(); +} +``` + +Setting the same header multiple times (even in separate `load` functions) is an error — you can only set a given header once. + +You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](/docs/kit/@sveltejs-kit#Cookies) API instead. + +
+
+ +
+ +```dts +readonly url: URL; +``` + +
+ +The requested URL. + +Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. +Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page +the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it +to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. + +
+
+ +
+ +```dts +readonly isDataRequest: boolean; +``` + +
+ +`true` if the request comes from the client asking for `+page/layout.server.js` data. The `url` property will be stripped of the internal information +related to the data request in this case. Use this property instead if the distinction is important to you. + +
+
+ +
+ +```dts +readonly isSubRequest: boolean; +``` + +
+ +`true` for `+server.js` calls coming from SvelteKit without the overhead of actually making an HTTP request. This happens when you make same-origin `fetch` requests on the server. + +
+
+ +
+ +```dts +readonly tracing: {/*…*/} +``` + +
+ +
+ +- available since v2.31.0 + +
+ +Access to spans for tracing. If tracing is not enabled, these spans will do nothing. + +
+ +```dts +enabled: boolean; +``` + +
+ +Whether tracing is enabled. + +
+
+
+ +```dts +root: Span; +``` + +
+ +The root span for the request. This span is named `sveltekit.handle.root`. + +
+
+
+ +```dts +current: Span; +``` + +
+ +The span associated with the current `handle` hook, `load` function, or form action. + +
+
+ +
+
+ +
+ +```dts +readonly isRemoteRequest: boolean; +``` + +
+ +`true` if the request comes from the client via a remote function. The `url` property will be stripped of the internal information +related to the data request in this case. Use this property instead if the distinction is important to you. + +
+
+ ## RequestHandler A `(event: RequestEvent) => Response` function exported from a `+server.js` file that corresponds to an HTTP verb (`GET`, `PUT`, `PATCH`, etc) and handles requests with that method. diff --git a/apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md b/apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md index 149f60a60d..e64632d76a 100644 --- a/apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md +++ b/apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md @@ -368,455 +368,6 @@ function requested( -## Cookies - -
- -```dts -interface Cookies {/*…*/} -``` - -
- -```dts -get: (name: string, opts?: import('cookie').ParseOptions) => string | undefined; -``` - -
- -
- -- `name` the name of the cookie -- `opts` the options, passed directly to `cookie.parseCookie`. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookieparsecookiestr-options) - -
- -Gets a cookie that was previously set with `cookies.set`, or from the request headers. - -
-
- -
- -```dts -getAll: (opts?: import('cookie').ParseOptions) => Array<{ name: string; value: string }>; -``` - -
- -
- -- `opts` the options, passed directly to `cookie.parseCookie`. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookieparsecookiestr-options) - -
- -Gets all cookies that were previously set with `cookies.set`, or from the request headers. - -
-
- -
- -```dts -set: (name: string, value: string, opts: import('cookie').SerializeOptions) => void; -``` - -
- -
- -- `name` the name of the cookie -- `value` the cookie value -- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) - -
- -Sets a cookie. This will add a `set-cookie` header to the response, but also make the cookie available via `cookies.get` or `cookies.getAll` during the current request. - -The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. - -The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. - -
-
- -
- -```dts -delete: (name: string, opts: import('cookie').SerializeOptions) => void; -``` - -
- -
- -- `name` the name of the cookie -- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) - -
- -Deletes a cookie by setting its value to an empty string and setting the expiry date in the past. - -The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. - -The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. - -
-
- -
- -```dts -parse: typeof import('cookie').parseSetCookie; -``` - -
- -Parses a single `Set-Cookie` header. This allows you to apply cookies received from an external source: - -```js -// @errors: 7031 -import { getRequestEvent } from '$app/server'; - -export async function GET() { - const { cookies } = getRequestEvent(); - - const response = await fetch('...'); - - for (const str of response.headers.getSetCookie()) { - const { name, value, ...options } = cookies.parse(str); - cookies.set(name, value, { ...options, path: '/' }); - } - - // ... -} -``` - -Note the use of `headers.getSetCookie()`, which returns an array of cookie headers, _not_ `headers.get('set-cookie')` which returns a single comma-separated string. - -
-
- -
- -```dts -serialize: (name: string, value: string, opts: import('cookie').SerializeOptions) => string; -``` - -
- -
- -- `name` the name of the cookie -- `value` the cookie value -- `opts` the options passed to `cookie.stringifySetCookie` with the SvelteKit defaults described above. See documentation [here](https://github.com/jshttp/cookie?tab=readme-ov-file#cookiestringifysetcookiesetcookieobj-options) - -
- -Serialize a cookie name-value pair into a `Set-Cookie` header string, but don't apply it to the response. - -The `httpOnly` is `true` by default, as is `secure`, except during development, when it defaults to `false`. These must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. - -The `path` option is `'/'` by default. You can use relative paths, or set `path: ''` to make the cookie only available on the current path and its children. - -
-
- -## RequestEvent - -
- -```dts -interface RequestEvent< - Params extends AppLayoutParams<'/'> = - AppLayoutParams<'/'>, - RouteId extends AppRouteId | null = AppRouteId | null -> {/*…*/} -``` - -
- -```dts -readonly cookies: Cookies; -``` - -
- -Get or set cookies related to the current request - -
-
- -
- -```dts -readonly fetch: typeof fetch; -``` - -
- -`fetch` is equivalent to the [native `fetch` web API](https://developer.mozilla.org/en-US/docs/Web/API/fetch), with a few additional features: - -- It can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers for the page request. -- It can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context). -- Internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call. -- During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](/docs/kit/hooks#handle) -- During hydration, the response will be read from the HTML, guaranteeing consistency and preventing an additional network request. - -You can learn more about making credentialed requests with cookies [here](/docs/kit/load#Cookies). - -
-
- -
- -```dts -readonly getClientAddress: () => string; -``` - -
- -The client's IP address, set by the adapter. - -
-
- -
- -```dts -readonly locals: App.Locals; -``` - -
- -Contains custom data that was added to the request within the [`server handle hook`](/docs/kit/hooks#handle). - -
-
- -
- -```dts -readonly params: Params; -``` - -
- -The parameters of the current route - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object. - -Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. -Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page -the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it -to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. - -
-
- -
- -```dts -readonly platform: Readonly | undefined; -``` - -
- -Additional data made available through the adapter. - -
-
- -
- -```dts -readonly request: Request; -``` - -
- -The original request object. - -
-
- -
- -```dts -readonly route: {/*…*/} -``` - -
- -Info about the current route. - -
- -```dts -id: RouteId; -``` - -
- -The ID of the current route - e.g. for `src/routes/blog/[slug]`, it would be `/blog/[slug]`. It is `null` when no route is matched. - -Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. -Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page -the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it -to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. - -
-
- -
-
- -
- -```dts -readonly setHeaders: (headers: Record) => void; -``` - -
- -If you need to set headers for the response, you can do so using the this method. This is useful if you want the page to be cached, for example: - -```js -// @errors: 7031 -/// file: src/routes/blog/+page.js -export async function load({ fetch, setHeaders }) { - const url = `https://cms.example.com/articles.json`; - const response = await fetch(url); - - setHeaders({ - age: response.headers.get('age'), - 'cache-control': response.headers.get('cache-control') - }); - - return response.json(); -} -``` - -Setting the same header multiple times (even in separate `load` functions) is an error — you can only set a given header once. - -You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](/docs/kit/$app-server#Cookies) API instead. - -
-
- -
- -```dts -readonly url: URL; -``` - -
- -The requested URL. - -Inside `query` functions (including `query.batch` and `query.live`), accessing this property throws an error. -Pass values from the page as arguments to the query instead. Inside `form` and `command` functions it relates to the page -the remote function was called from, _not_ the URL of the endpoint SvelteKit creates for the remote function. Never use it -to determine whether or not a user is authorized to access certain data, as these values are part of the request which could be manipulated. - -
-
- -
- -```dts -readonly isDataRequest: boolean; -``` - -
- -`true` if the request comes from the client asking for `+page/layout.server.js` data. The `url` property will be stripped of the internal information -related to the data request in this case. Use this property instead if the distinction is important to you. - -
-
- -
- -```dts -readonly isSubRequest: boolean; -``` - -
- -`true` for `+server.js` calls coming from SvelteKit without the overhead of actually making an HTTP request. This happens when you make same-origin `fetch` requests on the server. - -
-
- -
- -```dts -readonly tracing: {/*…*/} -``` - -
- -
- -- available since v2.31.0 - -
- -Access to spans for tracing. If tracing is not enabled, these spans will do nothing. - -
- -```dts -enabled: boolean; -``` - -
- -Whether tracing is enabled. - -
-
-
- -```dts -root: Span; -``` - -
- -The root span for the request. This span is named `sveltekit.handle.root`. - -
-
-
- -```dts -current: Span; -``` - -
- -The span associated with the current `handle` hook, `load` function, or form action. - -
-
- -
-
- -
- -```dts -readonly isRemoteRequest: boolean; -``` - -
- -`true` if the request comes from the client via a remote function. The `url` property will be stripped of the internal information -related to the data request in this case. Use this property instead if the distinction is important to you. - -
-
- ## query