Commit 6e44cdef authored by Kevin Decker's avatar Kevin Decker Committed by Tim Neutkens

Expose mechanism to monitor render events (#2655)

* Allow custom clientBootstrap entrypoint

This allows for customization of the client global bootstrap.

* Expose render event emitter to client

* Include client entry point in npm package
parent 1e151058
module.exports = require('./dist/client/index')
...@@ -56,6 +56,8 @@ let ErrorDebugComponent ...@@ -56,6 +56,8 @@ let ErrorDebugComponent
let Component let Component
let stripAnsi = (s) => s let stripAnsi = (s) => s
export const emitter = new EventEmitter()
export default async ({ ErrorDebugComponent: passedDebugComponent, stripAnsi: passedStripAnsi } = {}) => { export default async ({ ErrorDebugComponent: passedDebugComponent, stripAnsi: passedStripAnsi } = {}) => {
// Wait for all the dynamic chunks to get loaded // Wait for all the dynamic chunks to get loaded
for (const chunkName of chunks) { for (const chunkName of chunks) {
...@@ -80,8 +82,6 @@ export default async ({ ErrorDebugComponent: passedDebugComponent, stripAnsi: pa ...@@ -80,8 +82,6 @@ export default async ({ ErrorDebugComponent: passedDebugComponent, stripAnsi: pa
err err
}) })
const emitter = new EventEmitter()
router.subscribe(({ Component, props, hash, err }) => { router.subscribe(({ Component, props, hash, err }) => {
render({ Component, props, err, hash, emitter }) render({ Component, props, err, hash, emitter })
}) })
...@@ -132,7 +132,7 @@ export async function renderError (error) { ...@@ -132,7 +132,7 @@ export async function renderError (error) {
} }
} }
async function doRender ({ Component, props, hash, err, emitter }) { async function doRender ({ Component, props, hash, err, emitter: emitterProp = emitter }) {
if (!props && Component && if (!props && Component &&
Component !== ErrorComponent && Component !== ErrorComponent &&
lastAppProps.Component === ErrorComponent) { lastAppProps.Component === ErrorComponent) {
...@@ -141,10 +141,6 @@ async function doRender ({ Component, props, hash, err, emitter }) { ...@@ -141,10 +141,6 @@ async function doRender ({ Component, props, hash, err, emitter }) {
props = await loadGetInitialProps(Component, { err, pathname, query, asPath }) props = await loadGetInitialProps(Component, { err, pathname, query, asPath })
} }
if (emitter) {
emitter.emit('before-reactdom-render', { Component, ErrorComponent })
}
Component = Component || lastAppProps.Component Component = Component || lastAppProps.Component
props = props || lastAppProps.props props = props || lastAppProps.props
...@@ -152,13 +148,13 @@ async function doRender ({ Component, props, hash, err, emitter }) { ...@@ -152,13 +148,13 @@ async function doRender ({ Component, props, hash, err, emitter }) {
// lastAppProps has to be set before ReactDom.render to account for ReactDom throwing an error. // lastAppProps has to be set before ReactDom.render to account for ReactDom throwing an error.
lastAppProps = appProps lastAppProps = appProps
emitterProp.emit('before-reactdom-render', { Component, ErrorComponent, appProps })
// We need to clear any existing runtime error messages // We need to clear any existing runtime error messages
ReactDOM.unmountComponentAtNode(errorContainer) ReactDOM.unmountComponentAtNode(errorContainer)
renderReactElement(createElement(App, appProps), appContainer) renderReactElement(createElement(App, appProps), appContainer)
if (emitter) { emitterProp.emit('after-reactdom-render', { Component, ErrorComponent, appProps })
emitter.emit('after-reactdom-render', { Component, ErrorComponent })
}
} }
let isInitialRender = true let isInitialRender = true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"files": [ "files": [
"dist", "dist",
"babel.js", "babel.js",
"client.js",
"link.js", "link.js",
"css.js", "css.js",
"head.js", "head.js",
......
...@@ -43,6 +43,7 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet ...@@ -43,6 +43,7 @@ export default async function createCompiler (dir, { buildId, dev = false, quiet
const entries = { const entries = {
'main.js': [ 'main.js': [
...defaultEntries, ...defaultEntries,
...config.clientBootstrap || [],
mainJS mainJS
] ]
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment