From 4f26e84ff2708fbfa9f7e99dbb915222b0472798 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Thu, 6 Apr 2017 12:19:00 +0530 Subject: [PATCH] Move webpack idle wait code to the page-loader. Because that's the place to do it. --- lib/page-loader.js | 45 +++++++++++++++++----------- lib/router/router.js | 16 ---------- server/build/plugins/pages-plugin.js | 6 ++-- server/render.js | 8 +++-- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/lib/page-loader.js b/lib/page-loader.js index 4b1b33a1..cdf69008 100644 --- a/lib/page-loader.js +++ b/lib/page-loader.js @@ -1,6 +1,8 @@ /* global window, document */ import mitt from 'mitt' +const webpackModule = module + export default class PageLoader { constructor (buildId) { this.buildId = buildId @@ -18,19 +20,6 @@ export default class PageLoader { return route.replace(/index$/, '') } - loadPageSync (route) { - route = this.normalizeRoute(route) - const cachedPage = this.pageCache[route] - - if (!cachedPage) { - return null - } else if (cachedPage.error) { - throw cachedPage.error - } else { - return cachedPage.page - } - } - loadPage (route) { route = this.normalizeRoute(route) @@ -79,12 +68,32 @@ export default class PageLoader { } // This method if called by the route code. - registerPage (route, error, page) { - route = this.normalizeRoute(route) + registerPage (route, regFn) { + const register = () => { + regFn((error, page) => { + route = this.normalizeRoute(route) - // add the page to the cache - this.pageCache[route] = { error, page } - this.registerEvents.emit(route, { error, page }) + // add the page to the cache + this.pageCache[route] = { error, page } + this.registerEvents.emit(route, { error, page }) + }) + } + + // Wait for webpack to became idle if it's not. + // More info: https://github.com/zeit/next.js/pull/1511 + if (webpackModule && webpackModule.hot && webpackModule.hot.status() !== 'idle') { + console.log(`Waiting webpack to became "idle" to initialize the page: "${route}"`) + + const check = (status) => { + if (status === 'idle') { + webpackModule.hot.removeStatusHandler(check) + register() + } + } + webpackModule.hot.status(check) + } else { + register() + } } clearCache (route) { diff --git a/lib/router/router.js b/lib/router/router.js index ea70df68..ec53fa22 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -5,8 +5,6 @@ import PQueue from '../p-queue' import { loadGetInitialProps, getURL } from '../utils' import { _notifyBuildIdMismatch } from './' -const webpackModule = module - export default class Router { constructor (pathname, query, as, { pageLoader, Component, ErrorComponent, err } = {}) { // represents the current component key @@ -320,20 +318,6 @@ export default class Router { } async fetchRoute (route) { - // Wait for webpack to became idle if it's not. - // More info: https://github.com/zeit/next.js/pull/1511 - if (webpackModule && webpackModule.hot && webpackModule.hot.status() !== 'idle') { - await new Promise((resolve) => { - const check = (status) => { - if (status === 'idle') { - webpackModule.hot.removeStatusHandler(check) - resolve() - } - } - webpackModule.hot.status(check) - }) - } - return await this.pageLoader.loadPage(route) } diff --git a/server/build/plugins/pages-plugin.js b/server/build/plugins/pages-plugin.js index 21791e63..32c0501a 100644 --- a/server/build/plugins/pages-plugin.js +++ b/server/build/plugins/pages-plugin.js @@ -17,8 +17,10 @@ export default class PagesPlugin { const content = page.source() const newContent = ` function loadPage () { - var comp = ${content} - window.__NEXT_PAGE_LOADER__.registerPage('${routeName}', null, comp.default) + window.__NEXT_PAGE_LOADER__.registerPage('${routeName}', function(cb) { + var comp = ${content} + cb(null, comp.default) + }) } if (window.__NEXT_PAGE_LOADER__) { diff --git a/server/render.js b/server/render.js index 69019975..2722e300 100644 --- a/server/render.js +++ b/server/render.js @@ -123,7 +123,9 @@ export async function renderScriptError (req, res, page, error, customFields, op var error = new Error('Page not exists: ${page}') error.pageNotFound = true error.statusCode = 404 - __NEXT_PAGE_LOADER__.registerPage('${page}', error) + __NEXT_PAGE_LOADER__.registerPage('${page}', function(cb) { + cb(error) + }) } if (window.__NEXT_PAGE_LOADER__) { @@ -145,7 +147,9 @@ export async function renderScriptError (req, res, page, error, customFields, op res.end(` function loadPage () { var error = ${JSON.stringify(errorJson)} - __NEXT_PAGE_LOADER__.registerPage('${page}', error) + __NEXT_PAGE_LOADER__.registerPage('${page}', function(cb) { + cb(error) + }) } if (window.__NEXT_PAGE_LOADER__) {