Skip to content

Commit ac92c4a

Browse files
committed
Expose initial state in head
1 parent 7562b51 commit ac92c4a

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
@@ -101,6 +101,7 @@ function node (state, createEdge) {
101101
var d = documentify(filename, html)
102102
var header = [
103103
viewportTag(),
104+
content.state && initialState(content.state),
104105
scriptTag({ hash: state.scripts.bundle.hash, base: base }),
105106
hasDynamicScripts && dynamicScriptsTag({
106107
bundleNames: state.scripts.bundle.dynamicBundles,
@@ -157,6 +158,13 @@ function preloadTag () {
157158
return `<script>${content}</script>`
158159
}
159160

161+
function initialState (state) {
162+
var json = JSON.stringify(state)
163+
.replace(/\u2028/g, '\\u2028')
164+
.replace(/\u2029/g, '\\u2029')
165+
return `<script>window.initialState = ${json}</script>`
166+
}
167+
160168
function scriptTag (opts) {
161169
var hex = opts.hash.toString('hex').slice(0, 16)
162170
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">
@@ -151,6 +152,7 @@ tape('server render choo apps with root set', function (assert) {
151152
<head>
152153
<meta charset="utf-8">
153154
<meta name="viewport" content="width=device-width, initial-scale=1.0">
155+
<script>window.initialState = {}</script>
154156
<script src="some-custom-root/__SCRIPTS_HASH__/bundle.js" integrity="sha512-__SCRIPTS_INTEGRITY__" defer></script>
155157
<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>
156158
<link rel="manifest" href="some-custom-root/manifest.json">

0 commit comments

Comments
 (0)