1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/index.js
Tim Neutkens e093441bad Universal Webpack (#3578)
* Speed up next build

* Document webpack config

* Speed up next build

* Remove comment

* Add comment

* Clean up rules

* Add comments

* Run in parallel

* Push plugins seperately

* Create a new chunk for react

* Don’t uglify react since it’s already uglified. Move react to commons in development

* Use the minified version directly

* Re-add globpattern

* Move loaders into a separate variable

* Add comment linking to Dan’s explanation

* Remove dot

* Add universal webpack

* Initial dev support

* Fix linting

* Add changes from Arunoda's work

* Made next dev works.
But super slow and no HMR support.

* Fix client side hot reload

* Server side hmr

* Only in dev

* Add on-demand-entries client + hot-middleware

* Add .babelrc support

* Speed up on demand entries by running in parallel

* Serve static generated files

* Add missing config in dev

* Add sass support

* Add support for .map

* Add cssloader config and fix .jsx support

* Rename

* use same defaults as css-loader. Fix linting

* Add NoEmitErrorsPlugin

* Add clientBootstrap

* Use webpackhotmiddleware on the multi compiler

* alpha.3

* Use babel 16.2.x

* Fix reloading after error

* Remove comment

* Release 5.0.0-univeral-alpha.1

* Remove check for React 16

* Release 5.0.0-universal-alpha.2

* React hot loader v4

* Use our static file rendering machanism to serve pages.
This should work well since the file path for a page is predictable.

* Release 5.0.0-universal-alpha.3

* Remove optional loaders

* Release 5.0.0-universal-alpha.4

* Remove clientBootstrap

* Remove renderScript

* Make sure pages bundles are served correctly

* Remove unused import

* Revert to using the same code as canary

* Fix hot loader

* Release 5.0.0-universal-alpha.5

* Check if externals dir exist before applying config

* Add typescript support

* Add support for transpiling certain packages in node_modules

Thanks to @giuseppeg’s work in https://github.com/zeit/next.js/pull/3319

* Add BABEL_DISABLE_CACHE support

* Make sourcemaps in production opt-in

* Revert "Add support for transpiling certain packages in node_modules"

This reverts commit d4b1d9babfb4b9ed4f4b12d56d52dee233e862da.

In favor of a better api around this.

* Support typescript through next.config.js

* Remove comments

* Bring back commons.js calculation

* Remove unused dependencies

* Move base.config.js to webpack.js

* Make sure to only invalidate webpackDevMiddleware one after other.

* Allow babel-loder caching by default.

* Add comment about preact support

* Bring back buildir replace

* Remove obsolete plugin

* Remove build replace, speed up build

* Resolve page entries like pages/day/index.js to pages/day.js

* Add componentDidCatch back

* Compile to bundles

* Use config.distDir everywhere

* Make sure the file is an array

* Remove console.log

* Apply optimization to uglifyjs

* Add comment pointing to source

* Create entries the same way in dev and production

* Remove unused and broken pagesGlobPattern

* day/index.js is automatically turned into day.js at build time

* Remove poweredByHeader option

* Load pages with the correct path.

* Release 5.0.0-universal-alpha.6

* Make sure react-dom/server can be overwritten by module-alias

* Only add react-hot-loader babel plugin in dev

* Release 5.0.0-universal-alpha.7

* Revert tests

* Release 5.0.0-universal-alpha.10

* Make sure next/head is working properly.

* Add wepack alias for 'next' back.

* Make sure overriding className in next/head works

* Alias react too

* Add missing r

* Fragment fallback has to wrap the children

* Use min.js

* Remove css.js

* Remove wallaby.js

* Release 5.0.0-universal-alpha.11

* Resolve relative to workdir instead of next

* Make sure we touch the right file

* Resolve next modules

* Remove dotjsx removal plugins since we use webpack on the server

* Revert "Resolve relative to workdir instead of next"

This reverts commit a13f3e4ab565df9e2c9a3dfc8eb4009c0c2e02ed.

* Externalize any locally loaded module lives outside of app dir.

* Remove server aliases

* Check node_modules reliably

* Add symlink to next for tests

* Make sure dynamic imports work locally.
This is why we need it: b545b519b2/lib/MainTemplate.js (L68)
We need to have the finally clause in the above in __webpack_require__.
webpack output option strictModuleExceptionHandling does that.

* dynmaic -> dynamic

* Remove webpack-node-externals

* Make sure dynamic imports support SSR.

* Remove css support in favor of next-css

* Make sure we load path from `/` since it’s included in the path matching

* Catch when ensurepage couldn’t be fulfilled for `.js.map`

* Register require cache flusher for both client and server

* Add comment explaining this is to facilitate hot reloading

* Only load module when needed

* Remove unused modules

* Release 5.0.0-universal-alpha.12

* Only log the `found babel` message once

* Make sure ondemand entries working correctly.
Now we are just using a single instance of OnDemandEntryHandler.

* Better sourcemaps

* Release 5.0.0-universal-alpha.13

* Lock uglify version to 1.1.6

* Release 5.0.0-universal-alpha.14

* Fix a typo.

* Introduce multi-zones support for mircofrontends

* Add section on css
2018-01-30 16:44:44 +01:00

439 lines
13 KiB
JavaScript

require('@zeit/source-map-support').install()
import { resolve, join, sep } from 'path'
import { parse as parseUrl } from 'url'
import { parse as parseQs } from 'querystring'
import fs from 'fs'
import http, { STATUS_CODES } from 'http'
import {
renderToHTML,
renderErrorToHTML,
sendHTML,
serveStatic,
renderScriptError
} from './render'
import Router from './router'
import { getAvailableChunks, isInternalUrl } from './utils'
import getConfig from './config'
// We need to go up one more level since we are in the `dist` directory
import pkg from '../../package'
import * as asset from '../lib/asset'
const blockedPages = {
'/_document': true,
'/_error': true
}
export default class Server {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false, conf = null } = {}) {
this.dir = resolve(dir)
this.dev = dev
this.quiet = quiet
this.router = new Router()
this.hotReloader = dev ? this.getHotReloader(this.dir, { quiet, conf }) : null
this.http = null
this.config = getConfig(this.dir, conf)
this.dist = this.config.distDir
if (!dev && !fs.existsSync(resolve(dir, this.dist, 'BUILD_ID'))) {
console.error(`> Could not find a valid build in the '${this.dist}' directory! Try building your app with 'next build' before starting the server.`)
process.exit(1)
}
this.buildStats = !dev ? require(join(this.dir, this.dist, 'build-stats.json')) : null
this.buildId = !dev ? this.readBuildId() : '-'
this.renderOpts = {
dev,
staticMarkup,
dir: this.dir,
hotReloader: this.hotReloader,
buildStats: this.buildStats,
buildId: this.buildId,
assetPrefix: this.config.assetPrefix.replace(/\/$/, ''),
availableChunks: dev ? {} : getAvailableChunks(this.dir, this.dist)
}
// With this, static assets will work across zones
asset.setAssetPrefix(this.config.assetPrefix)
this.defineRoutes()
}
getHotReloader (dir, options) {
const HotReloader = require('./hot-reloader').default
return new HotReloader(dir, options)
}
handleRequest (req, res, parsedUrl) {
// Parse url if parsedUrl not provided
if (!parsedUrl || typeof parsedUrl !== 'object') {
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)
}
res.statusCode = 200
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 () {
if (this.hotReloader) {
await this.hotReloader.start()
}
}
async close () {
if (this.hotReloader) {
await this.hotReloader.stop()
}
if (this.http) {
await new Promise((resolve, reject) => {
this.http.close((err) => {
if (err) return reject(err)
return resolve()
})
})
}
}
defineRoutes () {
const routes = {
'/_next-prefetcher.js': async (req, res, params) => {
const p = join(__dirname, '../client/next-prefetcher-bundle.js')
await this.serveStatic(req, res, p)
},
// This is to support, webpack dynamic imports in production.
'/_next/webpack/chunks/:name': async (req, res, params) => {
// Cache aggressively in production
if (!this.dev) {
res.setHeader('Cache-Control', 'max-age=31536000, immutable')
}
const p = join(this.dir, this.dist, 'chunks', params.name)
await this.serveStatic(req, res, p)
},
// This is to support, webpack dynamic import support with HMR
'/_next/webpack/:id': async (req, res, params) => {
const p = join(this.dir, this.dist, 'chunks', params.id)
await this.serveStatic(req, res, p)
},
'/_next/:hash/manifest.js': async (req, res, params) => {
if (!this.dev) return this.send404(res)
this.handleBuildHash('manifest.js', params.hash, res)
const p = join(this.dir, this.dist, 'manifest.js')
await this.serveStatic(req, res, p)
},
'/_next/:hash/main.js': async (req, res, params) => {
if (!this.dev) return this.send404(res)
this.handleBuildHash('main.js', params.hash, res)
const p = join(this.dir, this.dist, 'main.js')
await this.serveStatic(req, res, p)
},
'/_next/:hash/commons.js': async (req, res, params) => {
if (!this.dev) return this.send404(res)
this.handleBuildHash('commons.js', params.hash, res)
const p = join(this.dir, this.dist, 'commons.js')
await this.serveStatic(req, res, p)
},
'/_next/:hash/app.js': async (req, res, params) => {
if (this.dev) return this.send404(res)
this.handleBuildHash('app.js', params.hash, res)
const p = join(this.dir, this.dist, 'app.js')
await this.serveStatic(req, res, p)
},
'/_next/:buildId/page/:path*.js.map': async (req, res, params) => {
const paths = params.path || ['']
const page = `/${paths.join('/')}`
if (this.dev) {
try {
await this.hotReloader.ensurePage(page)
} catch (err) {
await this.render404(req, res)
}
}
const dist = getConfig(this.dir).distDir
const path = join(this.dir, dist, 'bundles', 'pages', `${page}.js.map`)
await serveStatic(req, res, path)
},
'/_next/:buildId/page/_error*': async (req, res, params) => {
if (!this.handleBuildId(params.buildId, res)) {
const error = new Error('INVALID_BUILD_ID')
const customFields = { buildIdMismatched: true }
return await renderScriptError(req, res, '/_error', error, customFields, this.renderOpts)
}
const p = join(this.dir, `${this.dist}/bundles/pages/_error.js`)
await this.serveStatic(req, res, p)
},
'/_next/:buildId/page/:path*.js': async (req, res, params) => {
const paths = params.path || ['']
const page = `/${paths.join('/')}`
if (!this.handleBuildId(params.buildId, res)) {
const error = new Error('INVALID_BUILD_ID')
const customFields = { buildIdMismatched: true }
return await renderScriptError(req, res, page, error, customFields, this.renderOpts)
}
if (this.dev) {
try {
await this.hotReloader.ensurePage(page)
} catch (error) {
return await renderScriptError(req, res, page, error, {}, this.renderOpts)
}
const compilationErr = await this.getCompilationError()
if (compilationErr) {
const customFields = { statusCode: 500 }
return await renderScriptError(req, res, page, compilationErr, customFields, this.renderOpts)
}
}
const p = join(this.dir, this.dist, 'bundles', 'pages', `${page}.js`)
await this.serveStatic(req, res, p)
},
'/_next/static/:path*': async (req, res, params) => {
const p = join(this.dist, 'static', ...(params.path || []))
await this.serveStatic(req, res, p)
},
// It's very important keep this route's param optional.
// (but it should support as many as params, seperated by '/')
// Othewise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617
'/_next/:path*': async (req, res, params) => {
const p = join(__dirname, '..', 'client', ...(params.path || []))
await this.serveStatic(req, res, p)
},
// It's very important keep this route's param optional.
// (but it should support as many as params, seperated by '/')
// Othewise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617
'/static/:path*': async (req, res, params) => {
const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p)
}
}
if (this.config.useFileSystemPublicRoutes) {
routes['/:path*'] = async (req, res, params, parsedUrl) => {
const { pathname, query } = parsedUrl
await this.render(req, res, pathname, query)
}
}
for (const method of ['GET', 'HEAD']) {
for (const p of Object.keys(routes)) {
this.router.add(method, p, routes[p])
}
}
}
async start (port, hostname) {
await this.prepare()
this.http = http.createServer(this.getRequestHandler())
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
this.http.on('error', reject)
this.http.on('listening', () => resolve())
this.http.listen(port, hostname)
})
}
async run (req, res, parsedUrl) {
if (this.hotReloader) {
await this.hotReloader.run(req, res)
}
const fn = this.router.match(req, res, parsedUrl)
if (fn) {
await fn()
return
}
if (req.method === 'GET' || req.method === 'HEAD') {
await this.render404(req, res, parsedUrl)
} else {
res.statusCode = 501
res.end(STATUS_CODES[501])
}
}
async render (req, res, pathname, query, parsedUrl) {
if (isInternalUrl(req.url)) {
return this.handleRequest(req, res, parsedUrl)
}
if (blockedPages[pathname]) {
return await this.render404(req, res, parsedUrl)
}
const html = await this.renderToHTML(req, res, pathname, query)
res.setHeader('X-Powered-By', `Next.js ${pkg.version}`)
return sendHTML(req, res, html, req.method, this.renderOpts)
}
async renderToHTML (req, res, pathname, query) {
if (this.dev) {
const compilationErr = await this.getCompilationError()
if (compilationErr) {
res.statusCode = 500
return this.renderErrorToHTML(compilationErr, req, res, pathname, query)
}
}
try {
const out = await renderToHTML(req, res, pathname, query, this.renderOpts)
return out
} catch (err) {
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)
}
}
}
async renderError (err, req, res, pathname, query) {
const html = await this.renderErrorToHTML(err, req, res, pathname, query)
return sendHTML(req, res, html, req.method, this.renderOpts)
}
async renderErrorToHTML (err, req, res, pathname, query) {
if (this.dev) {
const compilationErr = await this.getCompilationError()
if (compilationErr) {
res.statusCode = 500
return renderErrorToHTML(compilationErr, req, res, pathname, query, this.renderOpts)
}
}
try {
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)
} else {
throw err2
}
}
}
async render404 (req, res, parsedUrl = parseUrl(req.url, true)) {
const { pathname, query } = parsedUrl
res.statusCode = 404
return this.renderError(null, req, res, pathname, query)
}
async serveStatic (req, res, path) {
if (!this.isServeableUrl(path)) {
return this.render404(req, res)
}
try {
return await serveStatic(req, res, path)
} catch (err) {
if (err.code === 'ENOENT') {
this.render404(req, res)
} else {
throw err
}
}
}
isServeableUrl (path) {
const resolved = resolve(path)
if (
resolved.indexOf(join(this.dir, this.dist) + sep) !== 0 &&
resolved.indexOf(join(this.dir, 'static') + sep) !== 0
) {
// Seems like the user is trying to traverse the filesystem.
return false
}
return true
}
readBuildId () {
const buildIdPath = join(this.dir, this.dist, 'BUILD_ID')
const buildId = fs.readFileSync(buildIdPath, 'utf8')
return buildId.trim()
}
handleBuildId (buildId, res) {
if (this.dev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return true
}
if (buildId !== this.renderOpts.buildId) {
return false
}
res.setHeader('Cache-Control', 'max-age=31536000, immutable')
return true
}
async getCompilationError () {
if (!this.hotReloader) return
const errors = await this.hotReloader.getCompilationErrors()
if (!errors.size) return
// Return the very first error we found.
return Array.from(errors.values())[0][0]
}
handleBuildHash (filename, hash, res) {
if (this.dev) {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return true
}
if (hash !== this.buildStats[filename].hash) {
throw new Error(`Invalid Build File Hash(${hash}) for chunk: ${filename}`)
}
res.setHeader('Cache-Control', 'max-age=31536000, immutable')
return true
}
send404 (res) {
res.statusCode = 404
res.end('404 - Not Found')
}
}