2016-10-19 12:41:45 +00:00
|
|
|
import { resolve, join } from 'path'
|
2017-03-01 23:30:07 +00:00
|
|
|
import { parse as parseUrl } from 'url'
|
|
|
|
import { parse as parseQs } from 'querystring'
|
2017-03-07 18:43:56 +00:00
|
|
|
import fs from 'fs'
|
2017-01-12 15:38:43 +00:00
|
|
|
import http, { STATUS_CODES } from 'http'
|
2016-12-16 20:33:08 +00:00
|
|
|
import {
|
|
|
|
renderToHTML,
|
|
|
|
renderErrorToHTML,
|
2016-12-31 12:46:23 +00:00
|
|
|
sendHTML,
|
2017-04-18 04:18:43 +00:00
|
|
|
serveStatic,
|
|
|
|
renderScript,
|
|
|
|
renderScriptError
|
2016-12-16 20:33:08 +00:00
|
|
|
} from './render'
|
2016-10-05 23:52:50 +00:00
|
|
|
import Router from './router'
|
2016-10-17 07:05:46 +00:00
|
|
|
import HotReloader from './hot-reloader'
|
2016-10-19 12:41:45 +00:00
|
|
|
import { resolveFromList } from './resolve'
|
2016-12-19 15:27:47 +00:00
|
|
|
import getConfig from './config'
|
|
|
|
// We need to go up one more level since we are in the `dist` directory
|
|
|
|
import pkg from '../../package'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-04-07 17:58:35 +00:00
|
|
|
const internalPrefixes = [
|
|
|
|
/^\/_next\//,
|
|
|
|
/^\/static\//
|
|
|
|
]
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
export default class Server {
|
2016-12-16 20:33:08 +00:00
|
|
|
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
|
2016-10-06 11:05:52 +00:00
|
|
|
this.dir = resolve(dir)
|
|
|
|
this.dev = dev
|
2016-12-16 20:33:08 +00:00
|
|
|
this.quiet = quiet
|
2016-10-05 23:52:50 +00:00
|
|
|
this.router = new Router()
|
2016-12-21 14:39:08 +00:00
|
|
|
this.hotReloader = dev ? new HotReloader(this.dir, { quiet }) : null
|
2016-12-16 20:33:08 +00:00
|
|
|
this.http = null
|
2016-12-27 23:27:51 +00:00
|
|
|
this.config = getConfig(this.dir)
|
2017-04-06 10:09:26 +00:00
|
|
|
this.dist = this.config.distDir
|
|
|
|
this.buildStats = !dev ? require(join(this.dir, this.dist, 'build-stats.json')) : null
|
2017-03-07 18:43:56 +00:00
|
|
|
this.buildId = !dev ? this.readBuildId() : '-'
|
|
|
|
this.renderOpts = {
|
|
|
|
dev,
|
|
|
|
staticMarkup,
|
|
|
|
dir: this.dir,
|
|
|
|
hotReloader: this.hotReloader,
|
|
|
|
buildStats: this.buildStats,
|
2017-04-18 04:18:43 +00:00
|
|
|
buildId: this.buildId,
|
|
|
|
assetPrefix: this.config.assetPrefix.replace(/\/$/, '')
|
2017-03-07 18:43:56 +00:00
|
|
|
}
|
2016-12-16 20:33:08 +00:00
|
|
|
|
|
|
|
this.defineRoutes()
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-04-07 17:58:35 +00:00
|
|
|
handleRequest (req, res, parsedUrl) {
|
|
|
|
// Parse url if parsedUrl not provided
|
|
|
|
if (!parsedUrl) {
|
|
|
|
parsedUrl = parseUrl(req.url, true)
|
|
|
|
}
|
2017-02-09 03:22:48 +00:00
|
|
|
|
2017-04-07 17:58:35 +00:00
|
|
|
// Parse the querystring ourselves if the user doesn't handle querystring parsing
|
|
|
|
if (typeof parsedUrl.query === 'string') {
|
|
|
|
parsedUrl.query = parseQs(parsedUrl.query)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2017-04-07 17:58:35 +00:00
|
|
|
|
|
|
|
return this.run(req, res, parsedUrl)
|
|
|
|
.catch((err) => {
|
|
|
|
if (!this.quiet) console.error(err)
|
|
|
|
res.statusCode = 500
|
|
|
|
res.end(STATUS_CODES[500])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestHandler () {
|
|
|
|
return this.handleRequest.bind(this)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async prepare () {
|
2016-10-17 07:07:41 +00:00
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 04:04:40 +00:00
|
|
|
async close () {
|
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.stop()
|
|
|
|
}
|
2017-01-12 04:14:49 +00:00
|
|
|
|
|
|
|
if (this.http) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.http.close((err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
return resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2016-12-17 04:04:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 07:07:41 +00:00
|
|
|
defineRoutes () {
|
2017-01-12 15:38:43 +00:00
|
|
|
const routes = {
|
|
|
|
'/_next-prefetcher.js': async (req, res, params) => {
|
|
|
|
const p = join(__dirname, '../client/next-prefetcher-bundle.js')
|
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
2017-03-26 20:48:59 +00:00
|
|
|
'/_next/:hash/manifest.js': async (req, res, params) => {
|
|
|
|
this.handleBuildHash('manifest.js', params.hash, res)
|
2017-04-06 10:09:26 +00:00
|
|
|
const p = join(this.dir, `${this.dist}/manifest.js`)
|
2017-03-26 20:48:59 +00:00
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
2017-03-07 18:43:56 +00:00
|
|
|
'/_next/:hash/main.js': async (req, res, params) => {
|
|
|
|
this.handleBuildHash('main.js', params.hash, res)
|
2017-04-06 10:09:26 +00:00
|
|
|
const p = join(this.dir, `${this.dist}/main.js`)
|
2017-02-15 14:49:45 +00:00
|
|
|
await this.serveStatic(req, res, p)
|
2017-01-12 15:38:43 +00:00
|
|
|
},
|
|
|
|
|
2017-03-07 18:43:56 +00:00
|
|
|
'/_next/:hash/commons.js': async (req, res, params) => {
|
|
|
|
this.handleBuildHash('commons.js', params.hash, res)
|
2017-04-06 10:09:26 +00:00
|
|
|
const p = join(this.dir, `${this.dist}/commons.js`)
|
2017-02-15 14:49:45 +00:00
|
|
|
await this.serveStatic(req, res, p)
|
2017-01-12 15:38:43 +00:00
|
|
|
},
|
|
|
|
|
2017-03-24 07:51:34 +00:00
|
|
|
'/_next/:hash/app.js': async (req, res, params) => {
|
|
|
|
this.handleBuildHash('app.js', params.hash, res)
|
2017-04-06 10:09:26 +00:00
|
|
|
const p = join(this.dir, `${this.dist}/app.js`)
|
2017-03-24 07:51:34 +00:00
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
'/_next/:buildId/page/_error': async (req, res, params) => {
|
2017-02-20 23:48:17 +00:00
|
|
|
if (!this.handleBuildId(params.buildId, res)) {
|
2017-04-18 04:18:43 +00:00
|
|
|
const error = new Error('INVALID_BUILD_ID')
|
|
|
|
const customFields = { buildIdMismatched: true }
|
|
|
|
|
|
|
|
return await renderScriptError(req, res, '/_error', error, customFields, this.renderOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
const p = join(this.dir, '.next/bundles/pages/_error.js')
|
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
|
|
|
'/_next/:buildId/page/:path*': async (req, res, params) => {
|
|
|
|
const paths = params.path || ['']
|
|
|
|
const page = `/${paths.join('/')}`
|
|
|
|
|
|
|
|
if (!this.handleBuildId(params.buildId, res)) {
|
|
|
|
const error = new Error('INVALID_BUILD_ID')
|
|
|
|
const customFields = { buildIdMismatched: true }
|
|
|
|
|
|
|
|
return await renderScriptError(req, res, page, error, customFields, this.renderOpts)
|
2017-02-20 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
if (this.dev) {
|
|
|
|
try {
|
|
|
|
await this.hotReloader.ensurePage(page)
|
|
|
|
} catch (error) {
|
|
|
|
return await renderScriptError(req, res, page, error, {}, this.renderOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
const compilationErr = this.getCompilationError(page)
|
|
|
|
if (compilationErr) {
|
|
|
|
const customFields = { statusCode: 500 }
|
|
|
|
return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
await renderScript(req, res, page, this.renderOpts)
|
2017-01-12 15:38:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
'/_next/:path+': async (req, res, params) => {
|
|
|
|
const p = join(__dirname, '..', 'client', ...(params.path || []))
|
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
|
|
|
'/static/:path+': async (req, res, params) => {
|
|
|
|
const p = join(this.dir, 'static', ...(params.path || []))
|
|
|
|
await this.serveStatic(req, res, p)
|
|
|
|
},
|
|
|
|
|
2017-02-02 06:51:08 +00:00
|
|
|
'/:path*': async (req, res, params, parsedUrl) => {
|
|
|
|
const { pathname, query } = parsedUrl
|
2017-01-12 15:38:43 +00:00
|
|
|
await this.render(req, res, pathname, query)
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-01-12 15:38:43 +00:00
|
|
|
for (const method of ['GET', 'HEAD']) {
|
|
|
|
for (const p of Object.keys(routes)) {
|
|
|
|
this.router.add(method, p, routes[p])
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-02-12 11:26:10 +00:00
|
|
|
async start (port, hostname) {
|
2016-12-16 20:33:08 +00:00
|
|
|
await this.prepare()
|
|
|
|
this.http = http.createServer(this.getRequestHandler())
|
|
|
|
await new Promise((resolve, reject) => {
|
2017-02-01 20:36:23 +00:00
|
|
|
// This code catches EADDRINUSE error if the port is already in use
|
|
|
|
this.http.on('error', reject)
|
|
|
|
this.http.on('listening', () => resolve())
|
2017-02-12 16:23:42 +00:00
|
|
|
this.http.listen(port, hostname)
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:51:08 +00:00
|
|
|
async run (req, res, parsedUrl) {
|
2016-11-23 18:32:49 +00:00
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.run(req, res)
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:51:08 +00:00
|
|
|
const fn = this.router.match(req, res, parsedUrl)
|
2016-10-05 23:52:50 +00:00
|
|
|
if (fn) {
|
|
|
|
await fn()
|
2017-01-12 15:38:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
2017-02-02 06:51:08 +00:00
|
|
|
await this.render404(req, res, parsedUrl)
|
2017-01-12 15:38:43 +00:00
|
|
|
} else {
|
|
|
|
res.statusCode = 501
|
|
|
|
res.end(STATUS_CODES[501])
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 17:58:35 +00:00
|
|
|
async render (req, res, pathname, query, parsedUrl) {
|
|
|
|
if (this.isInternalUrl(req)) {
|
|
|
|
return this.handleRequest(req, res, parsedUrl)
|
|
|
|
}
|
|
|
|
|
2016-12-19 15:27:47 +00:00
|
|
|
if (this.config.poweredByHeader) {
|
|
|
|
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
|
|
|
|
}
|
2016-12-16 20:33:08 +00:00
|
|
|
const html = await this.renderToHTML(req, res, pathname, query)
|
2017-02-16 02:48:35 +00:00
|
|
|
return sendHTML(res, html, req.method)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async renderToHTML (req, res, pathname, query) {
|
|
|
|
if (this.dev) {
|
|
|
|
const compilationErr = this.getCompilationError(pathname)
|
|
|
|
if (compilationErr) {
|
|
|
|
res.statusCode = 500
|
|
|
|
return this.renderErrorToHTML(compilationErr, req, res, pathname, query)
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-12-16 20:33:08 +00:00
|
|
|
return await renderToHTML(req, res, pathname, query, this.renderOpts)
|
2016-11-24 14:03:16 +00:00
|
|
|
} catch (err) {
|
2016-12-16 20:33:08 +00:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
res.statusCode = 404
|
|
|
|
return this.renderErrorToHTML(null, req, res, pathname, query)
|
|
|
|
} else {
|
|
|
|
if (!this.quiet) console.error(err)
|
|
|
|
res.statusCode = 500
|
|
|
|
return this.renderErrorToHTML(err, req, res, pathname, query)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async renderError (err, req, res, pathname, query) {
|
|
|
|
const html = await this.renderErrorToHTML(err, req, res, pathname, query)
|
2017-02-16 02:48:35 +00:00
|
|
|
return sendHTML(res, html, req.method)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async renderErrorToHTML (err, req, res, pathname, query) {
|
|
|
|
if (this.dev) {
|
|
|
|
const compilationErr = this.getCompilationError('/_error')
|
|
|
|
if (compilationErr) {
|
|
|
|
res.statusCode = 500
|
|
|
|
return renderErrorToHTML(compilationErr, req, res, pathname, query, this.renderOpts)
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
try {
|
2016-12-16 20:33:08 +00:00
|
|
|
return await renderErrorToHTML(err, req, res, pathname, query, this.renderOpts)
|
|
|
|
} catch (err2) {
|
|
|
|
if (this.dev) {
|
|
|
|
if (!this.quiet) console.error(err2)
|
|
|
|
res.statusCode = 500
|
|
|
|
return renderErrorToHTML(err2, req, res, pathname, query, this.renderOpts)
|
2016-11-24 14:03:16 +00:00
|
|
|
} else {
|
2016-12-16 20:33:08 +00:00
|
|
|
throw err2
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 23:30:07 +00:00
|
|
|
async render404 (req, res, parsedUrl = parseUrl(req.url, true)) {
|
2017-02-02 06:51:08 +00:00
|
|
|
const { pathname, query } = parsedUrl
|
2016-12-16 20:33:08 +00:00
|
|
|
res.statusCode = 404
|
2017-02-16 02:48:35 +00:00
|
|
|
return this.renderError(null, req, res, pathname, query)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2017-02-16 02:48:35 +00:00
|
|
|
async serveStatic (req, res, path) {
|
2017-01-01 19:36:37 +00:00
|
|
|
try {
|
2017-02-16 02:48:35 +00:00
|
|
|
return await serveStatic(req, res, path)
|
2017-01-01 19:36:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
this.render404(req, res)
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 17:58:35 +00:00
|
|
|
isInternalUrl (req) {
|
|
|
|
for (const prefix of internalPrefixes) {
|
|
|
|
if (prefix.test(req.url)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-07 18:43:56 +00:00
|
|
|
readBuildId () {
|
2017-04-06 10:09:26 +00:00
|
|
|
const buildIdPath = join(this.dir, this.dist, 'BUILD_ID')
|
2017-03-07 18:43:56 +00:00
|
|
|
const buildId = fs.readFileSync(buildIdPath, 'utf8')
|
|
|
|
return buildId.trim()
|
2017-01-11 20:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleBuildId (buildId, res) {
|
2017-02-20 23:48:17 +00:00
|
|
|
if (this.dev) return true
|
2017-01-11 20:16:18 +00:00
|
|
|
if (buildId !== this.renderOpts.buildId) {
|
2017-02-20 23:48:17 +00:00
|
|
|
return false
|
2017-01-11 20:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
|
2017-02-20 23:48:17 +00:00
|
|
|
return true
|
2017-01-11 20:16:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
getCompilationError (page) {
|
2016-10-19 12:41:45 +00:00
|
|
|
if (!this.hotReloader) return
|
|
|
|
|
|
|
|
const errors = this.hotReloader.getCompilationErrors()
|
|
|
|
if (!errors.size) return
|
|
|
|
|
2017-04-06 10:09:26 +00:00
|
|
|
const id = join(this.dir, this.dist, 'bundles', 'pages', page)
|
2016-12-16 20:33:08 +00:00
|
|
|
const p = resolveFromList(id, errors.keys())
|
|
|
|
if (p) return errors.get(p)[0]
|
2016-10-19 12:41:45 +00:00
|
|
|
}
|
2017-02-20 23:48:17 +00:00
|
|
|
|
2017-03-07 18:43:56 +00:00
|
|
|
handleBuildHash (filename, hash, res) {
|
|
|
|
if (this.dev) return
|
|
|
|
if (hash !== this.buildStats[filename].hash) {
|
|
|
|
throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
|
|
|
|
}
|
2017-02-20 23:48:17 +00:00
|
|
|
}
|