From 3b3a4e5dcd5f7a9438c2bb81afa0a27f97438a75 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 3 Mar 2017 11:51:52 +0530 Subject: [PATCH 01/11] Use file hashes instead of BUILD_ID. Now JSON pages also not prefixed with a hash and doesn't support immutable caching. Instead it supports Etag bases caching. --- lib/router/index.js | 10 +--------- lib/router/router.js | 12 +----------- server/build/index.js | 19 ++++++++++++++++-- server/document.js | 6 +++--- server/index.js | 45 ++++++++++++++++++------------------------- server/render.js | 4 ++-- 6 files changed, 43 insertions(+), 53 deletions(-) diff --git a/lib/router/index.js b/lib/router/index.js index 550ef110d3e2a..de3ac9ba6935f 100644 --- a/lib/router/index.js +++ b/lib/router/index.js @@ -1,4 +1,4 @@ -/* global window, location */ +/* global window */ import _Router from './router' const SingletonRouter = { @@ -76,11 +76,3 @@ export const createRouter = function (...args) { // Export the actual Router class, which is usually used inside the server export const Router = _Router - -export function _notifyBuildIdMismatch (nextRoute) { - if (SingletonRouter.onAppUpdated) { - SingletonRouter.onAppUpdated(nextRoute) - } else { - location.href = nextRoute - } -} diff --git a/lib/router/router.js b/lib/router/router.js index 518d1faff7746..ebbf9fb5b7420 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -4,7 +4,6 @@ import evalScript from '../eval-script' import shallowEquals from '../shallow-equals' import PQueue from '../p-queue' import { loadGetInitialProps, getURL } from '../utils' -import { _notifyBuildIdMismatch } from './' import fetch from 'unfetch' if (typeof window !== 'undefined' && typeof navigator.serviceWorker !== 'undefined') { @@ -260,14 +259,6 @@ export default class Router extends EventEmitter { const jsonPageRes = await this.fetchRoute(route) const jsonData = await jsonPageRes.json() - if (jsonData.buildIdMismatch) { - _notifyBuildIdMismatch(as) - - const error = Error('Abort due to BUILD_ID mismatch') - error.cancelled = true - throw error - } - const newData = { ...loadComponent(jsonData), jsonPageRes @@ -320,8 +311,7 @@ export default class Router extends EventEmitter { } doFetchRoute (route) { - const { buildId } = window.__NEXT_DATA__ - const url = `/_next/${encodeURIComponent(buildId)}/pages${route}` + const url = `/_next/pages${route}` return fetch(url, { method: 'GET', headers: { 'Accept': 'application/json' } diff --git a/server/build/index.js b/server/build/index.js index 65dcf31da8f44..0fcff294423b3 100644 --- a/server/build/index.js +++ b/server/build/index.js @@ -11,7 +11,8 @@ export default async function build (dir) { const compiler = await webpack(dir, { buildDir }) try { - await runCompiler(compiler) + const webpackStats = await runCompiler(compiler) + await writeBuildStats(buildDir, webpackStats) await writeBuildId(buildDir) } catch (err) { console.error(`> Failed to build on ${buildDir}`) @@ -30,6 +31,7 @@ function runCompiler (compiler) { if (err) return reject(err) const jsonStats = stats.toJson() + if (jsonStats.errors.length > 0) { const error = new Error(jsonStats.errors[0]) error.errors = jsonStats.errors @@ -37,11 +39,24 @@ function runCompiler (compiler) { return reject(error) } - resolve() + resolve(jsonStats) }) }) } +async function writeBuildStats (dir, webpackStats) { + const chunkHashMap = {} + webpackStats.chunks + // We are not interested about pages + .filter(({ files }) => !/^bundles/.test(files[0])) + .forEach(({ hash, files }) => { + chunkHashMap[files[0]] = { hash } + }) + + const buildStatsPath = join(dir, '.next', 'build-stats.json') + await fs.writeFile(buildStatsPath, JSON.stringify(chunkHashMap), 'utf8') +} + async function writeBuildId (dir) { const buildIdPath = join(dir, '.next', 'BUILD_ID') const buildId = uuid.v4() diff --git a/server/document.js b/server/document.js index 588da328f7b8d..bef4cd71218de 100644 --- a/server/document.js +++ b/server/document.js @@ -61,14 +61,14 @@ export class NextScript extends Component { render () { const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps - let { buildId } = __NEXT_DATA__ + let { buildStats } = __NEXT_DATA__ return
{staticMarkup ? null :