diff --git a/client/index.js b/client/index.js index 540e669c6e7e728c61e8099fa41fa4748d121f37..073274eafd892985e2c495ce5ab4bb8bd416bfab 100644 --- a/client/index.js +++ b/client/index.js @@ -28,11 +28,13 @@ const { location } = window -const pageLoader = window.__NEXT_PAGE_LOADER__ = new PageLoader(buildId) -if (window.__NEXT_LOADED_PAGES__) { - window.__NEXT_LOADED_PAGES__.forEach((fn) => fn()) - delete window.__NEXT_LOADED_PAGES__ -} +const pageLoader = new PageLoader(buildId) +window.__NEXT_LOADED_PAGES__.forEach(({ route, fn }) => { + pageLoader.registerPage(route, fn) +}) +delete window.__NEXT_LOADED_PAGES__ + +window.__NEXT_REGISTER_PAGE = pageLoader.registerPage.bind(pageLoader) const headManager = new HeadManager() const appContainer = document.getElementById('__next') diff --git a/lib/page-loader.js b/lib/page-loader.js index bf66f234d650b4f73f17d757f981b748f64b685e..866216fe89faf355f70045fe85c16a79a3337818 100644 --- a/lib/page-loader.js +++ b/lib/page-loader.js @@ -71,13 +71,12 @@ export default class PageLoader { // This method if called by the route code. registerPage (route, regFn) { - const register = () => { - regFn((error, page) => { - route = this.normalizeRoute(route) + route = this.normalizeRoute(route) - // add the page to the cache - this.pageCache[route] = { error, page } - }) + const register = () => { + // add the page to the cache + const { error, page } = regFn() + this.pageCache[route] = { error, page } } // Wait for webpack to became idle if it's not. diff --git a/server/build/plugins/pages-plugin.js b/server/build/plugins/pages-plugin.js index 32c0501af34b274eaccd9cf09b546b98f9569bd6..203fbe60134356b8e6538a835c388128e24a3c6a 100644 --- a/server/build/plugins/pages-plugin.js +++ b/server/build/plugins/pages-plugin.js @@ -16,19 +16,10 @@ export default class PagesPlugin { const content = page.source() const newContent = ` - function loadPage () { - window.__NEXT_PAGE_LOADER__.registerPage('${routeName}', function(cb) { - var comp = ${content} - cb(null, comp.default) - }) - } - - if (window.__NEXT_PAGE_LOADER__) { - loadPage() - } else { - window.__NEXT_LOADED_PAGES__ = window.__NEXT_LOADED_PAGES__ || [] - window.__NEXT_LOADED_PAGES__.push(loadPage) - } + window.__NEXT_REGISTER_PAGE('${routeName}', function() { + var comp = ${content} + return { page: comp.default } + }) ` // Replace the exisiting chunk with the new content compilation.assets[chunk.name] = { diff --git a/server/document.js b/server/document.js index 15e99a4417f0a5ea2c8ad9a3c76a5dc1c9af5116..a547c2ea211fbabec69947bdcfb5c45d5a4a8d51 100644 --- a/server/document.js +++ b/server/document.js @@ -101,7 +101,15 @@ export class NextScript extends Component { return
{staticMarkup ? null :