You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/06-runtime/05-hydratable.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,3 +63,61 @@ All data returned from a `hydratable` function must be serializable. But this do
63
63
{await promises.one}
64
64
{await promises.two}
65
65
```
66
+
67
+
## CSP
68
+
69
+
`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`:
70
+
71
+
```js
72
+
/// file: server.js
73
+
import { render } from'svelte/server';
74
+
importAppfrom'./App.svelte';
75
+
// ---cut---
76
+
constnonce=crypto.randomUUID();
77
+
78
+
const { head, body } =awaitrender(App, {
79
+
csp: { nonce }
80
+
});
81
+
```
82
+
83
+
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:
84
+
85
+
```js
86
+
/// file: server.js
87
+
let response =newResponse();
88
+
let nonce ='xyz123';
89
+
// ---cut---
90
+
response.headers.set(
91
+
'Content-Security-Policy',
92
+
`script-src 'nonce-${nonce}'`
93
+
);
94
+
```
95
+
96
+
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.
97
+
98
+
If instead you are generating static HTML ahead of time, you must use hashes instead:
99
+
100
+
```js
101
+
/// file: server.js
102
+
import { render } from'svelte/server';
103
+
importAppfrom'./App.svelte';
104
+
// ---cut---
105
+
const { head, body, hashes } =awaitrender(App, {
106
+
csp: { hash:true }
107
+
});
108
+
```
109
+
110
+
`hashes.script` will be an array of strings like `["sha256-abcd123"]`. As with `nonce`, the hashes should be used in your CSP header:
* `csp.nonce` was set while `csp.hash` was `true`. These options cannot be used simultaneously.
85
+
* @returns {never}
86
+
*/
87
+
exportfunctioninvalid_csp(){
88
+
consterror=newError(`invalid_csp\n\`csp.nonce\` was set while \`csp.hash\` was \`true\`. These options cannot be used simultaneously.\nhttps://svelte.dev/e/invalid_csp`);
* Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app.
/** @typedef {{ [key in RendererType]: string }} AccumulatedContent */
@@ -376,13 +376,13 @@ export class Renderer {
376
376
* Takes a component and returns an object with `body` and `head` properties on it, which you can use to populate the HTML when server-rendering your app.
0 commit comments