Skip to content

Commit

Permalink
Add next configuration as an option for custom servers (#2058)
Browse files Browse the repository at this point in the history
* Add a configuration parameter to custom server startup

* Adding related documentation

* Do not access filesystem if configuration is supplied

* Make the configuration log clearer

* Make the conf default value to `null`
  • Loading branch information
reel authored and arunoda committed May 31, 2017
1 parent e6f3651 commit 442c611
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ Supported options:
- `dev` (`bool`) whether to launch Next.js in dev mode - default `false`
- `dir` (`string`) where the Next project is located - default `'.'`
- `quiet` (`bool`) Hide error messages containing server information - default `false`
- `conf` (`object`) the same object you would use in `next.config.js` - default `{}`

Then, change your `start` script to `NODE_ENV=production node server.js`.

Expand Down
2 changes: 1 addition & 1 deletion server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
}

if (config.webpack) {
console.log('> Using "webpack" config function defined in next.config.js.')
console.log(`> Using "webpack" config function defined in ${config.configOrigin}.`)
webpackConfig = await config.webpack(webpackConfig, { dev })
}
return webpack(webpackConfig)
Expand Down
18 changes: 14 additions & 4 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ const defaultConfig = {
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true
}

export default function getConfig (dir) {
export default function getConfig (dir, customConfig) {
if (!cache.has(dir)) {
cache.set(dir, loadConfig(dir))
cache.set(dir, loadConfig(dir, customConfig))
}
return cache.get(dir)
}

function loadConfig (dir) {
function loadConfig (dir, customConfig) {
if (customConfig && typeof customConfig === 'object') {
customConfig.configOrigin = 'server'
return withDefaults(customConfig)
}
const path = join(dir, 'next.config.js')

let userConfig = {}
Expand All @@ -28,7 +33,12 @@ function loadConfig (dir) {
if (userHasConfig) {
const userConfigModule = require(path)
userConfig = userConfigModule.default || userConfigModule
userConfig.configOrigin = 'next.config.js'
}

return Object.assign({}, defaultConfig, userConfig)
return withDefaults(userConfig)
}

function withDefaults (config) {
return Object.assign({}, defaultConfig, config)
}
6 changes: 3 additions & 3 deletions server/hot-reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import clean from './build/clean'
import getConfig from './config'

export default class HotReloader {
constructor (dir, { quiet } = {}) {
constructor (dir, { quiet, conf } = {}) {
this.dir = dir
this.quiet = quiet
this.middlewares = []
Expand All @@ -22,7 +22,7 @@ export default class HotReloader {
this.prevFailedChunkNames = null
this.prevChunkHashes = null

this.config = getConfig(dir)
this.config = getConfig(dir, conf)
}

async run (req, res) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class HotReloader {
}

if (this.config.webpackDevMiddleware) {
console.log('> Using "webpackDevMiddleware" config function defined in next.config.js.')
console.log(`> Using "webpackDevMiddleware" config function defined in ${this.config.configOrigin}.`)
webpackDevMiddlewareConfig = this.config.webpackDevMiddleware(webpackDevMiddlewareConfig)
}

Expand Down
6 changes: 3 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ const internalPrefixes = [
]

export default class Server {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
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 ? new HotReloader(this.dir, { quiet }) : null
this.hotReloader = dev ? new HotReloader(this.dir, { quiet, conf }) : null
this.http = null
this.config = getConfig(this.dir)
this.config = getConfig(this.dir, conf)
this.dist = this.config.distDir
this.buildStats = !dev ? require(join(this.dir, this.dist, 'build-stats.json')) : null
this.buildId = !dev ? this.readBuildId() : '-'
Expand Down

0 comments on commit 442c611

Please sign in to comment.