2018-10-30 20:42:53 +00:00
|
|
|
import path from 'path'
|
2018-12-09 10:42:22 +00:00
|
|
|
import consola from 'consola'
|
2018-10-30 20:42:53 +00:00
|
|
|
import launchMiddleware from 'launch-editor-middleware'
|
|
|
|
import serveStatic from 'serve-static'
|
2018-11-14 19:32:07 +00:00
|
|
|
import servePlaceholder from 'serve-placeholder'
|
2018-10-30 20:42:53 +00:00
|
|
|
import connect from 'connect'
|
2018-12-22 21:05:13 +00:00
|
|
|
import { determineGlobals, isUrl } from '@nuxt/utils'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
import ServerContext from './context'
|
|
|
|
import renderAndGetWindow from './jsdom'
|
|
|
|
import nuxtMiddleware from './middleware/nuxt'
|
|
|
|
import errorMiddleware from './middleware/error'
|
2018-11-08 09:15:56 +00:00
|
|
|
import Listener from './listener'
|
2018-11-26 22:49:47 +00:00
|
|
|
import createModernMiddleware from './middleware/modern'
|
2019-01-19 18:14:54 +00:00
|
|
|
import createTimingMiddleware from './middleware/timing'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
export default class Server {
|
|
|
|
constructor(nuxt) {
|
|
|
|
this.nuxt = nuxt
|
|
|
|
this.options = nuxt.options
|
|
|
|
|
|
|
|
this.globals = determineGlobals(nuxt.options.globalName, nuxt.options.globals)
|
|
|
|
|
|
|
|
this.publicPath = isUrl(this.options.build.publicPath)
|
|
|
|
? this.options.build._publicPath
|
|
|
|
: this.options.build.publicPath
|
|
|
|
|
|
|
|
// Runtime shared resources
|
|
|
|
this.resources = {}
|
|
|
|
|
|
|
|
// Will be available on dev
|
2018-11-07 23:37:06 +00:00
|
|
|
this.devMiddleware = null
|
|
|
|
this.hotMiddleware = null
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
// Will be set after listen
|
|
|
|
this.listeners = []
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Create new connect instance
|
|
|
|
this.app = connect()
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
// Close hook
|
|
|
|
this.nuxt.hook('close', () => this.close())
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async ready() {
|
|
|
|
await this.nuxt.callHook('render:before', this, this.options.render)
|
|
|
|
|
|
|
|
// Initialize vue-renderer
|
|
|
|
const { VueRenderer } = await import('@nuxt/vue-renderer')
|
|
|
|
|
|
|
|
const context = new ServerContext(this)
|
|
|
|
this.renderer = new VueRenderer(context)
|
|
|
|
await this.renderer.ready()
|
|
|
|
|
|
|
|
// Setup nuxt middleware
|
|
|
|
await this.setupMiddleware()
|
|
|
|
|
|
|
|
// Call done hook
|
|
|
|
await this.nuxt.callHook('render:done', this)
|
|
|
|
}
|
|
|
|
|
|
|
|
async setupMiddleware() {
|
|
|
|
// Apply setupMiddleware from modules first
|
|
|
|
await this.nuxt.callHook('render:setupMiddleware', this.app)
|
|
|
|
|
|
|
|
// Compression middleware for production
|
|
|
|
if (!this.options.dev) {
|
2019-01-17 21:18:29 +00:00
|
|
|
const { compressor } = this.options.render
|
2018-10-30 20:42:53 +00:00
|
|
|
if (typeof compressor === 'object') {
|
|
|
|
// If only setting for `compression` are provided, require the module and insert
|
|
|
|
const compression = this.nuxt.resolver.requireModule('compression')
|
|
|
|
this.useMiddleware(compression(compressor))
|
2018-11-21 14:33:03 +00:00
|
|
|
} else if (compressor) {
|
|
|
|
// Else, require own compression middleware if compressor is actually truthy
|
2018-10-30 20:42:53 +00:00
|
|
|
this.useMiddleware(compressor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 18:14:54 +00:00
|
|
|
if (this.options.server.timing) {
|
|
|
|
this.useMiddleware(createTimingMiddleware(this.options.server.timing))
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:49:47 +00:00
|
|
|
const modernMiddleware = createModernMiddleware({
|
|
|
|
context: this.renderer.context
|
|
|
|
})
|
2018-11-07 23:37:06 +00:00
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Add webpack middleware support only for development
|
|
|
|
if (this.options.dev) {
|
2018-11-26 22:49:47 +00:00
|
|
|
this.useMiddleware(modernMiddleware)
|
2018-10-30 20:42:53 +00:00
|
|
|
this.useMiddleware(async (req, res, next) => {
|
2018-12-24 16:26:39 +00:00
|
|
|
const name = req.devModernMode ? 'modern' : 'client'
|
2018-12-11 15:20:44 +00:00
|
|
|
if (this.devMiddleware && this.devMiddleware[name]) {
|
2018-11-07 23:37:06 +00:00
|
|
|
await this.devMiddleware[name](req, res)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-11 15:20:44 +00:00
|
|
|
if (this.hotMiddleware && this.hotMiddleware[name]) {
|
2018-11-07 23:37:06 +00:00
|
|
|
await this.hotMiddleware[name](req, res)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// open in editor for debug mode only
|
|
|
|
if (this.options.debug && this.options.dev) {
|
|
|
|
this.useMiddleware({
|
|
|
|
path: '__open-in-editor',
|
|
|
|
handler: launchMiddleware(this.options.editor)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// For serving static/ files to /
|
|
|
|
const staticMiddleware = serveStatic(
|
|
|
|
path.resolve(this.options.srcDir, this.options.dir.static),
|
|
|
|
this.options.render.static
|
|
|
|
)
|
|
|
|
staticMiddleware.prefix = this.options.render.static.prefix
|
|
|
|
this.useMiddleware(staticMiddleware)
|
|
|
|
|
|
|
|
// Serve .nuxt/dist/client files only for production
|
|
|
|
// For dev they will be served with devMiddleware
|
|
|
|
if (!this.options.dev) {
|
|
|
|
const distDir = path.resolve(this.options.buildDir, 'dist', 'client')
|
|
|
|
this.useMiddleware({
|
|
|
|
path: this.publicPath,
|
|
|
|
handler: serveStatic(
|
|
|
|
distDir,
|
|
|
|
this.options.render.dist
|
|
|
|
)
|
|
|
|
})
|
2018-11-26 22:49:47 +00:00
|
|
|
this.useMiddleware(modernMiddleware)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 19:32:07 +00:00
|
|
|
// Add user provided middleware
|
2019-01-04 19:30:28 +00:00
|
|
|
for (const m of this.options.serverMiddleware) {
|
2018-10-30 20:42:53 +00:00
|
|
|
this.useMiddleware(m)
|
2019-01-04 19:30:28 +00:00
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-11-14 19:32:07 +00:00
|
|
|
const { fallback } = this.options.render
|
|
|
|
if (fallback) {
|
|
|
|
// Graceful 404 errors for dist files
|
|
|
|
if (fallback.dist) {
|
|
|
|
this.useMiddleware({
|
|
|
|
path: this.publicPath,
|
|
|
|
handler: servePlaceholder(fallback.dist)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graceful 404 errors for other paths
|
|
|
|
if (fallback.static) {
|
|
|
|
this.useMiddleware({
|
|
|
|
path: '/',
|
|
|
|
handler: servePlaceholder(fallback.static)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Finally use nuxtMiddleware
|
|
|
|
this.useMiddleware(nuxtMiddleware({
|
|
|
|
options: this.options,
|
|
|
|
nuxt: this.nuxt,
|
|
|
|
renderRoute: this.renderRoute.bind(this),
|
|
|
|
resources: this.resources
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Error middleware for errors that occurred in middleware that declared above
|
|
|
|
// Middleware should exactly take 4 arguments
|
|
|
|
// https://github.com/senchalabs/connect#error-middleware
|
|
|
|
|
|
|
|
// Apply errorMiddleware from modules first
|
|
|
|
await this.nuxt.callHook('render:errorMiddleware', this.app)
|
|
|
|
|
|
|
|
// Apply errorMiddleware from Nuxt
|
|
|
|
this.useMiddleware(errorMiddleware({
|
|
|
|
resources: this.resources,
|
|
|
|
options: this.options
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
useMiddleware(middleware) {
|
2018-12-09 10:42:22 +00:00
|
|
|
let handler = middleware.handler || middleware
|
|
|
|
|
|
|
|
// Resolve handler setup as string (path)
|
|
|
|
if (typeof handler === 'string') {
|
|
|
|
try {
|
2018-12-29 21:34:47 +00:00
|
|
|
const requiredModuleFromHandlerPath = this.nuxt.resolver.requireModule(handler)
|
|
|
|
|
|
|
|
// In case the "handler" is not derived from an object but is a normal string, another object with
|
|
|
|
// path and handler could be the result
|
|
|
|
|
|
|
|
// If the required module has handler, treat the module as new "middleware" object
|
|
|
|
if (requiredModuleFromHandlerPath.handler) {
|
|
|
|
middleware = requiredModuleFromHandlerPath
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = requiredModuleFromHandlerPath.handler || requiredModuleFromHandlerPath
|
2018-12-09 10:42:22 +00:00
|
|
|
} catch (err) {
|
2019-01-04 19:30:28 +00:00
|
|
|
consola.error(err)
|
|
|
|
// Throw error in production mode
|
2018-12-09 22:00:48 +00:00
|
|
|
if (!this.options.dev) {
|
2019-01-04 19:30:28 +00:00
|
|
|
throw err
|
2018-12-09 22:00:48 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve path
|
|
|
|
const path = (
|
|
|
|
(middleware.prefix !== false ? this.options.router.base : '') +
|
|
|
|
(typeof middleware.path === 'string' ? middleware.path : '')
|
|
|
|
).replace(/\/\//g, '/')
|
|
|
|
|
|
|
|
// Use middleware
|
|
|
|
this.app.use(path, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRoute() {
|
|
|
|
return this.renderer.renderRoute.apply(this.renderer, arguments)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadResources() {
|
|
|
|
return this.renderer.loadResources.apply(this.renderer, arguments)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAndGetWindow(url, opts = {}) {
|
|
|
|
return renderAndGetWindow(url, opts, {
|
|
|
|
loadedCallback: this.globals.loadedCallback,
|
|
|
|
ssr: this.options.render.ssr,
|
|
|
|
globals: this.globals
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
async listen(port, host, socket) {
|
|
|
|
// Create a new listener
|
|
|
|
const listener = new Listener({
|
2019-01-19 12:23:18 +00:00
|
|
|
port: isNaN(parseInt(port)) ? this.options.server.port : port,
|
2018-11-08 09:15:56 +00:00
|
|
|
host: host || this.options.server.host,
|
|
|
|
socket: socket || this.options.server.socket,
|
|
|
|
https: this.options.server.https,
|
2018-11-30 15:32:15 +00:00
|
|
|
app: this.app,
|
|
|
|
dev: this.options.dev
|
2018-11-08 09:15:56 +00:00
|
|
|
})
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
// Listen
|
|
|
|
await listener.listen()
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
// Push listener to this.listeners
|
|
|
|
this.listeners.push(listener)
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
await this.nuxt.callHook('listen', listener.server, listener)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
async close() {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this.__closed) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
this.__closed = true
|
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
await Promise.all(this.listeners.map(l => l.close()))
|
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
this.listeners = []
|
|
|
|
|
|
|
|
if (typeof this.renderer.close === 'function') {
|
|
|
|
await this.renderer.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.app.removeAllListeners()
|
|
|
|
this.app = null
|
|
|
|
|
|
|
|
for (const key in this.resources) {
|
|
|
|
delete this.resources[key]
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|