Commit b8e8228e authored by Tim Neutkens's avatar Tim Neutkens

Compile build-manifest per-page

parent 77a138cc
...@@ -3,7 +3,6 @@ export const PHASE_PRODUCTION_BUILD = 'phase-production-build' ...@@ -3,7 +3,6 @@ export const PHASE_PRODUCTION_BUILD = 'phase-production-build'
export const PHASE_PRODUCTION_SERVER = 'phase-production-server' export const PHASE_PRODUCTION_SERVER = 'phase-production-server'
export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server' export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'
export const PAGES_MANIFEST = 'pages-manifest.json' export const PAGES_MANIFEST = 'pages-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json'
export const SERVER_DIRECTORY = 'server' export const SERVER_DIRECTORY = 'server'
export const CONFIG_FILE = 'next.config.js' export const CONFIG_FILE = 'next.config.js'
export const BUILD_ID_FILE = 'BUILD_ID' export const BUILD_ID_FILE = 'BUILD_ID'
......
...@@ -13,7 +13,7 @@ import Loadable from '../lib/loadable' ...@@ -13,7 +13,7 @@ import Loadable from '../lib/loadable'
import LoadableCapture from '../lib/loadable-capture' import LoadableCapture from '../lib/loadable-capture'
import {promisify} from 'util' import {promisify} from 'util'
import fs from 'fs' import fs from 'fs'
import { BUILD_MANIFEST, SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH } from 'next-server/constants' import { SERVER_DIRECTORY, CLIENT_STATIC_FILES_PATH } from 'next-server/constants'
const access = promisify(fs.access) const access = promisify(fs.access)
...@@ -49,17 +49,6 @@ export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) { ...@@ -49,17 +49,6 @@ export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
return doRender(req, res, pathname, query, { ...opts, err, page: '/_error' }) return doRender(req, res, pathname, query, { ...opts, err, page: '/_error' })
} }
function getPageFiles (buildManifest, normalizedPagePath) {
const files = buildManifest.pages[normalizedPagePath]
if (!files) {
console.warn(`Could not find files for ${normalizedPagePath} in .next/build-manifest.json`)
return []
}
return files
}
async function doRender (req, res, pathname, query, { async function doRender (req, res, pathname, query, {
err, err,
page, page,
...@@ -107,7 +96,7 @@ async function doRender (req, res, pathname, query, { ...@@ -107,7 +96,7 @@ async function doRender (req, res, pathname, query, {
Component = Component.default || Component Component = Component.default || Component
const reactLoadableManifest = require(pagePath + '-loadable.json') const reactLoadableManifest = require(pagePath + '-loadable.json')
const buildManifest = require(join(distDir, BUILD_MANIFEST)) const buildManifest = require(pagePath + '-assets.json')
if (typeof Component !== 'function') { if (typeof Component !== 'function') {
throw new Error(`The default export is not a React Component in page: "${page}"`) throw new Error(`The default export is not a React Component in page: "${page}"`)
...@@ -117,13 +106,7 @@ async function doRender (req, res, pathname, query, { ...@@ -117,13 +106,7 @@ async function doRender (req, res, pathname, query, {
const ctx = { err, req, res, pathname, query, asPath } const ctx = { err, req, res, pathname, query, asPath }
const router = new Router(pathname, query, asPath) const router = new Router(pathname, query, asPath)
const props = await loadGetInitialProps(App, {Component, router, ctx}) const props = await loadGetInitialProps(App, {Component, router, ctx})
const files = [ const files = buildManifest.assets
...new Set([
...getPageFiles(buildManifest, normalizedPagePath),
...getPageFiles(buildManifest, '/_app'),
...getPageFiles(buildManifest, '/_error')
])
]
// the response might be finshed on the getinitialprops call // the response might be finshed on the getinitialprops call
if (isResSent(res)) return if (isResSent(res)) return
......
// @flow // @flow
import { RawSource } from 'webpack-sources' import path from 'path'
import {BUILD_MANIFEST, ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants' import {ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants'
// This plugin creates a build-manifest.json for all assets that are being output // This plugin creates a build-manifest.json for all assets that are being output
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production // It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
export default class BuildManifestPlugin { export default class BuildManifestPlugin {
apply (compiler: any) { apply (compiler: any) {
compiler.hooks.emit.tapAsync('NextJsBuildManifest', (compilation, callback) => { compiler.hooks.emit.tapPromise('NextJsBuildManifest', async (compilation, callback) => {
const {chunks} = compilation const {chunks} = compilation
const assetMap = {devFiles: [], pages: {}} const assetMap = {}
const mainJsChunk = chunks.find((c) => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN) const mainJsChunk = chunks.find((c) => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN)
const mainJsFiles = mainJsChunk && mainJsChunk.files.length > 0 ? mainJsChunk.files.filter((file) => /\.js$/.test(file)) : [] const mainJsFiles = mainJsChunk && mainJsChunk.files.length > 0 ? mainJsChunk.files.filter((file) => /\.js$/.test(file)) : []
for (const filePath of Object.keys(compilation.assets)) {
const path = filePath.replace(/\\/g, '/')
if (/^static\/development\/dll\//.test(path)) {
assetMap.devFiles.push(path)
}
}
// compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value // compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value
for (const [, entrypoint] of compilation.entrypoints.entries()) { for (const [, entrypoint] of compilation.entrypoints.entries()) {
const result = ROUTE_NAME_REGEX.exec(entrypoint.name) const result = ROUTE_NAME_REGEX.exec(entrypoint.name)
...@@ -59,15 +52,41 @@ export default class BuildManifestPlugin { ...@@ -59,15 +52,41 @@ export default class BuildManifestPlugin {
} }
} }
assetMap.pages[`/${pagePath.replace(/\\/g, '/')}`] = [...filesForEntry, ...mainJsFiles] assetMap[pagePath.replace(/\\/g, '/')] = [...filesForEntry, ...mainJsFiles]
} }
if (typeof assetMap.pages['/index'] !== 'undefined') { for (const [, entrypoint] of compilation.entrypoints.entries()) {
assetMap.pages['/'] = assetMap.pages['/index'] const manifestFile = path.join('server', entrypoint.name.replace(/\.js$/, '-assets.json'))
}
const result = ROUTE_NAME_REGEX.exec(entrypoint.name)
if (!result) {
continue
}
compilation.assets[BUILD_MANIFEST] = new RawSource(JSON.stringify(assetMap, null, 2)) const pagePath = result[1]
callback()
if (!pagePath) {
continue
}
const assets = [
...new Set([
...(assetMap['/_app'] || []),
...(assetMap['/_error'] || []),
...(assetMap[pagePath.replace(/\\/g, '/')] || [])
])
]
const json = JSON.stringify({assets})
compilation.assets[manifestFile] = {
source () {
return json
},
size () {
return json.length
}
}
}
}) })
} }
} }
/* global describe, test, it, expect */ /* global describe, test, it, expect */
import cheerio from 'cheerio' import cheerio from 'cheerio'
import {BUILD_MANIFEST} from 'next-server/constants'
import { join } from 'path' import { join } from 'path'
export default function ({ app }, suiteName, render, fetch, appPort) { export default function ({ app }, suiteName, render, fetch, appPort) {
...@@ -172,7 +171,7 @@ export default function ({ app }, suiteName, render, fetch, appPort) { ...@@ -172,7 +171,7 @@ export default function ({ app }, suiteName, render, fetch, appPort) {
// build dynamic page // build dynamic page
await fetch('/dynamic/ssr') await fetch('/dynamic/ssr')
const buildManifest = require(join('../.next', BUILD_MANIFEST)) const buildManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-assets.json'))
const reactLoadableManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-loadable.json')) const reactLoadableManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-loadable.json'))
const resources = [] const resources = []
......
...@@ -16,7 +16,6 @@ import fetch from 'node-fetch' ...@@ -16,7 +16,6 @@ import fetch from 'node-fetch'
import dynamicImportTests from './dynamic' import dynamicImportTests from './dynamic'
import processEnv from './process-env' import processEnv from './process-env'
import security from './security' import security from './security'
import {BUILD_MANIFEST} from 'next-server/constants'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
...@@ -78,7 +77,7 @@ describe('Production Usage', () => { ...@@ -78,7 +77,7 @@ describe('Production Usage', () => {
it('should set Cache-Control header', async () => { it('should set Cache-Control header', async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8') const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const buildManifest = require(join('../.next', BUILD_MANIFEST)) const buildManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-assets.json'))
const reactLoadableManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-loadable.json')) const reactLoadableManifest = require(join('../.next', 'server', 'static', buildId, 'pages', 'dynamic', 'ssr-loadable.json'))
const url = `http://localhost:${appPort}/_next/` const url = `http://localhost:${appPort}/_next/`
......
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