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'
|
2020-12-22 17:07:50 +00:00
|
|
|
import compression from 'compression'
|
2020-11-09 17:17:32 +00:00
|
|
|
import { determineGlobals, isUrl, urlJoin } from '@nuxt/utils'
|
2020-12-22 17:07:50 +00:00
|
|
|
import { VueRenderer } from '@nuxt/vue-renderer'
|
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'
|
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 {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (nuxt) {
|
2018-10-30 20:42:53 +00:00
|
|
|
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
|
2021-03-10 11:48:41 +00:00
|
|
|
: this.options.build.publicPath.replace(/^\.+\//, '/')
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
// Runtime shared resources
|
|
|
|
this.resources = {}
|
|
|
|
|
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())
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
// devMiddleware placeholder
|
|
|
|
if (this.options.dev) {
|
|
|
|
this.nuxt.hook('server:devMiddleware', (devMiddleware) => {
|
|
|
|
this.devMiddleware = devMiddleware
|
|
|
|
})
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async ready () {
|
2019-03-08 20:43:23 +00:00
|
|
|
if (this._readyCalled) {
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
this._readyCalled = true
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
await this.nuxt.callHook('render:before', this, this.options.render)
|
|
|
|
|
|
|
|
// Initialize vue-renderer
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext = new ServerContext(this)
|
|
|
|
this.renderer = new VueRenderer(this.serverContext)
|
2018-10-30 20:42:53 +00:00
|
|
|
await this.renderer.ready()
|
|
|
|
|
|
|
|
// Setup nuxt middleware
|
|
|
|
await this.setupMiddleware()
|
|
|
|
|
|
|
|
// Call done hook
|
|
|
|
await this.nuxt.callHook('render:done', this)
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
return this
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async setupMiddleware () {
|
2018-10-30 20:42:53 +00:00
|
|
|
// 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
|
|
|
|
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-10-30 20:42:53 +00:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
})
|
2019-03-08 20:43:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 09:17:53 +00:00
|
|
|
// Dev middleware
|
2019-03-08 20:43:23 +00:00
|
|
|
if (this.options.dev) {
|
|
|
|
this.useMiddleware((req, res, next) => {
|
|
|
|
if (!this.devMiddleware) {
|
|
|
|
return next()
|
|
|
|
}
|
2021-03-23 18:07:35 +00:00
|
|
|
// Safari over-caches JS (breaking HMR) and the seemingly only way to turn
|
|
|
|
// this off in dev mode is to set Vary: * header
|
|
|
|
// #3828, #9034
|
|
|
|
if (req.url.startsWith(this.publicPath) && req.url.endsWith('.js')) {
|
|
|
|
res.setHeader('Vary', '*')
|
|
|
|
}
|
2019-03-08 20:43:23 +00:00
|
|
|
this.devMiddleware(req, res, next)
|
|
|
|
})
|
|
|
|
|
|
|
|
// open in editor for debug mode only
|
|
|
|
if (this.options.debug) {
|
|
|
|
this.useMiddleware({
|
|
|
|
path: '__open-in-editor',
|
|
|
|
handler: launchMiddleware(this.options.editor)
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Graceful 404 error handler
|
2018-11-14 19:32:07 +00:00
|
|
|
const { fallback } = this.options.render
|
|
|
|
if (fallback) {
|
2019-03-08 20:43:23 +00:00
|
|
|
// Dist files
|
2018-11-14 19:32:07 +00:00
|
|
|
if (fallback.dist) {
|
|
|
|
this.useMiddleware({
|
|
|
|
path: this.publicPath,
|
|
|
|
handler: servePlaceholder(fallback.dist)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Other paths
|
2018-11-14 19:32:07 +00:00
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
2020-11-09 17:17:32 +00:00
|
|
|
// DX: redirect if router.base in development
|
2020-12-06 17:32:39 +00:00
|
|
|
const routerBase = this.nuxt.options.router.base
|
|
|
|
if (this.options.dev && routerBase !== '/') {
|
2020-11-09 17:17:32 +00:00
|
|
|
this.useMiddleware({
|
|
|
|
prefix: false,
|
2020-12-06 17:32:39 +00:00
|
|
|
handler: (req, res, next) => {
|
|
|
|
if (decodeURI(req.url).startsWith(decodeURI(routerBase))) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
const to = urlJoin(routerBase, req.url)
|
|
|
|
consola.info(`[Development] Redirecting from \`${decodeURI(req.url)}\` to \`${decodeURI(to)}\` (router.base specified)`)
|
2020-11-09 17:17:32 +00:00
|
|
|
res.writeHead(302, {
|
|
|
|
Location: to
|
|
|
|
})
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Apply errorMiddleware from modules first
|
|
|
|
await this.nuxt.callHook('render:errorMiddleware', this.app)
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Error middleware for errors that occurred in middleware that declared above
|
2018-10-30 20:42:53 +00:00
|
|
|
this.useMiddleware(errorMiddleware({
|
|
|
|
resources: this.resources,
|
|
|
|
options: this.options
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
_normalizeMiddleware (middleware) {
|
|
|
|
// Normalize plain function
|
|
|
|
if (typeof middleware === 'function') {
|
|
|
|
middleware = { handle: middleware }
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
// If a plain string provided as path to middleware
|
|
|
|
if (typeof middleware === 'string') {
|
|
|
|
middleware = this._requireMiddleware(middleware)
|
|
|
|
}
|
2018-12-29 21:34:47 +00:00
|
|
|
|
2021-01-12 12:17:20 +00:00
|
|
|
// #8584
|
|
|
|
// shallow clone the middleware before any change is made,
|
|
|
|
// in case any following mutation breaks when applied repeatedly.
|
|
|
|
middleware = Object.assign({}, middleware)
|
|
|
|
|
2020-04-14 09:26:40 +00:00
|
|
|
// Normalize handler to handle (backward compatibility)
|
2020-01-19 08:34:35 +00:00
|
|
|
if (middleware.handler && !middleware.handle) {
|
|
|
|
middleware.handle = middleware.handler
|
|
|
|
delete middleware.handler
|
|
|
|
}
|
2018-12-29 21:34:47 +00:00
|
|
|
|
2020-04-14 09:26:40 +00:00
|
|
|
// Normalize path to route (backward compatibility)
|
2020-01-19 08:34:35 +00:00
|
|
|
if (middleware.path && !middleware.route) {
|
|
|
|
middleware.route = middleware.path
|
|
|
|
delete middleware.path
|
|
|
|
}
|
2018-12-29 21:34:47 +00:00
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
// If handle is a string pointing to path
|
|
|
|
if (typeof middleware.handle === 'string') {
|
|
|
|
Object.assign(middleware, this._requireMiddleware(middleware.handle))
|
|
|
|
}
|
|
|
|
|
|
|
|
// No handle
|
|
|
|
if (!middleware.handle) {
|
|
|
|
middleware.handle = (req, res, next) => {
|
|
|
|
next(new Error('ServerMiddleware should expose a handle: ' + middleware.entry))
|
2018-12-09 10:42:22 +00:00
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 17:33:22 +00:00
|
|
|
// Prefix on handle (proxy-module)
|
|
|
|
if (middleware.handle.prefix !== undefined && middleware.prefix === undefined) {
|
|
|
|
middleware.prefix = middleware.handle.prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
// sub-app (express)
|
2020-01-20 18:28:52 +00:00
|
|
|
if (typeof middleware.handle.handle === 'function') {
|
|
|
|
const server = middleware.handle
|
|
|
|
middleware.handle = server.handle.bind(server)
|
|
|
|
}
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
return middleware
|
|
|
|
}
|
|
|
|
|
|
|
|
_requireMiddleware (entry) {
|
|
|
|
// Resolve entry
|
|
|
|
entry = this.nuxt.resolver.resolvePath(entry)
|
|
|
|
|
|
|
|
// Require middleware
|
|
|
|
let middleware
|
|
|
|
try {
|
|
|
|
middleware = this.nuxt.resolver.requireModule(entry)
|
|
|
|
} catch (error) {
|
|
|
|
// Show full error
|
|
|
|
consola.error('ServerMiddleware Error:', error)
|
|
|
|
|
|
|
|
// Placeholder for error
|
2020-01-20 18:28:52 +00:00
|
|
|
middleware = (req, res, next) => { next(error) }
|
2020-01-19 08:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
middleware = this._normalizeMiddleware(middleware)
|
|
|
|
|
|
|
|
// Set entry
|
|
|
|
middleware.entry = entry
|
|
|
|
|
|
|
|
return middleware
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:28:52 +00:00
|
|
|
resolveMiddleware (middleware, fallbackRoute = '/') {
|
2020-01-19 08:34:35 +00:00
|
|
|
// Ensure middleware is normalized
|
|
|
|
middleware = this._normalizeMiddleware(middleware)
|
|
|
|
|
2020-01-20 18:28:52 +00:00
|
|
|
// Fallback route
|
|
|
|
if (!middleware.route) {
|
|
|
|
middleware.route = fallbackRoute
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:17:20 +00:00
|
|
|
// #8584
|
|
|
|
// save the original route before applying defaults
|
|
|
|
middleware._originalRoute = middleware.route
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
// Resolve final route
|
|
|
|
middleware.route = (
|
2018-10-30 20:42:53 +00:00
|
|
|
(middleware.prefix !== false ? this.options.router.base : '') +
|
2020-01-19 08:34:35 +00:00
|
|
|
(typeof middleware.route === 'string' ? middleware.route : '')
|
2018-10-30 20:42:53 +00:00
|
|
|
).replace(/\/\//g, '/')
|
|
|
|
|
2020-03-27 21:30:43 +00:00
|
|
|
// Strip trailing slash
|
|
|
|
if (middleware.route.endsWith('/')) {
|
|
|
|
middleware.route = middleware.route.slice(0, -1)
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:26:40 +00:00
|
|
|
// Assign _middleware to handle to make accessible from app.stack
|
2020-01-19 08:34:35 +00:00
|
|
|
middleware.handle._middleware = middleware
|
|
|
|
|
|
|
|
return middleware
|
|
|
|
}
|
|
|
|
|
|
|
|
useMiddleware (middleware) {
|
|
|
|
const { route, handle } = this.resolveMiddleware(middleware)
|
2020-01-20 18:28:52 +00:00
|
|
|
this.app.use(route, handle)
|
2020-01-19 08:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceMiddleware (query, middleware) {
|
|
|
|
let serverStackItem
|
|
|
|
|
|
|
|
if (typeof query === 'string') {
|
|
|
|
// Search by entry
|
|
|
|
serverStackItem = this.app.stack.find(({ handle }) => handle._middleware && handle._middleware.entry === query)
|
|
|
|
} else {
|
|
|
|
// Search by reference
|
|
|
|
serverStackItem = this.app.stack.find(({ handle }) => handle === query)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop if item not found
|
|
|
|
if (!serverStackItem) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:16:09 +00:00
|
|
|
// unload middleware
|
|
|
|
this.unloadMiddleware(serverStackItem)
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
// Resolve middleware
|
2021-01-12 12:17:20 +00:00
|
|
|
const { route, handle } = this.resolveMiddleware(
|
|
|
|
middleware,
|
|
|
|
// #8584 pass the original route as fallback
|
|
|
|
serverStackItem.handle._middleware
|
|
|
|
? serverStackItem.handle._middleware._originalRoute
|
|
|
|
: serverStackItem.route
|
|
|
|
)
|
2020-01-19 08:34:35 +00:00
|
|
|
|
|
|
|
// Update serverStackItem
|
|
|
|
serverStackItem.handle = handle
|
|
|
|
|
|
|
|
// Error State
|
2020-03-27 21:30:43 +00:00
|
|
|
serverStackItem.route = route
|
2020-01-19 08:34:35 +00:00
|
|
|
|
|
|
|
// Return updated item
|
|
|
|
return serverStackItem
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:16:09 +00:00
|
|
|
unloadMiddleware ({ handle }) {
|
|
|
|
if (handle._middleware && typeof handle._middleware.unload === 'function') {
|
|
|
|
handle._middleware.unload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 08:34:35 +00:00
|
|
|
serverMiddlewarePaths () {
|
|
|
|
return this.app.stack.map(({ handle }) => handle._middleware && handle._middleware.entry).filter(Boolean)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
renderRoute () {
|
2018-10-30 20:42:53 +00:00
|
|
|
return this.renderer.renderRoute.apply(this.renderer, arguments)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
loadResources () {
|
2018-10-30 20:42:53 +00:00
|
|
|
return this.renderer.loadResources.apply(this.renderer, arguments)
|
|
|
|
}
|
|
|
|
|
2019-08-05 11:14:03 +00:00
|
|
|
renderAndGetWindow (url, opts = {}, {
|
|
|
|
loadingTimeout = 2000,
|
|
|
|
loadedCallback = this.globals.loadedCallback,
|
|
|
|
globals = this.globals
|
|
|
|
} = {}) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return renderAndGetWindow(url, opts, {
|
2019-08-05 11:14:03 +00:00
|
|
|
loadingTimeout,
|
|
|
|
loadedCallback,
|
|
|
|
globals
|
2018-10-30 20:42:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async listen (port, host, socket) {
|
2019-03-08 20:43:23 +00:00
|
|
|
// Ensure nuxt is ready
|
2019-03-08 12:20:03 +00:00
|
|
|
await this.nuxt.ready()
|
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
// 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,
|
2019-04-05 14:15:58 +00:00
|
|
|
dev: this.options.dev,
|
|
|
|
baseURL: this.options.router.base
|
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)
|
2019-05-25 15:18:36 +00:00
|
|
|
|
|
|
|
return listener
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2019-07-10 10:45:49 +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()
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:16:09 +00:00
|
|
|
this.app.stack.forEach(this.unloadMiddleware)
|
2018-12-09 10:42:22 +00:00
|
|
|
this.app.removeAllListeners()
|
|
|
|
this.app = null
|
|
|
|
|
|
|
|
for (const key in this.resources) {
|
|
|
|
delete this.resources[key]
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|