-
Notifications
You must be signed in to change notification settings - Fork 47.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hydration DOM Fixture #13521
Merged
Merged
Hydration DOM Fixture #13521
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
de6ea59
Manually join extra attributes in warning
nhunzaker 5c67343
Add home component. Async load fixtures.
nhunzaker 1546840
Tweak select width
nhunzaker a8d8ce8
Adds experimental hydration fixture
nhunzaker 91cd8c2
Transform with buble
nhunzaker ce9a2b2
Eliminate dependencies
nhunzaker 546cb07
Pull in react-live for better editing
nhunzaker 9dad4a7
Handle encoding errors, pass react version
nhunzaker 0c88335
Load the correct version of React
nhunzaker 50856cc
Tweaks
nhunzaker 05f3042
Revert style change
nhunzaker ad5c392
Revert warning update
nhunzaker 7284c87
Properly handle script errors. Fix dom-server CDN loading
nhunzaker 046a27f
Fix 15.x releases
nhunzaker eac9900
Use postMessage to reduce latency, support older browsers
nhunzaker 2e69dc5
Fix fixture renamespacing bug
nhunzaker be904d1
Gracefully fallback to textarea in React 14
nhunzaker 88b1dd8
Replace buble with babel, react-live with codemirror
nhunzaker 84ac939
Simplify layout to resolve production code-mirror issues
nhunzaker b4ed966
Tweak height rules for code-mirror
nhunzaker 8a40c04
Update theme to paraiso
nhunzaker 17a957d
Format Code.js
nhunzaker 9d2b4a9
Adjust viewport to fix CodeMirror resize issue in production build
nhunzaker 6cd0e2e
Eliminate react-code-mirror
nhunzaker ecb1ea8
Improve error state. Make full stack collapsable
nhunzaker 4b50198
Add link to license in codemirror stylesheet
nhunzaker 3bf211e
Make code example more concise
nhunzaker 437c45b
Replace "Hydrate" with "Auto-hydrate" for clarity
nhunzaker aa321e2
Remove border below hydration header
nhunzaker 565ac72
Rename query function in render.js
nhunzaker d5a4acd
Use Function(code) to evaluate hydration fixture
nhunzaker ec2e1a2
Extend hydration fixture to fill width. Design adjustments
nhunzaker c7d4738
Improve error scroll state
nhunzaker e79cd06
Lazy load CodeMirror together before executing
nhunzaker bccb786
Fix indentation on error message
nhunzaker c1843bf
Do not highlight errors from Babel. Add setPrototypeOf polyfill
nhunzaker 81dcf6c
Increase resilience to bad errors in Hydration fixture
nhunzaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Renderer</title> | ||
<style> | ||
*, | ||
*:before, | ||
*:after { | ||
box-sizing: border-box; | ||
} | ||
|
||
html, | ||
body { | ||
font-family: sans-serif; | ||
margin: 0; | ||
height: 100%; | ||
} | ||
|
||
body { | ||
padding-top: 32px; | ||
} | ||
|
||
#status { | ||
font-size: 12px; | ||
left: 8px; | ||
letter-spacing: 0.05em; | ||
line-height: 16px; | ||
margin: -8px 0 0; | ||
max-width: 50%; | ||
overflow: hidden; | ||
position: absolute; | ||
text-align: left; | ||
text-overflow: ellipsis; | ||
top: 50%; | ||
white-space: nowrap; | ||
width: 100%; | ||
} | ||
|
||
#output { | ||
margin: 16px; | ||
} | ||
|
||
.header { | ||
background: white; | ||
border-bottom: 1px solid #d9d9d9; | ||
padding: 4px; | ||
top: 0; | ||
left: 0; | ||
position: fixed; | ||
width: 100%; | ||
text-align: right; | ||
} | ||
|
||
.controls { | ||
display: inline-block; | ||
margin: 0; | ||
} | ||
|
||
.button { | ||
background: #eee; | ||
border-radius: 2px; | ||
border: 1px solid #aaa; | ||
font-size: 11px; | ||
padding: 4px 6px; | ||
text-transform: uppercase; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<header class="header"> | ||
<p id="status">Loading</p> | ||
|
||
<menu class="controls"> | ||
<button class="button" id="hydrate">Hydrate</button> | ||
<button class="button" id="reload">Reload</button> | ||
</menu> | ||
</header> | ||
|
||
<div id="output"></div> | ||
|
||
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script> | ||
<script src="renderer.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* Supports render.html, a piece of the hydration fixture. See /hydration | ||
*/ | ||
|
||
'use strict'; | ||
|
||
(function() { | ||
var Fixture = null; | ||
var output = document.getElementById('output'); | ||
var status = document.getElementById('status'); | ||
var hydrate = document.getElementById('hydrate'); | ||
var reload = document.getElementById('reload'); | ||
var renders = 0; | ||
var failed = false; | ||
|
||
function getQueryParam(key) { | ||
var pattern = new RegExp(key + '=([^&]+)(&|$)'); | ||
var matches = window.location.search.match(pattern); | ||
|
||
if (matches) { | ||
return decodeURIComponent(matches[1]); | ||
} | ||
|
||
handleError(new Error('No key found for' + key)); | ||
} | ||
|
||
function getBooleanQueryParam(key) { | ||
return getQueryParam(key) === 'true'; | ||
} | ||
|
||
function setStatus(label) { | ||
status.innerHTML = label; | ||
} | ||
|
||
function prerender() { | ||
setStatus('Generating markup'); | ||
|
||
output.innerHTML = ReactDOMServer.renderToString( | ||
React.createElement(Fixture) | ||
); | ||
|
||
setStatus('Markup only (No React)'); | ||
} | ||
|
||
function render() { | ||
setStatus('Hydrating'); | ||
|
||
if (ReactDOM.hydrate) { | ||
ReactDOM.hydrate(React.createElement(Fixture), output); | ||
} else { | ||
ReactDOM.render(React.createElement(Fixture), output); | ||
} | ||
|
||
setStatus(renders > 0 ? 'Re-rendered (' + renders + 'x)' : 'Hydrated'); | ||
renders += 1; | ||
hydrate.innerHTML = 'Re-render'; | ||
} | ||
|
||
function handleError(error) { | ||
console.log(error); | ||
failed = true; | ||
setStatus('Javascript Error'); | ||
output.innerHTML = error; | ||
} | ||
|
||
function loadScript(src) { | ||
return new Promise(function(resolve, reject) { | ||
var script = document.createElement('script'); | ||
script.async = true; | ||
script.src = src; | ||
|
||
script.onload = resolve; | ||
script.onerror = function(error) { | ||
reject(new Error('Unable to load ' + src)); | ||
}; | ||
|
||
document.body.appendChild(script); | ||
}); | ||
} | ||
|
||
function injectFixture(src) { | ||
Fixture = new Function(src + '\nreturn Fixture;')(); | ||
|
||
if (typeof Fixture === 'undefined') { | ||
setStatus('Failed'); | ||
output.innerHTML = 'Please name your root component "Fixture"'; | ||
} else { | ||
prerender(); | ||
|
||
if (getBooleanQueryParam('hydrate')) { | ||
render(); | ||
} | ||
} | ||
} | ||
|
||
function reloadFixture(code) { | ||
renders = 0; | ||
ReactDOM.unmountComponentAtNode(output); | ||
injectFixture(code); | ||
} | ||
|
||
window.onerror = handleError; | ||
|
||
reload.onclick = function() { | ||
window.location.reload(); | ||
}; | ||
|
||
hydrate.onclick = render; | ||
|
||
loadScript(getQueryParam('reactPath')) | ||
.then(function() { | ||
return getBooleanQueryParam('needsReactDOM') | ||
? loadScript(getQueryParam('reactDOMPath')) | ||
: null; | ||
}) | ||
.then(function() { | ||
return loadScript(getQueryParam('reactDOMServerPath')); | ||
}) | ||
.then(function() { | ||
if (failed) { | ||
return; | ||
} | ||
|
||
window.addEventListener('message', function(event) { | ||
var data = JSON.parse(event.data); | ||
|
||
switch (data.type) { | ||
case 'code': | ||
reloadFixture(data.payload); | ||
break; | ||
default: | ||
throw new Error( | ||
'Renderer Error: Unrecognized message "' + data.type + '"' | ||
); | ||
} | ||
}); | ||
|
||
window.parent.postMessage(JSON.stringify({type: 'ready'}), '*'); | ||
}) | ||
.catch(handleError); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const React = window.React; | ||
|
||
export class CodeEditor extends React.Component { | ||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
componentDidMount() { | ||
// Important: CodeMirror incorrectly lays out the editor | ||
// if it executes before CSS has loaded | ||
// https://github.com/graphql/graphiql/issues/33#issuecomment-318188555 | ||
Promise.all([ | ||
import('codemirror'), | ||
import('codemirror/mode/jsx/jsx'), | ||
import('codemirror/lib/codemirror.css'), | ||
import('./codemirror-paraiso-dark.css'), | ||
]).then(([CodeMirror]) => this.install(CodeMirror)); | ||
} | ||
|
||
install(CodeMirror) { | ||
if (!this.textarea) { | ||
return; | ||
} | ||
|
||
const {onChange} = this.props; | ||
|
||
this.editor = CodeMirror.fromTextArea(this.textarea, { | ||
mode: 'jsx', | ||
theme: 'paraiso-dark', | ||
lineNumbers: true, | ||
}); | ||
|
||
this.editor.on('change', function(doc) { | ||
onChange(doc.getValue()); | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.editor) { | ||
this.editor.toTextArea(); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<textarea | ||
ref={ref => (this.textarea = ref)} | ||
defaultValue={this.props.code} | ||
autoComplete="off" | ||
hidden={true} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export class CodeError extends React.Component { | ||
render() { | ||
const {error, className} = this.props; | ||
|
||
if (!error) { | ||
return null; | ||
} | ||
|
||
const [summary, ...body] = error.message.split('\n'); | ||
|
||
return ( | ||
<details className={className}> | ||
<summary>{summary}</summary> | ||
<p>{body.join('\n').trim()}</p> | ||
</details> | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
fixtures/dom/src/components/fixtures/hydration/code-transformer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Babel works across all browsers, however it requires many polyfills. | ||
*/ | ||
|
||
import 'core-js/es6/weak-map'; | ||
import 'core-js/es6/weak-set'; | ||
import 'core-js/es6/number'; | ||
import 'core-js/es6/string'; | ||
import 'core-js/es6/array'; | ||
|
||
import {transform} from '@babel/standalone'; | ||
|
||
const presets = ['es2015', 'stage-3', 'react']; | ||
|
||
export function compile(raw) { | ||
return transform(raw, {presets}).code; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@philipp-spiess great call here. I was evaluating this in the context of the window, but I'm pretty sure we can keep it local just using a
Function
constructor.