2019-02-22 17:33:28 +01:00
|
|
|
import mkdirpModule from 'mkdirp'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
import { extname, join, dirname, sep } from 'path'
|
2019-03-20 16:42:02 +01:00
|
|
|
import { cleanAmpPath } from 'next-server/dist/server/utils'
|
2019-02-22 17:33:28 +01:00
|
|
|
import { renderToHTML } from 'next-server/dist/server/render'
|
|
|
|
import { writeFile } from 'fs'
|
|
|
|
import Sema from 'async-sema'
|
2019-03-26 22:21:27 +01:00
|
|
|
import AmpHtmlValidator from 'amphtml-validator'
|
2019-02-22 17:33:28 +01:00
|
|
|
import { loadComponents } from 'next-server/dist/server/load-components'
|
|
|
|
|
|
|
|
const envConfig = require('next-server/config')
|
|
|
|
const mkdirp = promisify(mkdirpModule)
|
|
|
|
|
2018-12-12 13:59:11 +01:00
|
|
|
global.__NEXT_DATA__ = {
|
|
|
|
nextExport: true
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on(
|
|
|
|
'message',
|
|
|
|
async ({
|
2018-12-18 17:12:49 +01:00
|
|
|
distDir,
|
|
|
|
buildId,
|
2018-12-12 13:59:11 +01:00
|
|
|
exportPaths,
|
|
|
|
exportPathMap,
|
|
|
|
outDir,
|
|
|
|
renderOpts,
|
2019-02-12 02:28:47 +01:00
|
|
|
serverRuntimeConfig,
|
2018-12-12 13:59:11 +01:00
|
|
|
concurrency
|
|
|
|
}) => {
|
|
|
|
const sema = new Sema(concurrency, { capacity: exportPaths.length })
|
|
|
|
try {
|
|
|
|
const work = async path => {
|
|
|
|
await sema.acquire()
|
|
|
|
const { page, query = {} } = exportPathMap[path]
|
2019-04-02 20:01:34 +02:00
|
|
|
const ampOpts = { amphtml: query.amphtml, hasAmp: query.hasAmp, ampPath: query.ampPath }
|
|
|
|
const ampOnly = query.ampOnly
|
|
|
|
delete query.ampOnly
|
2019-03-20 04:53:47 +01:00
|
|
|
delete query.hasAmp
|
|
|
|
delete query.ampPath
|
2019-04-02 20:01:34 +02:00
|
|
|
delete query.amphtml
|
2019-03-20 04:53:47 +01:00
|
|
|
|
2018-12-12 13:59:11 +01:00
|
|
|
const req = { url: path }
|
|
|
|
const res = {}
|
2019-02-12 02:28:47 +01:00
|
|
|
envConfig.setConfig({
|
|
|
|
serverRuntimeConfig,
|
|
|
|
publicRuntimeConfig: renderOpts.runtimeConfig
|
|
|
|
})
|
2018-12-12 13:59:11 +01:00
|
|
|
|
2019-04-02 20:01:34 +02:00
|
|
|
if (ampOnly) {
|
2019-03-20 04:53:47 +01:00
|
|
|
path = cleanAmpPath(path)
|
2019-04-02 20:01:34 +02:00
|
|
|
ampOpts.ampPath = path + '.amp'
|
2019-03-20 04:53:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 19:17:04 +01:00
|
|
|
// replace /docs/index.amp with /docs.amp
|
|
|
|
path = path.replace(/(?<!^)\/index\.amp$/, '.amp')
|
|
|
|
|
2018-12-12 13:59:11 +01:00
|
|
|
let htmlFilename = `${path}${sep}index.html`
|
2019-03-13 00:30:03 +01:00
|
|
|
const pageExt = extname(page)
|
|
|
|
const pathExt = extname(path)
|
|
|
|
// Make sure page isn't a folder with a dot in the name e.g. `v1.2`
|
|
|
|
if (pageExt !== pathExt && pathExt !== '') {
|
2018-12-12 13:59:11 +01:00
|
|
|
// If the path has an extension, use that as the filename instead
|
|
|
|
htmlFilename = path
|
|
|
|
} else if (path === '/') {
|
|
|
|
// If the path is the root, just use index.html
|
|
|
|
htmlFilename = 'index.html'
|
|
|
|
}
|
|
|
|
const baseDir = join(outDir, dirname(htmlFilename))
|
|
|
|
const htmlFilepath = join(outDir, htmlFilename)
|
|
|
|
|
|
|
|
await mkdirp(baseDir)
|
2018-12-18 17:12:49 +01:00
|
|
|
const components = await loadComponents(distDir, buildId, page)
|
2019-03-20 04:53:47 +01:00
|
|
|
const html = await renderToHTML(req, res, page, query, { ...components, ...renderOpts, ...ampOpts })
|
2019-03-26 22:21:27 +01:00
|
|
|
|
2019-04-02 20:01:34 +02:00
|
|
|
if (ampOpts.amphtml && query.amp) {
|
2019-03-26 22:21:27 +01:00
|
|
|
const validator = await AmpHtmlValidator.getInstance()
|
|
|
|
const result = validator.validateString(html)
|
|
|
|
const errors = result.errors.filter(e => e.severity === 'ERROR')
|
|
|
|
const warnings = result.errors.filter(e => e.severity !== 'ERROR')
|
|
|
|
|
|
|
|
if (warnings.length || errors.length) {
|
|
|
|
process.send({
|
|
|
|
type: 'amp-validation',
|
|
|
|
payload: {
|
|
|
|
page,
|
|
|
|
result: {
|
|
|
|
errors,
|
|
|
|
warnings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:59:11 +01:00
|
|
|
await new Promise((resolve, reject) =>
|
|
|
|
writeFile(
|
|
|
|
htmlFilepath,
|
|
|
|
html,
|
|
|
|
'utf8',
|
|
|
|
err => (err ? reject(err) : resolve())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
process.send({ type: 'progress' })
|
|
|
|
sema.release()
|
|
|
|
}
|
|
|
|
await Promise.all(exportPaths.map(work))
|
|
|
|
process.send({ type: 'done' })
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
process.send({ type: 'error', payload: err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|