From bffffe003521b2dbd75119c0c88127da7c71edea Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 13 Dec 2017 20:24:58 -0500 Subject: [PATCH] detach SSRd contents --- connect.js | 3 ++- runtime/{router.js => app.js} | 6 ++---- templates/main.js | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) rename runtime/{router.js => app.js} (86%) diff --git a/connect.js b/connect.js index 38aa1a1..257da8d 100644 --- a/connect.js +++ b/connect.js @@ -64,11 +64,12 @@ module.exports = function connect(opts) { let data = { params: req.params, query: req.query }; if (mod.default.preload) data = Object.assign(data, await mod.default.preload(data)); - const { html, css } = mod.default.render(data); + const { html, head, css } = mod.default.render(data); const page = templates.render(200, { main, html, + head: `${head}`, styles: (css && css.code ? `` : '') }); diff --git a/runtime/router.js b/runtime/app.js similarity index 86% rename from runtime/router.js rename to runtime/app.js index be0c781..ca91898 100644 --- a/runtime/router.js +++ b/runtime/app.js @@ -1,4 +1,4 @@ -const router = { +const app = { init(callback) { window.addEventListener('click', event => { let a = event.target; @@ -19,6 +19,4 @@ const router = { } }; -window.router = router; - -export default router; \ No newline at end of file +export default app; \ No newline at end of file diff --git a/templates/main.js b/templates/main.js index 9013c2a..09cff6f 100644 --- a/templates/main.js +++ b/templates/main.js @@ -1,9 +1,10 @@ -import router from 'sapper/runtime/router.js'; +import app from 'sapper/runtime/app.js'; +import { detachNode } from 'svelte/shared.js'; const target = document.querySelector('__selector__'); let component; -router.init(url => { +app.init(url => { if (url.origin !== window.location.origin) return; let match; @@ -19,6 +20,16 @@ router.init(url => { if (component) { component.destroy(); } else { + // remove SSR'd contents + const start = document.querySelector('#sapper-head-start'); + let end = document.querySelector('#sapper-head-end'); + + if (start && end) { + while (start.nextSibling !== end) detachNode(start.nextSibling); + detachNode(start); + detachNode(end); + } + target.innerHTML = ''; }