Skip to content

Commit

Permalink
only inline data if hydrate=true (#837)
Browse files Browse the repository at this point in the history
* only inline data if hydrate=true

* changeset
  • Loading branch information
Rich Harris committed Apr 2, 2021
1 parent 4d2cd62 commit 6384af6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-beans-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Only inline data if hydrate=true
11 changes: 7 additions & 4 deletions packages/kit/src/runtime/server/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function get_response({ request, options, $session, route, status = 200, e
}
}

if (response) {
if (response && page_config.hydrate) {
const proxy = new Proxy(response, {
get(response, key, receiver) {
async function text() {
Expand Down Expand Up @@ -190,9 +190,12 @@ async function get_response({ request, options, $session, route, status = 200, e
return proxy;
}

return new Response('Not found', {
status: 404
});
return (
response ||
new Response('Not found', {
status: 404
})
);
};

const component_promises = error
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/src/routes/hydrate/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default function (test, is_dev) {

await page.click('button');
assert.equal(await page.textContent('button'), 'clicks: 1');
} else {
// ensure data wasn't inlined
assert.equal(
await page.evaluate(() => document.querySelectorAll('script[type="svelte-data"]').length),
0
);
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function get() {
return {
body: {
type: 'hydrate'
}
};
}
17 changes: 16 additions & 1 deletion packages/kit/test/apps/basics/src/routes/hydrate/index.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
<script context="module">
export const hydrate = false;
/** @type {import('../../../../../../types').Load} */
export async function load({ fetch }) {
const res = await fetch('/hydrate.json');
/** @type {any} */
const { type } = await res.json();
return {
props: { type }
}
}
</script>

<script>
/** @type {string} */
export let type;
let n = 0;
</script>

<button on:click={() => n += 1}>
clicks: {n}
</button>

<a href="/hydrate/other">other</a>
<a href="/{type}/other">other</a>

0 comments on commit 6384af6

Please sign in to comment.