Skip to content

Commit f2f07d6

Browse files
author
Rich Harris
committed
document headers and cookies
1 parent 7d455cb commit f2f07d6

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Setting headers
3+
---
4+
5+
Inside a `load` function (as well as in [form actions](the-form-element), [hooks](handle) and [API routes](get-handlers), which we'll learn about later) you have access to a `setHeaders` function, which — unsurprisingly — can be used to set headers on the response.
6+
7+
Most commonly, you'd use it to customise caching behaviour with the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) response header, but for the sake of this tutorial we'll do something less advisable and more dramatic:
8+
9+
```js
10+
/// file: src/routes/+page.server.js
11+
export function load(+++{ setHeaders }+++) {
12+
+++ setHeaders({
13+
'Content-Type': 'text/plain'
14+
});+++
15+
}
16+
```
17+
18+
(You may need to reload the iframe to see the effect.)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function load() {
2+
// set headers
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>hello world</h1>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function load({ setHeaders }) {
2+
setHeaders({
3+
'Content-Type': 'text/plain'
4+
});
5+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Reading and writing cookies
3+
---
4+
5+
The [`setHeaders`](headers) function can't be used with the `Set-Cookie` header. Instead, you should use the `cookies` API.
6+
7+
In your `load` functions, you can read a cookie with `cookies.get(name, options)`:
8+
9+
```js
10+
/// file: src/routes/+page.server.js
11+
export function load(+++{ cookies }+++) {
12+
+++const visited = cookies.get('visited');+++
13+
14+
return {
15+
visited
16+
};
17+
}
18+
```
19+
20+
To set a cookie, you `cookies.set(name, value, options)`. It's strongly recommended that you explicitly configure the `path` when setting a cookie, since browsers' default behaviour — somewhat uselessly — is to set the cookie on the parent of the current path.
21+
22+
```js
23+
/// file: src/routes/+page.server.js
24+
export function load({ cookies }) {
25+
const visited = cookies.get('visited');
26+
27+
+++cookies.set('visited', 'true', { path: '/' });+++
28+
29+
return {
30+
visited
31+
};
32+
}
33+
```
34+
35+
Now, if you reload the iframe, `Hello stranger!` becomes `Hello friend!`.
36+
37+
Calling `cookies.set(name, ...)` causes a `Set-Cookie` header to be written, but it _also_ updates the internal map of cookies, meaning any subsequent calls to `cookies.get(name)` during the same request will return the updated value. Under the hood, the `cookies` API uses the popular `cookie` package — the options passed to `cookies.get` and `cookies.set` correspond to the `parse` and `serialize` options from the `cookie` [documentation](https://github.com/jshttp/cookie#api). SvelteKit sets the following defaults to make your cookies more secure:
38+
39+
```js
40+
/// no-file
41+
{
42+
httpOnly: true,
43+
secure: true,
44+
sameSite: 'lax'
45+
}
46+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function load() {
2+
const visited = false;
3+
4+
return {
5+
visited
6+
};
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>Hello {data.visited ? 'friend' : 'stranger'}!</h1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function load({ cookies }) {
2+
const visited = cookies.get('visited');
3+
4+
cookies.set('visited', 'true', { path: '/' });
5+
6+
return {
7+
visited
8+
};
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"title": "Headers and cookies"
3+
}

0 commit comments

Comments
 (0)