mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
[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.
This commit is contained in:
parent
0487956c47
commit
0007cd2a97
|
@ -1,4 +1,4 @@
|
|||
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)
|
||||
}
|
||||
|
||||
return this.run(req, res, parsedUrl)
|
||||
.catch((err) => {
|
||||
if (!this.quiet) console.error(err)
|
||||
res.statusCode = 500
|
||||
res.end(STATUS_CODES[500])
|
||||
})
|
||||
handleRequest (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)
|
||||
}
|
||||
|
||||
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')
|
||||
|
|
Loading…
Reference in a new issue