Skip to content

Commit 31b33af

Browse files
github-actions[bot]svelte-docs-bot[bot]paoloricciutiRich-Harris
authored
Sync svelte docs (#1703)
sync svelte docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com> Co-authored-by: Paolo Ricciuti <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent c5b0434 commit 31b33af

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

apps/svelte.dev/content/docs/svelte/06-runtime/05-hydratable.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,61 @@ All data returned from a `hydratable` function must be serializable. But this do
6464
{await promises.one}
6565
{await promises.two}
6666
```
67+
68+
## CSP
69+
70+
`hydratable` adds an inline `<script>` block to the `head` returned from `render`. If you're using [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) (CSP), this script will likely fail to run. You can provide a `nonce` to `render`:
71+
72+
```js
73+
/// file: server.js
74+
import { render } from 'svelte/server';
75+
import App from './App.svelte';
76+
// ---cut---
77+
const nonce = crypto.randomUUID();
78+
79+
const { head, body } = await render(App, {
80+
csp: { nonce }
81+
});
82+
```
83+
84+
This will add the `nonce` to the script block, on the assumption that you will later add the same nonce to the CSP header of the document that contains it:
85+
86+
```js
87+
/// file: server.js
88+
let response = new Response();
89+
let nonce = 'xyz123';
90+
// ---cut---
91+
response.headers.set(
92+
'Content-Security-Policy',
93+
`script-src 'nonce-${nonce}'`
94+
);
95+
```
96+
97+
It's essential that a `nonce` — which, British slang definition aside, means 'number used once' — is only used when dynamically server rendering an individual response.
98+
99+
If instead you are generating static HTML ahead of time, you must use hashes instead:
100+
101+
```js
102+
/// file: server.js
103+
import { render } from 'svelte/server';
104+
import App from './App.svelte';
105+
// ---cut---
106+
const { head, body, hashes } = await render(App, {
107+
csp: { hash: true }
108+
});
109+
```
110+
111+
`hashes.script` will be an array of strings like `["sha256-abcd123"]`. As with `nonce`, the hashes should be used in your CSP header:
112+
113+
```js
114+
/// file: server.js
115+
let response = new Response();
116+
let hashes = { script: ['sha256-xyz123'] };
117+
// ---cut---
118+
response.headers.set(
119+
'Content-Security-Policy',
120+
`script-src ${hashes.script.map((hash) => `'${hash}'`).join(' ')}`
121+
);
122+
```
123+
124+
We recommend using `nonce` over hash if you can, as `hash` will interfere with streaming SSR in the future.

apps/svelte.dev/content/docs/svelte/98-reference/.generated/server-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Cause:
5555
%stack%
5656
```
5757

58+
### invalid_csp
59+
60+
```
61+
`csp.nonce` was set while `csp.hash` was `true`. These options cannot be used simultaneously.
62+
```
63+
5864
### lifecycle_function_unavailable
5965

6066
```

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-server.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function render<
3131
props?: Omit<Props, '$$slots' | '$$events'>;
3232
context?: Map<any, any>;
3333
idPrefix?: string;
34+
csp?: Csp;
3435
}
3536
]
3637
: [
@@ -41,6 +42,7 @@ function render<
4142
props: Omit<Props, '$$slots' | '$$events'>;
4243
context?: Map<any, any>;
4344
idPrefix?: string;
45+
csp?: Csp;
4446
}
4547
]
4648
): RenderOutput;

apps/svelte.dev/content/docs/svelte/98-reference/30-runtime-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ Cause:
357357
%stack%
358358
```
359359

360+
### invalid_csp
361+
362+
```
363+
`csp.nonce` was set while `csp.hash` was `true`. These options cannot be used simultaneously.
364+
```
365+
360366
### lifecycle_function_unavailable
361367

362368
```

0 commit comments

Comments
 (0)