Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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---
Expand Down
Loading