提交 0007cd2a 编写于 作者: A Arunoda Susiripala 提交者: Guillermo Rauch

[custom server] Handle internal routes automatically (#1658)

* Implement the initial version.

* Improve the render logic a bit.

* Move all the webpack paths under /_next/

* Keep the log:false flag.
上级 0487956c
import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true'
import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true&path=/_next/webpack-hmr'
import Router from '../lib/router'
const handlers = {
......
......@@ -268,7 +268,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
path: buildDir ? join(buildDir, '.next') : join(dir, config.distDir),
filename: '[name]',
libraryTarget: 'commonjs2',
publicPath: '/_webpack/',
publicPath: '/_next/webpack/',
strictModuleExceptionHandling: true,
devtoolModuleFilenameTemplate ({ resourcePath }) {
const hash = createHash('sha1')
......
......@@ -140,7 +140,7 @@ export default class HotReloader {
} : {}
this.webpackDevMiddleware = webpackDevMiddleware(compiler, {
publicPath: '/_webpack/',
publicPath: '/_next/webpack/',
noInfo: true,
quiet: true,
clientLogLevel: 'warning',
......@@ -148,7 +148,10 @@ export default class HotReloader {
...windowsSettings
})
this.webpackHotMiddleware = webpackHotMiddleware(compiler, { log: false })
this.webpackHotMiddleware = webpackHotMiddleware(compiler, {
path: '/_next/webpack-hmr',
log: false
})
this.onDemandEntries = onDemandEntryHandler(this.webpackDevMiddleware, compiler, {
dir: this.dir,
dev: true,
......
......@@ -18,6 +18,11 @@ import getConfig from './config'
// We need to go up one more level since we are in the `dist` directory
import pkg from '../../package'
const internalPrefixes = [
/^\/_next\//,
/^\/static\//
]
export default class Server {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
this.dir = resolve(dir)
......@@ -42,25 +47,27 @@ export default class Server {
this.defineRoutes()
}
getRequestHandler () {
return (req, res, parsedUrl) => {
// Parse url if parsedUrl not provided
if (!parsedUrl) {
parsedUrl = parseUrl(req.url, true)
}
// Parse the querystring ourselves if the user doesn't handle querystring parsing
if (typeof parsedUrl.query === 'string') {
parsedUrl.query = parseQs(parsedUrl.query)
}
handleRequest (req, res, parsedUrl) {
// Parse url if parsedUrl not provided
if (!parsedUrl) {
parsedUrl = parseUrl(req.url, true)
}
return this.run(req, res, parsedUrl)
.catch((err) => {
if (!this.quiet) console.error(err)
res.statusCode = 500
res.end(STATUS_CODES[500])
})
// Parse the querystring ourselves if the user doesn't handle querystring parsing
if (typeof parsedUrl.query === 'string') {
parsedUrl.query = parseQs(parsedUrl.query)
}
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)
}
async prepare () {
......@@ -181,7 +188,11 @@ export default class Server {
}
}
async render (req, res, pathname, query) {
async render (req, res, pathname, query, parsedUrl) {
if (this.isInternalUrl(req)) {
return this.handleRequest(req, res, parsedUrl)
}
if (this.config.poweredByHeader) {
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
}
......@@ -291,6 +302,16 @@ export default class Server {
}
}
isInternalUrl (req) {
for (const prefix of internalPrefixes) {
if (prefix.test(req.url)) {
return true
}
}
return false
}
readBuildId () {
const buildIdPath = join(this.dir, this.dist, 'BUILD_ID')
const buildId = fs.readFileSync(buildIdPath, 'utf8')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册