-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Don't discard component state on error #741
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createElement } from 'react' | ||
import ReactDOM from 'react-dom' | ||
import HeadManager from './head-manager' | ||
import { rehydrate } from '../lib/css' | ||
import { createRouter } from '../lib/router' | ||
import App from '../lib/app' | ||
import evalScript from '../lib/eval-script' | ||
|
||
const { | ||
__NEXT_DATA__: { | ||
component, | ||
errorComponent, | ||
props, | ||
ids, | ||
err, | ||
pathname, | ||
query | ||
} | ||
} = window | ||
|
||
const Component = evalScript(component).default | ||
const ErrorComponent = evalScript(errorComponent).default | ||
let lastAppProps | ||
|
||
export const router = createRouter(pathname, query, { | ||
Component, | ||
ErrorComponent, | ||
err | ||
}) | ||
|
||
const headManager = new HeadManager() | ||
const container = document.getElementById('__next') | ||
|
||
export default (onError) => { | ||
if (ids && ids.length) rehydrate(ids) | ||
|
||
router.subscribe(({ Component, props, err }) => { | ||
render({ Component, props, err }, onError) | ||
}) | ||
|
||
render({ Component, props, err }, onError) | ||
} | ||
|
||
export async function render (props, onError = renderErrorComponent) { | ||
try { | ||
await doRender(props) | ||
} catch (err) { | ||
await onError(err) | ||
} | ||
} | ||
|
||
async function renderErrorComponent (err) { | ||
const { pathname, query } = router | ||
const props = await getInitialProps(ErrorComponent, { err, pathname, query }) | ||
await doRender({ Component: ErrorComponent, props, err }) | ||
} | ||
|
||
async function doRender ({ Component, props, err }) { | ||
if (!props && Component && | ||
Component !== ErrorComponent && | ||
lastAppProps.Component === ErrorComponent) { | ||
// fetch props if ErrorComponent was replaced with a page component by HMR | ||
const { pathname, query } = router | ||
props = await getInitialProps(Component, { err, pathname, query }) | ||
} | ||
|
||
Component = Component || lastAppProps.Component | ||
props = props || lastAppProps.props | ||
|
||
const appProps = { Component, props, err, router, headManager } | ||
lastAppProps = appProps | ||
ReactDOM.render(createElement(App, appProps), container) | ||
} | ||
|
||
function getInitialProps (Component, ctx) { | ||
return Component.getInitialProps ? Component.getInitialProps(ctx) : {} | ||
} |
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 |
---|---|---|
@@ -1,59 +1,3 @@ | ||
import { createElement } from 'react' | ||
import ReactDOM from 'react-dom' | ||
import HeadManager from './head-manager' | ||
import { rehydrate } from '../lib/css' | ||
import { createRouter } from '../lib/router' | ||
import App from '../lib/app' | ||
import evalScript from '../lib/eval-script' | ||
import next from './' | ||
|
||
const { | ||
__NEXT_DATA__: { | ||
component, | ||
errorComponent, | ||
props, | ||
ids, | ||
err, | ||
pathname, | ||
query | ||
} | ||
} = window | ||
|
||
const Component = evalScript(component).default | ||
const ErrorComponent = evalScript(errorComponent).default | ||
|
||
export const router = createRouter(pathname, query, { | ||
Component, | ||
ErrorComponent, | ||
ctx: { err } | ||
}) | ||
|
||
const headManager = new HeadManager() | ||
const container = document.getElementById('__next') | ||
const defaultProps = { Component, ErrorComponent, props, router, headManager } | ||
|
||
if (ids && ids.length) rehydrate(ids) | ||
|
||
render() | ||
|
||
export function render (props = {}) { | ||
try { | ||
doRender(props) | ||
} catch (err) { | ||
renderError(err) | ||
} | ||
} | ||
|
||
export async function renderError (err) { | ||
const { pathname, query } = router | ||
const props = await ErrorComponent.getInitialProps({ err, pathname, query }) | ||
try { | ||
doRender({ Component: ErrorComponent, props }) | ||
} catch (err2) { | ||
console.error(err2) | ||
} | ||
} | ||
|
||
function doRender (props) { | ||
const appProps = { ...defaultProps, ...props } | ||
ReactDOM.render(createElement(App, appProps), container) | ||
} | ||
next() |
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,67 @@ | ||
import React from 'react' | ||
import ansiHTML from 'ansi-html' | ||
import Head from './head' | ||
|
||
export default ({ err: { name, message, stack, module } }) => ( | ||
<div style={styles.errorDebug}> | ||
<Head> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | ||
</Head> | ||
{module ? <div style={styles.heading}>Error in {module.rawRequest}</div> : null} | ||
{ | ||
name === 'ModuleBuildError' | ||
? <pre style={styles.message} dangerouslySetInnerHTML={{ __html: ansiHTML(encodeHtml(message)) }} /> | ||
: <pre style={styles.message}>{stack}</pre> | ||
} | ||
</div> | ||
) | ||
|
||
const styles = { | ||
errorDebug: { | ||
background: '#a6004c', | ||
boxSizing: 'border-box', | ||
overflow: 'auto', | ||
padding: '16px', | ||
position: 'fixed', | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
bottom: 0, | ||
zIndex: 9999 | ||
}, | ||
|
||
message: { | ||
fontFamily: '"SF Mono", "Roboto Mono", "Fira Mono", menlo-regular, monospace', | ||
fontSize: '10px', | ||
color: '#fbe7f1', | ||
margin: 0, | ||
whiteSpace: 'pre-wrap', | ||
wordWrap: 'break-word' | ||
}, | ||
|
||
heading: { | ||
fontFamily: '-apple-system, BlinkMacSystemFont, Roboto, "Segoe UI", "Fira Sans", Avenir, "Helvetica Neue", "Lucida Grande", sans-serif', | ||
fontSize: '13px', | ||
fontWeight: 'bold', | ||
color: '#ff84bf', | ||
marginBottom: '20px' | ||
} | ||
} | ||
|
||
const encodeHtml = str => { | ||
return str.replace(/</g, '<').replace(/>/g, '>') | ||
} | ||
|
||
// see color definitions of babel-code-frame: | ||
// https://github.com/babel/babel/blob/master/packages/babel-code-frame/src/index.js | ||
|
||
ansiHTML.setColors({ | ||
reset: ['fff', 'a6004c'], | ||
darkgrey: 'e54590', | ||
yellow: 'ee8cbb', | ||
green: 'f2a2c7', | ||
magenta: 'fbe7f1', | ||
blue: 'fff', | ||
cyan: 'ef8bb9', | ||
red: 'fff' | ||
}) |
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.
_error-debug
is not a page component anymore.