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
5 changes: 5 additions & 0 deletions .changeset/tidy-timers-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: page response missing CSP and Link headers when return promise in `load`
12 changes: 7 additions & 5 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export async function render_response({
event,
options,
branch.map((b) => b.server_data),
csp,
global
);

Expand Down Expand Up @@ -511,9 +512,7 @@ export async function render_response({
type: 'bytes'
}),
{
headers: {
'content-type': 'text/html'
}
headers
}
);
}
Expand All @@ -524,10 +523,11 @@ export async function render_response({
* @param {import('@sveltejs/kit').RequestEvent} event
* @param {import('types').SSROptions} options
* @param {Array<import('types').ServerDataNode | null>} nodes
* @param {import('./csp.js').Csp} csp
* @param {string} global
* @returns {{ data: string, chunks: AsyncIterable<string> | null }}
*/
function get_data(event, options, nodes, global) {
function get_data(event, options, nodes, csp, global) {
let promise_id = 1;
let count = 0;

Expand Down Expand Up @@ -566,7 +566,9 @@ function get_data(event, options, nodes, global) {
str = devalue.uneval({ id, data, error }, replacer);
}

push(`<script>${global}.resolve(${str})</script>\n`);
push(
`<script${csp.script_needs_nonce ? ` nonce="${csp.nonce}"` : ''}>${global}.resolve(${str})</script>\n`
);
if (count === 0) done();
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function load() {
return {
lazy: new Promise((resolve) => setTimeout(() => resolve(), 1000)).then(() => 'Moo Deng!')
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export let data;
</script>

{#await data.lazy}
Loading...
{:then value}
<h2>{value}</h2>
{/await}
9 changes: 9 additions & 0 deletions packages/kit/test/apps/options/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ test.describe('CSP', () => {
expect(await page.evaluate('window.pwned')).toBe(undefined);
});

test('ensure CSP header in stream response', async ({ page, javaScriptEnabled }) => {
if (!javaScriptEnabled) return;
const response = await page.goto('/path-base/csp-with-stream');
expect(response.headers()['content-security-policy']).toMatch(
/require-trusted-types-for 'script'/
);
expect(await page.textContent('h2')).toBe('Moo Deng!');
});

test("quotes 'script'", async ({ page }) => {
const response = await page.goto('/path-base');
expect(response.headers()['content-security-policy']).toMatch(
Expand Down
Loading