Skip to content

Commit 0007cd2

Browse files
arunodarauchg
authored andcommitted
[custom server] Handle internal routes automatically (vercel#1658)
* Implement the initial version. * Improve the render logic a bit. * Move all the webpack paths under /_next/ * Keep the log:false flag.
1 parent 0487956 commit 0007cd2

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

client/webpack-hot-middleware-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true'
1+
import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true&path=/_next/webpack-hmr'
22
import Router from '../lib/router'
33

44
const handlers = {

server/build/webpack.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
268268
path: buildDir ? join(buildDir, '.next') : join(dir, config.distDir),
269269
filename: '[name]',
270270
libraryTarget: 'commonjs2',
271-
publicPath: '/_webpack/',
271+
publicPath: '/_next/webpack/',
272272
strictModuleExceptionHandling: true,
273273
devtoolModuleFilenameTemplate ({ resourcePath }) {
274274
const hash = createHash('sha1')

server/hot-reloader.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,18 @@ export default class HotReloader {
140140
} : {}
141141

142142
this.webpackDevMiddleware = webpackDevMiddleware(compiler, {
143-
publicPath: '/_webpack/',
143+
publicPath: '/_next/webpack/',
144144
noInfo: true,
145145
quiet: true,
146146
clientLogLevel: 'warning',
147147
watchOptions: { ignored },
148148
...windowsSettings
149149
})
150150

151-
this.webpackHotMiddleware = webpackHotMiddleware(compiler, { log: false })
151+
this.webpackHotMiddleware = webpackHotMiddleware(compiler, {
152+
path: '/_next/webpack-hmr',
153+
log: false
154+
})
152155
this.onDemandEntries = onDemandEntryHandler(this.webpackDevMiddleware, compiler, {
153156
dir: this.dir,
154157
dev: true,

server/index.js

+39-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import getConfig from './config'
1818
// We need to go up one more level since we are in the `dist` directory
1919
import pkg from '../../package'
2020

21+
const internalPrefixes = [
22+
/^\/_next\//,
23+
/^\/static\//
24+
]
25+
2126
export default class Server {
2227
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
2328
this.dir = resolve(dir)
@@ -42,25 +47,27 @@ export default class Server {
4247
this.defineRoutes()
4348
}
4449

45-
getRequestHandler () {
46-
return (req, res, parsedUrl) => {
47-
// Parse url if parsedUrl not provided
48-
if (!parsedUrl) {
49-
parsedUrl = parseUrl(req.url, true)
50-
}
51-
52-
// Parse the querystring ourselves if the user doesn't handle querystring parsing
53-
if (typeof parsedUrl.query === 'string') {
54-
parsedUrl.query = parseQs(parsedUrl.query)
55-
}
50+
handleRequest (req, res, parsedUrl) {
51+
// Parse url if parsedUrl not provided
52+
if (!parsedUrl) {
53+
parsedUrl = parseUrl(req.url, true)
54+
}
5655

57-
return this.run(req, res, parsedUrl)
58-
.catch((err) => {
59-
if (!this.quiet) console.error(err)
60-
res.statusCode = 500
61-
res.end(STATUS_CODES[500])
62-
})
56+
// Parse the querystring ourselves if the user doesn't handle querystring parsing
57+
if (typeof parsedUrl.query === 'string') {
58+
parsedUrl.query = parseQs(parsedUrl.query)
6359
}
60+
61+
return this.run(req, res, parsedUrl)
62+
.catch((err) => {
63+
if (!this.quiet) console.error(err)
64+
res.statusCode = 500
65+
res.end(STATUS_CODES[500])
66+
})
67+
}
68+
69+
getRequestHandler () {
70+
return this.handleRequest.bind(this)
6471
}
6572

6673
async prepare () {
@@ -181,7 +188,11 @@ export default class Server {
181188
}
182189
}
183190

184-
async render (req, res, pathname, query) {
191+
async render (req, res, pathname, query, parsedUrl) {
192+
if (this.isInternalUrl(req)) {
193+
return this.handleRequest(req, res, parsedUrl)
194+
}
195+
185196
if (this.config.poweredByHeader) {
186197
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
187198
}
@@ -291,6 +302,16 @@ export default class Server {
291302
}
292303
}
293304

305+
isInternalUrl (req) {
306+
for (const prefix of internalPrefixes) {
307+
if (prefix.test(req.url)) {
308+
return true
309+
}
310+
}
311+
312+
return false
313+
}
314+
294315
readBuildId () {
295316
const buildIdPath = join(this.dir, this.dist, 'BUILD_ID')
296317
const buildId = fs.readFileSync(buildIdPath, 'utf8')

0 commit comments

Comments
 (0)