1
0
Fork 0
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:
Arunoda Susiripala 2017-04-07 23:28:35 +05:30 committed by Guillermo Rauch
parent 0487956c47
commit 0007cd2a97
4 changed files with 47 additions and 23 deletions

View file

@ -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' import Router from '../lib/router'
const handlers = { const handlers = {

View file

@ -268,7 +268,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
path: buildDir ? join(buildDir, '.next') : join(dir, config.distDir), path: buildDir ? join(buildDir, '.next') : join(dir, config.distDir),
filename: '[name]', filename: '[name]',
libraryTarget: 'commonjs2', libraryTarget: 'commonjs2',
publicPath: '/_webpack/', publicPath: '/_next/webpack/',
strictModuleExceptionHandling: true, strictModuleExceptionHandling: true,
devtoolModuleFilenameTemplate ({ resourcePath }) { devtoolModuleFilenameTemplate ({ resourcePath }) {
const hash = createHash('sha1') const hash = createHash('sha1')

View file

@ -140,7 +140,7 @@ export default class HotReloader {
} : {} } : {}
this.webpackDevMiddleware = webpackDevMiddleware(compiler, { this.webpackDevMiddleware = webpackDevMiddleware(compiler, {
publicPath: '/_webpack/', publicPath: '/_next/webpack/',
noInfo: true, noInfo: true,
quiet: true, quiet: true,
clientLogLevel: 'warning', clientLogLevel: 'warning',
@ -148,7 +148,10 @@ export default class HotReloader {
...windowsSettings ...windowsSettings
}) })
this.webpackHotMiddleware = webpackHotMiddleware(compiler, { log: false }) this.webpackHotMiddleware = webpackHotMiddleware(compiler, {
path: '/_next/webpack-hmr',
log: false
})
this.onDemandEntries = onDemandEntryHandler(this.webpackDevMiddleware, compiler, { this.onDemandEntries = onDemandEntryHandler(this.webpackDevMiddleware, compiler, {
dir: this.dir, dir: this.dir,
dev: true, dev: true,

View file

@ -18,6 +18,11 @@ import getConfig from './config'
// We need to go up one more level since we are in the `dist` directory // We need to go up one more level since we are in the `dist` directory
import pkg from '../../package' import pkg from '../../package'
const internalPrefixes = [
/^\/_next\//,
/^\/static\//
]
export default class Server { export default class Server {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) { constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
this.dir = resolve(dir) this.dir = resolve(dir)
@ -42,8 +47,7 @@ export default class Server {
this.defineRoutes() this.defineRoutes()
} }
getRequestHandler () { handleRequest (req, res, parsedUrl) {
return (req, res, parsedUrl) => {
// Parse url if parsedUrl not provided // Parse url if parsedUrl not provided
if (!parsedUrl) { if (!parsedUrl) {
parsedUrl = parseUrl(req.url, true) parsedUrl = parseUrl(req.url, true)
@ -61,6 +65,9 @@ export default class Server {
res.end(STATUS_CODES[500]) res.end(STATUS_CODES[500])
}) })
} }
getRequestHandler () {
return this.handleRequest.bind(this)
} }
async prepare () { 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) { if (this.config.poweredByHeader) {
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`) 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 () { readBuildId () {
const buildIdPath = join(this.dir, this.dist, 'BUILD_ID') const buildIdPath = join(this.dir, this.dist, 'BUILD_ID')
const buildId = fs.readFileSync(buildIdPath, 'utf8') const buildId = fs.readFileSync(buildIdPath, 'utf8')