Skip to content

Commit ea5cc03

Browse files
committed
Expose initial state in head
1 parent 3c06a3a commit ea5cc03

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/graph-document.js

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function node (state, createEdge) {
8181
var d = documentify(entry, html)
8282
var header = [
8383
viewportTag(),
84+
content.state && initialState(content.state),
8485
scriptTag({ hash: state.scripts.bundle.hash, base: base }),
8586
hasDynamicScripts && dynamicScriptsTag({
8687
bundleNames: String(state.scripts.list.buffer).split(','),
@@ -131,6 +132,13 @@ function preloadTag () {
131132
return `<script>${content}</script>`
132133
}
133134

135+
function initialState (state) {
136+
var json = JSON.stringify(state)
137+
.replace(/\u2028/g, '\\u2028')
138+
.replace(/\u2029/g, '\\u2029')
139+
return `<script>window.initialState = ${json}</script>`
140+
}
141+
134142
function scriptTag (opts) {
135143
var hex = opts.hash.toString('hex').slice(0, 16)
136144
var base64 = 'sha512-' + opts.hash.toString('base64')

test/document.js

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ tape('server render choo apps', function (assert) {
8181
<head>
8282
<meta charset="utf-8">
8383
<meta name="viewport" content="width=device-width, initial-scale=1.0">
84+
<script>window.initialState = {}</script>
8485
<script src="/__SCRIPTS_HASH__/bundle.js" integrity="sha512-__SCRIPTS_INTEGRITY__" defer></script>
8586
<script>;(function(a){"use strict";var b=function(b,c,d){function e(a){return h.body?a():void setTimeout(function(){e(a)})}function f(){i.addEventListener&&i.removeEventListener("load",f),i.media=d||"all"}var g,h=a.document,i=h.createElement("link");if(c)g=c;else{var j=(h.body||h.getElementsByTagName("head")[0]).childNodes;g=j[j.length-1]}var k=h.styleSheets;i.rel="stylesheet",i.href=b,i.media="only x",e(function(){g.parentNode.insertBefore(i,c?g:g.nextSibling)});var l=function(a){for(var b=i.href,c=k.length;c--;)if(k[c].href===b)return a();setTimeout(function(){l(a)})};return i.addEventListener&&i.addEventListener("load",f),i.onloadcssdefined=l,l(f),i};"undefined"!=typeof exports?exports.loadCSS=b:a.loadCSS=b})("undefined"!=typeof global?global:this);;(function(a){if(a.loadCSS){var b=loadCSS.relpreload={};if(b.support=function(){try{return a.document.createElement("link").relList.supports("preload")}catch(b){return!1}},b.poly=function(){for(var b=a.document.getElementsByTagName("link"),c=0;c<b.length;c++){var d=b[c];"preload"===d.rel&&"style"===d.getAttribute("as")&&(a.loadCSS(d.href,d,d.getAttribute("media")),d.rel=null)}},!b.support()){b.poly();var c=a.setInterval(b.poly,300);a.addEventListener&&a.addEventListener("load",function(){b.poly(),a.clearInterval(c)}),a.attachEvent&&a.attachEvent("onload",function(){a.clearInterval(c)})}}})(this);</script>
8687
<link rel="manifest" href="/manifest.json">
@@ -149,6 +150,7 @@ tape('server render choo apps with root set', function (assert) {
149150
<head>
150151
<meta charset="utf-8">
151152
<meta name="viewport" content="width=device-width, initial-scale=1.0">
153+
<script>window.initialState = {}</script>
152154
<script src="some-custom-root/__SCRIPTS_HASH__/bundle.js" integrity="sha512-__SCRIPTS_INTEGRITY__" defer></script>
153155
<script>;(function(a){"use strict";var b=function(b,c,d){function e(a){return h.body?a():void setTimeout(function(){e(a)})}function f(){i.addEventListener&&i.removeEventListener("load",f),i.media=d||"all"}var g,h=a.document,i=h.createElement("link");if(c)g=c;else{var j=(h.body||h.getElementsByTagName("head")[0]).childNodes;g=j[j.length-1]}var k=h.styleSheets;i.rel="stylesheet",i.href=b,i.media="only x",e(function(){g.parentNode.insertBefore(i,c?g:g.nextSibling)});var l=function(a){for(var b=i.href,c=k.length;c--;)if(k[c].href===b)return a();setTimeout(function(){l(a)})};return i.addEventListener&&i.addEventListener("load",f),i.onloadcssdefined=l,l(f),i};"undefined"!=typeof exports?exports.loadCSS=b:a.loadCSS=b})("undefined"!=typeof global?global:this);;(function(a){if(a.loadCSS){var b=loadCSS.relpreload={};if(b.support=function(){try{return a.document.createElement("link").relList.supports("preload")}catch(b){return!1}},b.poly=function(){for(var b=a.document.getElementsByTagName("link"),c=0;c<b.length;c++){var d=b[c];"preload"===d.rel&&"style"===d.getAttribute("as")&&(a.loadCSS(d.href,d,d.getAttribute("media")),d.rel=null)}},!b.support()){b.poly();var c=a.setInterval(b.poly,300);a.addEventListener&&a.addEventListener("load",function(){b.poly(),a.clearInterval(c)}),a.attachEvent&&a.attachEvent("onload",function(){a.clearInterval(c)})}}})(this);</script>
154156
<link rel="manifest" href="some-custom-root/manifest.json">

0 commit comments

Comments
 (0)