Unverified Commit a2553bb4 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Remove registration code from initial markup (#5583)

Clears the way a bit for #4943, also makes _document.js less complex, and will allow us to move `__NEXT_DATA__` to a `application/json` script tag.

Also this causes a slightly smaller bundle size 😌 
parent a1bdbad2
...@@ -41,10 +41,10 @@ export default class PagesPlugin { ...@@ -41,10 +41,10 @@ export default class PagesPlugin {
routeName = `/${routeName.replace(/(^|\/)index$/, '')}` routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
const source = new ConcatSource( const source = new ConcatSource(
`__NEXT_REGISTER_PAGE('${routeName}', function() {\n`, `(window.__NEXT_P=window.__NEXT_P||[]).push(['${routeName}', function() {\n`,
moduleSourcePostModule, moduleSourcePostModule,
'\nreturn { page: module.exports.default }', '\nreturn { page: module.exports.default }',
'});' '}]);'
) )
return source return source
......
...@@ -48,11 +48,12 @@ envConfig.setConfig({ ...@@ -48,11 +48,12 @@ envConfig.setConfig({
const asPath = getURL() const asPath = getURL()
const pageLoader = new PageLoader(buildId, prefix) const pageLoader = new PageLoader(buildId, prefix)
window.__NEXT_LOADED_PAGES__.forEach(([r, f]) => { const register = ([r, f]) => pageLoader.registerPage(r, f)
pageLoader.registerPage(r, f) if (window.__NEXT_P) {
}) window.__NEXT_P.map(register)
delete window.__NEXT_LOADED_PAGES__ }
window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.bind(pageLoader) window.__NEXT_P = []
window.__NEXT_P.push = register
const headManager = new HeadManager() const headManager = new HeadManager()
const appContainer = document.getElementById('__next') const appContainer = document.getElementById('__next')
......
...@@ -187,7 +187,7 @@ export class NextScript extends Component { ...@@ -187,7 +187,7 @@ export class NextScript extends Component {
static getInlineScriptSource (documentProps) { static getInlineScriptSource (documentProps) {
const { __NEXT_DATA__ } = documentProps const { __NEXT_DATA__ } = documentProps
const { page } = __NEXT_DATA__ const { page } = __NEXT_DATA__
return `__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])};` return `__NEXT_DATA__ = ${htmlescape(__NEXT_DATA__)};`
} }
render () { render () {
......
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