2016-10-19 12:58:08 +00:00
|
|
|
import { join } from 'path'
|
2016-10-05 23:52:50 +00:00
|
|
|
import { createElement } from 'react'
|
|
|
|
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
|
2016-12-31 12:46:23 +00:00
|
|
|
import send from 'send'
|
2017-04-18 15:45:42 +00:00
|
|
|
import generateETag from 'etag'
|
|
|
|
import fresh from 'fresh'
|
2018-02-13 13:27:52 +00:00
|
|
|
import requirePage from './require'
|
2016-12-20 17:27:43 +00:00
|
|
|
import { Router } from '../lib/router'
|
2018-01-31 13:12:21 +00:00
|
|
|
import { loadGetInitialProps, isResSent } from '../lib/utils'
|
2017-12-27 18:59:17 +00:00
|
|
|
import { getAvailableChunks } from './utils'
|
2016-12-16 18:42:40 +00:00
|
|
|
import Head, { defaultHead } from '../lib/head'
|
2017-04-01 21:03:40 +00:00
|
|
|
import ErrorDebug from '../lib/error-debug'
|
2017-04-17 20:15:50 +00:00
|
|
|
import { flushChunks } from '../lib/dynamic'
|
2018-05-31 18:56:04 +00:00
|
|
|
import { BUILD_MANIFEST, SERVER_DIRECTORY } from '../lib/constants'
|
2018-04-18 16:18:06 +00:00
|
|
|
import { applySourcemaps } from './lib/source-map-support'
|
2018-02-21 17:41:25 +00:00
|
|
|
|
|
|
|
const logger = console
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
export async function render (req, res, pathname, query, opts) {
|
2017-06-23 23:08:05 +00:00
|
|
|
const html = await renderToHTML(req, res, pathname, query, opts)
|
2017-05-25 16:28:08 +00:00
|
|
|
sendHTML(req, res, html, req.method, opts)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderToHTML (req, res, pathname, query, opts) {
|
|
|
|
return doRender(req, res, pathname, query, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function renderError (err, req, res, pathname, query, opts) {
|
|
|
|
const html = await renderErrorToHTML(err, req, res, query, opts)
|
2017-05-25 16:28:08 +00:00
|
|
|
sendHTML(req, res, html, req.method, opts)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderErrorToHTML (err, req, res, pathname, query, opts = {}) {
|
2018-02-13 15:13:22 +00:00
|
|
|
return doRender(req, res, pathname, query, { ...opts, err, page: '/_error' })
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function doRender (req, res, pathname, query, {
|
|
|
|
err,
|
|
|
|
page,
|
2017-01-11 20:16:18 +00:00
|
|
|
buildId,
|
2017-02-26 19:45:16 +00:00
|
|
|
hotReloader,
|
2017-04-18 04:18:43 +00:00
|
|
|
assetPrefix,
|
2018-02-26 11:03:27 +00:00
|
|
|
runtimeConfig,
|
2017-06-08 17:41:22 +00:00
|
|
|
availableChunks,
|
2018-06-04 13:45:39 +00:00
|
|
|
distDir,
|
|
|
|
dir,
|
2016-10-10 04:24:30 +00:00
|
|
|
dev = false,
|
2017-05-09 01:20:50 +00:00
|
|
|
staticMarkup = false,
|
|
|
|
nextExport = false
|
2016-10-10 04:24:30 +00:00
|
|
|
} = {}) {
|
2016-12-16 20:33:08 +00:00
|
|
|
page = page || pathname
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-04-18 16:18:06 +00:00
|
|
|
await applySourcemaps(err)
|
|
|
|
|
2018-02-13 13:27:52 +00:00
|
|
|
if (hotReloader) { // In dev mode we use on demand entries to compile the page before rendering
|
|
|
|
await ensurePage(page, { dir, hotReloader })
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-06-04 13:45:39 +00:00
|
|
|
const documentPath = join(distDir, SERVER_DIRECTORY, 'bundles', 'pages', '_document')
|
|
|
|
const appPath = join(distDir, SERVER_DIRECTORY, 'bundles', 'pages', '_app')
|
|
|
|
const buildManifest = require(join(distDir, BUILD_MANIFEST))
|
2018-04-12 08:33:22 +00:00
|
|
|
let [Component, Document, App] = await Promise.all([
|
2018-06-04 13:45:39 +00:00
|
|
|
requirePage(page, {distDir}),
|
2018-04-12 08:33:22 +00:00
|
|
|
require(documentPath),
|
|
|
|
require(appPath)
|
2018-03-09 16:14:30 +00:00
|
|
|
])
|
2018-04-12 08:33:22 +00:00
|
|
|
|
2016-12-16 18:42:40 +00:00
|
|
|
Component = Component.default || Component
|
2018-04-12 08:33:22 +00:00
|
|
|
|
|
|
|
if (typeof Component !== 'function') {
|
|
|
|
throw new Error(`The default export is not a React Component in page: "${pathname}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
App = App.default || App
|
2016-12-16 18:42:40 +00:00
|
|
|
Document = Document.default || Document
|
2017-05-03 16:40:09 +00:00
|
|
|
const asPath = req.url
|
|
|
|
const ctx = { err, req, res, pathname, query, asPath }
|
2018-04-12 08:33:22 +00:00
|
|
|
const router = new Router(pathname, query, asPath)
|
|
|
|
const props = await loadGetInitialProps(App, {Component, router, ctx})
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
// the response might be finshed on the getinitialprops call
|
2018-01-31 13:12:21 +00:00
|
|
|
if (isResSent(res)) return
|
2016-12-16 20:33:08 +00:00
|
|
|
|
2017-08-21 21:15:14 +00:00
|
|
|
const renderPage = (enhancer = Page => Page) => {
|
2016-10-06 02:42:55 +00:00
|
|
|
const app = createElement(App, {
|
2017-08-21 21:15:14 +00:00
|
|
|
Component: enhancer(Component),
|
2018-04-12 08:33:22 +00:00
|
|
|
router,
|
|
|
|
...props
|
2016-10-06 02:42:55 +00:00
|
|
|
})
|
2016-12-20 17:27:43 +00:00
|
|
|
|
2016-12-21 15:57:25 +00:00
|
|
|
const render = staticMarkup ? renderToStaticMarkup : renderToString
|
|
|
|
|
|
|
|
let html
|
|
|
|
let head
|
2017-04-01 21:03:40 +00:00
|
|
|
let errorHtml = ''
|
2017-10-21 23:47:55 +00:00
|
|
|
|
2016-12-21 15:57:25 +00:00
|
|
|
try {
|
2017-10-21 23:47:55 +00:00
|
|
|
if (err && dev) {
|
|
|
|
errorHtml = render(createElement(ErrorDebug, { error: err }))
|
|
|
|
} else if (err) {
|
2018-04-18 16:18:06 +00:00
|
|
|
html = render(app)
|
2017-10-21 23:47:55 +00:00
|
|
|
} else {
|
|
|
|
html = render(app)
|
|
|
|
}
|
2016-12-21 15:57:25 +00:00
|
|
|
} finally {
|
|
|
|
head = Head.rewind() || defaultHead()
|
|
|
|
}
|
2018-06-04 13:45:39 +00:00
|
|
|
const chunks = loadChunks({ dev, distDir, availableChunks })
|
2017-04-01 21:03:40 +00:00
|
|
|
|
2018-04-12 07:47:42 +00:00
|
|
|
return { html, head, errorHtml, chunks, buildManifest }
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
2016-10-06 02:42:55 +00:00
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
const docProps = await loadGetInitialProps(Document, { ...ctx, renderPage })
|
2016-12-16 18:42:40 +00:00
|
|
|
|
2018-01-31 13:12:21 +00:00
|
|
|
if (isResSent(res)) return
|
2017-01-26 11:06:50 +00:00
|
|
|
|
2018-06-24 15:10:24 +00:00
|
|
|
if (!Document.prototype || !Document.prototype.isReactComponent) throw new Error('_document.js is not exporting a React component')
|
2016-10-05 23:52:50 +00:00
|
|
|
const doc = createElement(Document, {
|
2016-12-16 18:42:40 +00:00
|
|
|
__NEXT_DATA__: {
|
2016-10-09 09:50:41 +00:00
|
|
|
props,
|
2018-02-09 15:55:45 +00:00
|
|
|
page, // the rendered page
|
|
|
|
pathname, // the requested path
|
2016-12-16 20:33:08 +00:00
|
|
|
query,
|
2017-10-30 14:57:35 +00:00
|
|
|
buildId,
|
2017-04-18 04:18:43 +00:00
|
|
|
assetPrefix,
|
2018-02-26 11:03:27 +00:00
|
|
|
runtimeConfig,
|
2017-05-09 01:20:50 +00:00
|
|
|
nextExport,
|
2017-04-25 04:15:58 +00:00
|
|
|
err: (err) ? serializeError(dev, err) : null
|
2016-10-09 09:50:41 +00:00
|
|
|
},
|
2016-10-10 04:24:30 +00:00
|
|
|
dev,
|
2017-05-08 02:36:11 +00:00
|
|
|
dir,
|
2016-10-28 14:38:24 +00:00
|
|
|
staticMarkup,
|
2018-04-12 07:47:42 +00:00
|
|
|
buildManifest,
|
2016-12-16 18:42:40 +00:00
|
|
|
...docProps
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
|
|
|
|
}
|
|
|
|
|
2018-02-21 17:41:25 +00:00
|
|
|
export async function renderScriptError (req, res, page, error) {
|
2017-04-24 20:48:34 +00:00
|
|
|
// Asks CDNs and others to not to cache the errored page
|
2018-05-06 20:57:27 +00:00
|
|
|
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
|
2017-04-24 20:48:34 +00:00
|
|
|
|
2018-03-31 07:01:46 +00:00
|
|
|
if (error.code === 'ENOENT' || error.message === 'INVALID_BUILD_ID') {
|
2018-02-21 17:41:25 +00:00
|
|
|
res.statusCode = 404
|
|
|
|
res.end('404 - Not Found')
|
2017-04-04 20:35:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-21 17:41:25 +00:00
|
|
|
logger.error(error.stack)
|
|
|
|
res.statusCode = 500
|
|
|
|
res.end('500 - Internal Error')
|
2017-04-04 19:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 13:18:59 +00:00
|
|
|
export function sendHTML (req, res, html, method, { dev, generateEtags }) {
|
2018-01-31 13:12:21 +00:00
|
|
|
if (isResSent(res)) return
|
2018-03-13 13:18:59 +00:00
|
|
|
const etag = generateEtags && generateETag(html)
|
2016-12-30 19:55:20 +00:00
|
|
|
|
2017-04-18 15:45:42 +00:00
|
|
|
if (fresh(req.headers, { etag })) {
|
|
|
|
res.statusCode = 304
|
|
|
|
res.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:28:08 +00:00
|
|
|
if (dev) {
|
|
|
|
// In dev, we should not cache pages for any reason.
|
|
|
|
// That's why we do this.
|
|
|
|
res.setHeader('Cache-Control', 'no-store, must-revalidate')
|
|
|
|
}
|
|
|
|
|
2018-03-13 13:18:59 +00:00
|
|
|
if (etag) {
|
|
|
|
res.setHeader('ETag', etag)
|
|
|
|
}
|
|
|
|
|
2017-11-05 19:17:03 +00:00
|
|
|
if (!res.getHeader('Content-Type')) {
|
2017-12-06 00:00:58 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
2017-11-05 19:17:03 +00:00
|
|
|
}
|
2016-12-16 20:33:08 +00:00
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(html))
|
2017-01-12 15:38:43 +00:00
|
|
|
res.end(method === 'HEAD' ? null : html)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:38:43 +00:00
|
|
|
export function sendJSON (res, obj, method) {
|
2018-01-31 13:12:21 +00:00
|
|
|
if (isResSent(res)) return
|
2016-12-30 19:55:20 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
const json = JSON.stringify(obj)
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.setHeader('Content-Length', Buffer.byteLength(json))
|
2017-01-12 15:38:43 +00:00
|
|
|
res.end(method === 'HEAD' ? null : json)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function errorToJSON (err) {
|
2016-10-19 12:41:45 +00:00
|
|
|
const { name, message, stack } = err
|
|
|
|
const json = { name, message, stack }
|
|
|
|
|
2017-03-22 23:13:23 +00:00
|
|
|
if (err.module) {
|
|
|
|
// rawRequest contains the filename of the module which has the error.
|
|
|
|
const { rawRequest } = err.module
|
2016-10-19 12:41:45 +00:00
|
|
|
json.module = { rawRequest }
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
2016-12-31 12:46:23 +00:00
|
|
|
|
2017-04-25 04:15:58 +00:00
|
|
|
function serializeError (dev, err) {
|
|
|
|
if (dev) {
|
|
|
|
return errorToJSON(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return { message: '500 - Internal Server Error.' }
|
|
|
|
}
|
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
export function serveStatic (req, res, path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
send(req, path)
|
2018-03-27 18:11:03 +00:00
|
|
|
.on('directory', () => {
|
2017-02-08 00:13:25 +00:00
|
|
|
// We don't allow directories to be read.
|
2018-03-27 18:11:03 +00:00
|
|
|
const err = new Error('No directory access')
|
|
|
|
err.code = 'ENOENT'
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
.on('error', reject)
|
|
|
|
.pipe(res)
|
|
|
|
.on('finish', resolve)
|
2016-12-31 12:46:23 +00:00
|
|
|
})
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
async function ensurePage (page, { dir, hotReloader }) {
|
2018-02-13 15:13:22 +00:00
|
|
|
if (page === '/_error') return
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
await hotReloader.ensurePage(page)
|
|
|
|
}
|
2017-06-08 17:41:22 +00:00
|
|
|
|
2018-06-04 13:45:39 +00:00
|
|
|
function loadChunks ({ dev, distDir, availableChunks }) {
|
2017-06-08 17:41:22 +00:00
|
|
|
const flushedChunks = flushChunks()
|
2017-12-27 18:59:17 +00:00
|
|
|
const response = {
|
|
|
|
names: [],
|
|
|
|
filenames: []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev) {
|
Process available chunk names properly in dev mode (#4604)
Fixes #4603.
Tests explain it the best:
```js
describe('development mode (no chunkhash)', () => {
it('should strip the extension from the filename', () => {
const filename = 'foo_bar_0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the extension even if there\'s a hyphen in the name', () => {
const filename = 'foo-bar-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
})
})
describe('production mode (with chunkhash)', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
})
})
```
2018-06-14 09:04:03 +00:00
|
|
|
availableChunks = getAvailableChunks(distDir, dev)
|
2017-12-27 18:59:17 +00:00
|
|
|
}
|
2017-06-08 17:41:22 +00:00
|
|
|
|
|
|
|
for (var chunk of flushedChunks) {
|
2017-12-27 18:59:17 +00:00
|
|
|
const filename = availableChunks[chunk]
|
|
|
|
if (filename) {
|
|
|
|
response.names.push(chunk)
|
|
|
|
response.filenames.push(filename)
|
2017-06-08 17:41:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 18:59:17 +00:00
|
|
|
return response
|
2017-06-08 17:41:22 +00:00
|
|
|
}
|