Commit 59b328b5 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Remove client bundles for AMP only pages (#7034)

* Remove client bundles for AMP pages
after build since they are not used

* Remove trailing white space

* Use async-sema to limit removing AMP client bundles

* Bring AMP client bundle removing
semaphore concurrency down to 20

* Don't check blocked pages when
deleting AMP client bundles

* Update client bundle removing for AMP pages

* Add error handling for removing client AMP pages

* rethrow error unless ENOENT during
deleting AMP client pages

* Handle error during removing AMP client
pages the same during dev

* Fix throwing instead of rejecting

* Make sure next/config is set before requiring page

* Update error check

* return on reject

* Fix next/config
parent 5054e943
...@@ -27,6 +27,7 @@ export function withAmp( ...@@ -27,6 +27,7 @@ export function withAmp(
return React.createElement(Component, props) return React.createElement(Component, props)
} }
WithAmpWrapper.__nextAmpOnly = !hybrid
WithAmpWrapper.getInitialProps = Component.getInitialProps WithAmpWrapper.getInitialProps = Component.getInitialProps
return WithAmpWrapper return WithAmpWrapper
} }
import chalk from 'chalk' import chalk from 'chalk'
import { PHASE_PRODUCTION_BUILD } from 'next-server/constants' import { PHASE_PRODUCTION_BUILD, BLOCKED_PAGES } from 'next-server/constants'
import loadConfig from 'next-server/next-config' import loadConfig from 'next-server/next-config'
import nanoid from 'next/dist/compiled/nanoid/index.js' import nanoid from 'next/dist/compiled/nanoid/index.js'
import Sema from 'async-sema'
import path from 'path' import path from 'path'
import fs from 'fs'
import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages' import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
...@@ -20,6 +22,9 @@ import { ...@@ -20,6 +22,9 @@ import {
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import { promisify } from 'util'
const unlink = promisify(fs.unlink)
export default async function build(dir: string, conf = null): Promise<void> { export default async function build(dir: string, conf = null): Promise<void> {
if (!(await isWriteable(dir))) { if (!(await isWriteable(dir))) {
...@@ -191,6 +196,37 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -191,6 +196,37 @@ export default async function build(dir: string, conf = null): Promise<void> {
result = formatWebpackMessages(result) result = formatWebpackMessages(result)
const pages = Object.keys(mappedPages)
const sema = new Sema(20, { capacity: pages.length })
await Promise.all(pages.map(async page => {
await sema.acquire()
page = page === '/' ? '/index' : page
if (BLOCKED_PAGES.includes(page)) {
return sema.release()
}
const serverPage = path.join(distDir, config.target === 'serverless' ? 'serverless/pages' : `server/static/${buildId}/pages`, page + '.js')
const clientPage = path.join(distDir, 'static', buildId, 'pages', page + '.js')
try {
require('next/config').setConfig({
publicRuntimeConfig: config.publicRuntimeConfig,
serverRuntimeConfig: config.serverRuntimeConfig
})
let mod = require(serverPage)
mod = mod.default || mod
if (mod && mod.__nextAmpOnly) {
await unlink(clientPage)
}
} catch (err) {
if (err.code !== 'ENOENT' && err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}
sema.release()
}))
if (isFlyingShuttle) { if (isFlyingShuttle) {
console.log() console.log()
} }
......
...@@ -35,8 +35,8 @@ const unlinkFile = async (p: string, t = 1): Promise<void> => { ...@@ -35,8 +35,8 @@ const unlinkFile = async (p: string, t = 1): Promise<void> => {
* Recursively delete directory contents * Recursively delete directory contents
* @param {string} dir Directory to delete the contents of * @param {string} dir Directory to delete the contents of
* @param {RegExp} [filter] Filter for the relative file path * @param {RegExp} [filter] Filter for the relative file path
* @param {boolean} [ensure] Esures that parameter dir exists, this is not passed recursively * @param {boolean} [ensure] Ensures that parameter dir exists, this is not passed recursively
* @param {string} [previousPath] Esures that parameter dir exists, this is not passed recursively * @param {string} [previousPath] Ensures that parameter dir exists, this is not passed recursively
* @returns Promise void * @returns Promise void
*/ */
export async function recursiveDelete( export async function recursiveDelete(
......
...@@ -341,8 +341,11 @@ export default class HotReloader { ...@@ -341,8 +341,11 @@ export default class HotReloader {
const onDemandEntries = onDemandEntryHandler(webpackDevMiddleware, multiCompiler, { const onDemandEntries = onDemandEntryHandler(webpackDevMiddleware, multiCompiler, {
dir: this.dir, dir: this.dir,
buildId: this.buildId, buildId: this.buildId,
distDir: this.config.distDir,
reload: this.reload.bind(this), reload: this.reload.bind(this),
pageExtensions: this.config.pageExtensions, pageExtensions: this.config.pageExtensions,
publicRuntimeConfig: this.config.publicRuntimeConfig,
serverRuntimeConfig: this.config.serverRuntimeConfig,
...this.config.onDemandEntries ...this.config.onDemandEntries
}) })
......
...@@ -2,6 +2,7 @@ import DynamicEntryPlugin from 'webpack/lib/DynamicEntryPlugin' ...@@ -2,6 +2,7 @@ import DynamicEntryPlugin from 'webpack/lib/DynamicEntryPlugin'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { join, posix } from 'path' import { join, posix } from 'path'
import { parse } from 'url' import { parse } from 'url'
import fs from 'fs'
import { pageNotFoundError } from 'next-server/dist/server/require' import { pageNotFoundError } from 'next-server/dist/server/require'
import { normalizePagePath } from 'next-server/dist/server/normalize-page-path' import { normalizePagePath } from 'next-server/dist/server/normalize-page-path'
import { ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX } from 'next-server/constants' import { ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX } from 'next-server/constants'
...@@ -28,10 +29,13 @@ function addEntry (compilation, context, name, entry) { ...@@ -28,10 +29,13 @@ function addEntry (compilation, context, name, entry) {
export default function onDemandEntryHandler (devMiddleware, multiCompiler, { export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
buildId, buildId,
dir, dir,
distDir,
reload, reload,
pageExtensions, pageExtensions,
maxInactiveAge, maxInactiveAge,
pagesBufferLength pagesBufferLength,
publicRuntimeConfig,
serverRuntimeConfig
}) { }) {
const pagesDir = join(dir, 'pages') const pagesDir = join(dir, 'pages')
const clients = new Map() const clients = new Map()
...@@ -261,6 +265,24 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, { ...@@ -261,6 +265,24 @@ export default function onDemandEntryHandler (devMiddleware, multiCompiler, {
function handleCallback (err) { function handleCallback (err) {
if (err) return reject(err) if (err) return reject(err)
const { name } = entries[normalizedPage]
let serverPage = join(dir, distDir, 'server', name)
const clientPage = join(dir, distDir, name)
try {
require('next/config').setConfig({
serverRuntimeConfig,
publicRuntimeConfig
})
let mod = require(serverPage)
mod = mod.default || mod
if (mod && mod.__nextAmpOnly) {
fs.unlinkSync(clientPage)
}
} catch (err) {
if (err.code !== 'ENOENT' && err.code !== 'MODULE_NOT_FOUND') {
return reject(err)
}
}
resolve() resolve()
} }
}) })
......
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