|
| 1 | +/** |
| 2 | + * Supports render.html, a piece of the hydration fixture. See /hydration |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +(function() { |
| 8 | + var Fixture = null; |
| 9 | + var output = document.getElementById('output'); |
| 10 | + var status = document.getElementById('status'); |
| 11 | + var hydrate = document.getElementById('hydrate'); |
| 12 | + var reload = document.getElementById('reload'); |
| 13 | + var renders = 0; |
| 14 | + var failed = false; |
| 15 | + |
| 16 | + function getQueryParam(key) { |
| 17 | + var pattern = new RegExp(key + '=([^&]+)(&|$)'); |
| 18 | + var matches = window.location.search.match(pattern); |
| 19 | + |
| 20 | + if (matches) { |
| 21 | + return decodeURIComponent(matches[1]); |
| 22 | + } |
| 23 | + |
| 24 | + handleError(new Error('No key found for' + key)); |
| 25 | + } |
| 26 | + |
| 27 | + function getBooleanQueryParam(key) { |
| 28 | + return getQueryParam(key) === 'true'; |
| 29 | + } |
| 30 | + |
| 31 | + function setStatus(label) { |
| 32 | + status.innerHTML = label; |
| 33 | + } |
| 34 | + |
| 35 | + function prerender() { |
| 36 | + setStatus('Generating markup'); |
| 37 | + |
| 38 | + output.innerHTML = ReactDOMServer.renderToString( |
| 39 | + React.createElement(Fixture) |
| 40 | + ); |
| 41 | + |
| 42 | + setStatus('Markup only (No React)'); |
| 43 | + } |
| 44 | + |
| 45 | + function render() { |
| 46 | + setStatus('Hydrating'); |
| 47 | + |
| 48 | + if (ReactDOM.hydrate) { |
| 49 | + ReactDOM.hydrate(React.createElement(Fixture), output); |
| 50 | + } else { |
| 51 | + ReactDOM.render(React.createElement(Fixture), output); |
| 52 | + } |
| 53 | + |
| 54 | + setStatus(renders > 0 ? 'Re-rendered (' + renders + 'x)' : 'Hydrated'); |
| 55 | + renders += 1; |
| 56 | + hydrate.innerHTML = 'Re-render'; |
| 57 | + } |
| 58 | + |
| 59 | + function handleError(error) { |
| 60 | + console.log(error); |
| 61 | + failed = true; |
| 62 | + setStatus('Javascript Error'); |
| 63 | + output.innerHTML = error; |
| 64 | + } |
| 65 | + |
| 66 | + function loadScript(src) { |
| 67 | + return new Promise(function(resolve, reject) { |
| 68 | + var script = document.createElement('script'); |
| 69 | + script.async = true; |
| 70 | + script.src = src; |
| 71 | + |
| 72 | + script.onload = resolve; |
| 73 | + script.onerror = function(error) { |
| 74 | + reject(new Error('Unable to load ' + src)); |
| 75 | + }; |
| 76 | + |
| 77 | + document.body.appendChild(script); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + function injectFixture(src) { |
| 82 | + Fixture = new Function(src + '\nreturn Fixture;')(); |
| 83 | + |
| 84 | + if (typeof Fixture === 'undefined') { |
| 85 | + setStatus('Failed'); |
| 86 | + output.innerHTML = 'Please name your root component "Fixture"'; |
| 87 | + } else { |
| 88 | + prerender(); |
| 89 | + |
| 90 | + if (getBooleanQueryParam('hydrate')) { |
| 91 | + render(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + function reloadFixture(code) { |
| 97 | + renders = 0; |
| 98 | + ReactDOM.unmountComponentAtNode(output); |
| 99 | + injectFixture(code); |
| 100 | + } |
| 101 | + |
| 102 | + window.onerror = handleError; |
| 103 | + |
| 104 | + reload.onclick = function() { |
| 105 | + window.location.reload(); |
| 106 | + }; |
| 107 | + |
| 108 | + hydrate.onclick = render; |
| 109 | + |
| 110 | + loadScript(getQueryParam('reactPath')) |
| 111 | + .then(function() { |
| 112 | + return getBooleanQueryParam('needsReactDOM') |
| 113 | + ? loadScript(getQueryParam('reactDOMPath')) |
| 114 | + : null; |
| 115 | + }) |
| 116 | + .then(function() { |
| 117 | + return loadScript(getQueryParam('reactDOMServerPath')); |
| 118 | + }) |
| 119 | + .then(function() { |
| 120 | + if (failed) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + window.addEventListener('message', function(event) { |
| 125 | + var data = JSON.parse(event.data); |
| 126 | + |
| 127 | + switch (data.type) { |
| 128 | + case 'code': |
| 129 | + reloadFixture(data.payload); |
| 130 | + break; |
| 131 | + default: |
| 132 | + throw new Error( |
| 133 | + 'Renderer Error: Unrecognized message "' + data.type + '"' |
| 134 | + ); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + window.parent.postMessage(JSON.stringify({type: 'ready'}), '*'); |
| 139 | + }) |
| 140 | + .catch(handleError); |
| 141 | +})(); |
0 commit comments