1.0.37 • Published 1 year ago
svelte-baked-cookie v1.0.37
svelte-baked-cookie
🍪 Universal accessible hard-baked cookies for SvelteKit
Demo
From a single schema, type-defined cookies can be accessed in any environment.
Installation
npm i svelte-baked-cookieUsage
- Use the
bakeryfunction to define the cookie type and getbakeandrebake. See ts-serde for more information on type guard
// bakery.js
import { bakery } from 'svelte-baked-cookie'
import { json, number, string } from 'svelte-baked-cookie/serde'
export const { bake, rebake } = bakery(
{
key1: string,
key2: number,
key3: json(
(x): x is string[] =>
Array.isArray(x) && x.every((y) => typeof y === 'string'),
[]
)
}
// {
// CookieSetOptions: (optional)
// }
)- In the server, you can get a typed accessor by passing a
cookiesobject obtained fromload, etc. to thebakefunction.
// +layout.server.js
import { bake } from './bakery.js'
export const load = ({ cookies }) => {
const { bakedCookies } = bake(cookies)
// string
const str = bakedCookies.key1.get()
// number
const num = bakedCookies.key2.set()
// string[]
bakedCookies.key3.set(['value', 'set', 'by', 'server'])
return {
// ...
}
}- On the client, the
rebakefunction can be used directly to obtain awritablesvelte-store of typed cookies.
<!-- +page.svelte -->
<script>
import { rebake } from './bakery.js'
const cookies = rebake()
// key1: Writable<string>
// key2: Writable<number>
// key3: Writable<string[]>
const { key1, key2, key3 } = cookies
// string
$: console.log($key1)
// string
$: $key2 = 123
// or
$: key2.set(123)
// string[]
$: $key3 = ['value', 'set', 'by', 'client']
// or
$: key3.set(['value', 'set', 'by', 'client'])
</script>- (optional): When using SSRs, this may not be sufficient.
When rendering svelte components on the server, the server's cookie cannot be accessed directly, which may result in display flickering.
To solve this, you need to pass the cookiepiefrom +layout.server.js, etc. and make it thedoughofrebake.
// +layout.server.ts
import { bake } from './bakery.js'
export const load = ({ cookies }) => {
const { bakedCookies, pie } = bake(cookies)
// ...
return {
pie
}
}<!-- +layout.svelte -->
<script>
import { rebake } from './bakery.js'
export let data
$: ({ pie } = data)
$: cookies = rebake(pie)
// key1: Writable<string>
// key2: Writable<number>
// key3: Writable<string[]>
$: { key1, key2, key3 } = cookies
</script>This is optional, but it provides full consistency and typed cookie access to the application.
License
1.0.33
1 year ago
1.0.32
1 year ago
1.0.37
1 year ago
1.0.36
1 year ago
1.0.35
1 year ago
1.0.34
1 year ago
2.0.0-next.1
1 year ago
1.0.26
2 years ago
1.0.29
1 year ago
1.0.28
1 year ago
1.0.27
2 years ago
1.0.31
1 year ago
1.0.30
1 year ago
1.0.25
2 years ago
1.0.24
2 years ago
1.0.23
2 years ago
1.0.22
2 years ago
1.0.21
2 years ago
1.0.20
2 years ago
1.0.19
2 years ago
1.0.18
2 years ago
1.0.17
2 years ago
1.0.16
2 years ago
1.0.15
2 years ago
1.0.14
2 years ago
1.0.13
2 years ago
1.0.12
2 years ago
1.0.11
2 years ago
1.0.10
2 years ago
1.0.9
2 years ago
1.0.8
2 years ago
1.0.7
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
0.1.0
2 years ago
1.0.0
2 years ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago