Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 44285cd

Browse files
committed
serialized preloaded data and send to client as initial payload - fixes #3
1 parent bd656cf commit 44285cd

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

lib/index.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const serialize = require('serialize-javascript');
34
const route_manager = require('./route_manager.js');
45
const templates = require('./templates.js');
56
const create_app = require('./utils/create_app.js');
@@ -144,15 +145,25 @@ function get_route_handler(fn) {
144145

145146
if (mod.preload) {
146147
const promise = Promise.resolve(mod.preload(req)).then(preloaded => {
148+
const serialized = try_serialize(preloaded);
147149
Object.assign(data, preloaded);
148-
return mod.render(data);
150+
151+
return { rendered: mod.render(data), serialized };
149152
});
150153

151154
return templates.stream(res, 200, {
152-
scripts: `<script src='${client.main_file}'></script>`,
153-
html: promise.then(rendered => rendered.html),
154-
head: promise.then(({ head }) => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`),
155-
styles: promise.then(({ css }) => (css && css.code ? `<style>${css.code}</style>` : ''))
155+
scripts: promise.then(({ serialized }) => {
156+
const main = `<script src='${client.main_file}'></script>`;
157+
158+
if (serialized) {
159+
return `<script>__SAPPER__ = { preloaded: ${serialized} };</script>${main}`;
160+
}
161+
162+
return main;
163+
}),
164+
html: promise.then(({ rendered }) => rendered.html),
165+
head: promise.then(({ rendered }) => `<noscript id='sapper-head-start'></noscript>${rendered.head}<noscript id='sapper-head-end'></noscript>`),
166+
styles: promise.then(({ rendered }) => (rendered.css && rendered.css.code ? `<style>${rendered.css.code}</style>` : ''))
156167
});
157168
} else {
158169
const { html, head, css } = mod.render(data);
@@ -249,4 +260,12 @@ function compose_handlers(handlers) {
249260

250261
function read_json(file) {
251262
return JSON.parse(fs.readFileSync(file, 'utf-8'));
263+
}
264+
265+
function try_serialize(data) {
266+
try {
267+
return serialize(data);
268+
} catch (err) {
269+
return null;
270+
}
252271
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"relative": "^3.0.2",
2727
"require-relative": "^0.8.7",
2828
"rimraf": "^2.6.2",
29+
"serialize-javascript": "^1.4.0",
2930
"webpack": "^3.10.0",
3031
"webpack-hot-middleware": "^2.21.0"
3132
},

src/runtime/index.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ function render(Component: ComponentConstructor, data: any, scroll: ScrollPositi
6868
}
6969
}
7070

71-
function prepare_route(Component, data) {
72-
return Promise.resolve(
73-
Component.preload ? Component.preload(data) : {}
74-
).then(preloaded => {
71+
function prepare_route(Component: ComponentConstructor, data: RouteData) {
72+
if (!Component.preload) {
73+
return { Component, data };
74+
}
75+
76+
if (!component && window.__SAPPER__ && window.__SAPPER__.preloaded) {
77+
return { Component, data: Object.assign(data, window.__SAPPER__.preloaded) };
78+
}
79+
80+
return Promise.resolve(Component.preload(data)).then(preloaded => {
7581
Object.assign(data, preloaded)
7682
return { Component, data };
7783
});
@@ -176,10 +182,10 @@ export function prefetch(href: string) {
176182
}
177183

178184
function handle_touchstart_mouseover(event: MouseEvent | TouchEvent) {
179-
const a: HTMLAnchorElement = <HTMLAnchorElement>findAnchor(<Node>event.target);
180-
if (!a || a.rel !== 'prefetch') return;
185+
const a: HTMLAnchorElement = <HTMLAnchorElement>findAnchor(<Node>event.target);
186+
if (!a || a.rel !== 'prefetch') return;
181187

182-
prefetch(a.href);
188+
prefetch(a.href);
183189
}
184190

185191
let inited: boolean;

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -3628,6 +3628,10 @@ [email protected]:
36283628
range-parser "~1.2.0"
36293629
statuses "~1.3.1"
36303630

3631+
serialize-javascript@^1.4.0:
3632+
version "1.4.0"
3633+
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.4.0.tgz#7c958514db6ac2443a8abc062dc9f7886a7f6005"
3634+
36313635
36323636
version "1.13.1"
36333637
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.13.1.tgz#4c57d53404a761d8f2e7c1e8a18a47dbf278a719"

0 commit comments

Comments
 (0)