2016-10-19 12:41:45 +00:00
|
|
|
import { resolve, join } from 'path'
|
|
|
|
import { parse } from 'url'
|
2016-12-16 20:33:08 +00:00
|
|
|
import http from 'http'
|
|
|
|
import {
|
|
|
|
renderToHTML,
|
|
|
|
renderErrorToHTML,
|
|
|
|
renderJSON,
|
|
|
|
renderErrorJSON,
|
2016-12-31 12:46:23 +00:00
|
|
|
sendHTML,
|
|
|
|
serveStatic,
|
|
|
|
serveStaticWithGzip
|
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
|
|
|
|
|
|
|
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
|
|
|
|
this.renderOpts = { dir: this.dir, dev, staticMarkup }
|
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)
|
2016-12-16 20:33:08 +00:00
|
|
|
|
|
|
|
this.defineRoutes()
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
getRequestHandler () {
|
|
|
|
return (req, res) => {
|
2016-10-09 09:25:38 +00:00
|
|
|
this.run(req, res)
|
|
|
|
.catch((err) => {
|
2016-12-16 20:33:08 +00:00
|
|
|
if (!this.quiet) console.error(err)
|
2016-11-24 14:03:16 +00:00
|
|
|
res.statusCode = 500
|
2016-10-17 00:00:17 +00:00
|
|
|
res.end('error')
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 07:07:41 +00:00
|
|
|
defineRoutes () {
|
2016-12-15 19:13:40 +00:00
|
|
|
this.router.get('/_next-prefetcher.js', async (req, res, params) => {
|
|
|
|
const p = join(__dirname, '../client/next-prefetcher-bundle.js')
|
2016-12-31 12:46:23 +00:00
|
|
|
await serveStatic(req, res, p)
|
2016-12-15 19:13:40 +00:00
|
|
|
})
|
|
|
|
|
2016-12-17 08:49:10 +00:00
|
|
|
this.router.get('/_next/main.js', async (req, res, params) => {
|
|
|
|
const p = join(this.dir, '.next/main.js')
|
2016-12-31 12:46:23 +00:00
|
|
|
await serveStaticWithGzip(req, res, p)
|
2016-12-17 08:49:10 +00:00
|
|
|
})
|
|
|
|
|
2016-11-28 00:15:56 +00:00
|
|
|
this.router.get('/_next/commons.js', async (req, res, params) => {
|
|
|
|
const p = join(this.dir, '.next/commons.js')
|
2016-12-31 12:46:23 +00:00
|
|
|
await serveStaticWithGzip(req, res, p)
|
2016-11-28 00:15:56 +00:00
|
|
|
})
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
this.router.get('/_next/pages/:path*', async (req, res, params) => {
|
2016-12-31 12:46:23 +00:00
|
|
|
const paths = params.path || ['index']
|
2016-12-16 20:33:08 +00:00
|
|
|
const pathname = `/${paths.join('/')}`
|
2016-12-31 12:46:23 +00:00
|
|
|
await this.renderJSON(req, res, pathname)
|
2016-12-16 20:33:08 +00:00
|
|
|
})
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
this.router.get('/_next/:path+', async (req, res, params) => {
|
2016-10-19 12:58:08 +00:00
|
|
|
const p = join(__dirname, '..', 'client', ...(params.path || []))
|
2016-12-31 12:46:23 +00:00
|
|
|
await serveStatic(req, res, p)
|
2016-10-08 05:21:10 +00:00
|
|
|
})
|
|
|
|
this.router.get('/static/:path+', async (req, res, params) => {
|
2016-10-19 12:58:08 +00:00
|
|
|
const p = join(this.dir, 'static', ...(params.path || []))
|
2016-12-31 12:46:23 +00:00
|
|
|
await serveStatic(req, res, p)
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
this.router.get('/:path*', async (req, res) => {
|
|
|
|
const { pathname, query } = parse(req.url, true)
|
|
|
|
await this.render(req, res, pathname, query)
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async start (port) {
|
|
|
|
await this.prepare()
|
|
|
|
this.http = http.createServer(this.getRequestHandler())
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.http.listen(port, (err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
resolve()
|
|
|
|
})
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async run (req, res) {
|
2016-11-23 18:32:49 +00:00
|
|
|
if (this.hotReloader) {
|
|
|
|
await this.hotReloader.run(req, res)
|
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
const fn = this.router.match(req, res)
|
|
|
|
if (fn) {
|
|
|
|
await fn()
|
|
|
|
} else {
|
|
|
|
await this.render404(req, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async render (req, res, pathname, query) {
|
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)
|
|
|
|
sendHTML(res, html)
|
|
|
|
}
|
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)
|
2016-10-10 04:24:30 +00:00
|
|
|
sendHTML(res, html)
|
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
|
|
|
}
|
|
|
|
|
2016-10-10 04:24:30 +00:00
|
|
|
async render404 (req, res) {
|
2016-11-04 08:05:03 +00:00
|
|
|
const { pathname, query } = parse(req.url, true)
|
2016-12-16 20:33:08 +00:00
|
|
|
res.statusCode = 404
|
|
|
|
this.renderErrorToHTML(null, req, res, pathname, query)
|
|
|
|
}
|
2016-11-03 15:12:37 +00:00
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
async renderJSON (req, res, page) {
|
2016-12-16 20:33:08 +00:00
|
|
|
if (this.dev) {
|
|
|
|
const compilationErr = this.getCompilationError(page)
|
|
|
|
if (compilationErr) {
|
2016-12-31 12:46:23 +00:00
|
|
|
return this.renderErrorJSON(compilationErr, req, res)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
2016-11-03 15:12:37 +00:00
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
try {
|
2016-12-31 12:46:23 +00:00
|
|
|
await renderJSON(req, res, page, 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
|
2016-12-31 12:46:23 +00:00
|
|
|
return this.renderErrorJSON(null, req, res)
|
2016-11-24 14:03:16 +00:00
|
|
|
} else {
|
2016-12-16 20:33:08 +00:00
|
|
|
if (!this.quiet) console.error(err)
|
|
|
|
res.statusCode = 500
|
2016-12-31 12:46:23 +00:00
|
|
|
return this.renderErrorJSON(err, req, res)
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 04:24:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
async renderErrorJSON (err, req, res) {
|
2016-12-16 20:33:08 +00:00
|
|
|
if (this.dev) {
|
|
|
|
const compilationErr = this.getCompilationError('/_error')
|
|
|
|
if (compilationErr) {
|
|
|
|
res.statusCode = 500
|
2016-12-31 12:46:23 +00:00
|
|
|
return renderErrorJSON(compilationErr, req, res, this.renderOpts)
|
2016-12-16 20:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
return renderErrorJSON(err, req, res, this.renderOpts)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2016-10-19 12:41:45 +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
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
const id = join(this.dir, '.next', 'bundles', 'pages', page)
|
|
|
|
const p = resolveFromList(id, errors.keys())
|
|
|
|
if (p) return errors.get(p)[0]
|
2016-10-19 12:41:45 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|