From 5d8239f40cbc1dc37b7fc50efb616cd363b33e7f Mon Sep 17 00:00:00 2001 From: Sven Wagner Date: Tue, 5 Mar 2019 06:55:48 +0100 Subject: [PATCH] feat(server): add option to control the default behaviour ...like serving static files or handlinng compression. Now you are able to set all your lovely custom express middleware and do compression or serving static files on your own. --- docs/guide/configuration.md | 2 ++ lib/app.js | 38 +++++++++++++++++++------------------ lib/config.js | 1 + 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index bcda274..84cc03d 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -22,6 +22,8 @@ module.exports = { skipRequests: req => req.originalUrl === '/graphql', // See https://ssr.vuejs.org/guide/build-config.html#externals-caveats nodeExternalsWhitelist: [/\.css$/, /\?vue&type=style/], + // apply default middleware like compression, serving static files + applyDefaultServer: true, // Function to connect custom middlewares extendServer: app => { const cookieParser = require('cookie-parser') diff --git a/lib/app.js b/lib/app.js index 4702776..53c132e 100644 --- a/lib/app.js +++ b/lib/app.js @@ -70,26 +70,28 @@ module.exports = (app, options) => { }) } - // Serve static files - const serve = (filePath, cache) => express.static(filePath, { - maxAge: cache && isProd ? 1000 * 60 * 60 * 24 * 30 : 0, - index: false, - }) + if(config.applyDefaultServer){ + // Serve static files + const serve = (filePath, cache) => express.static(filePath, { + maxAge: cache && isProd ? 1000 * 60 * 60 * 24 * 30 : 0, + index: false, + }) - // Serve static files - app.use(compression({ threshold: 0 })) - app.use(favicon(config.favicon)) - if (config.api.hasPlugin('pwa')) { - app.use('/service-worker.js', serve(config.serviceWorkerPath, true)) - } - const serveStaticFiles = serve(config.distPath, true) - app.use((req, res, next) => { - if (/index\.html/g.test(req.path)) { - next() - } else { - serveStaticFiles(req, res, next) + // Serve static files + app.use(compression({ threshold: 0 })) + app.use(favicon(config.favicon)) + if (config.api.hasPlugin('pwa')) { + app.use('/service-worker.js', serve(config.serviceWorkerPath, true)) } - }) + const serveStaticFiles = serve(config.distPath, true) + app.use((req, res, next) => { + if (/index\.html/g.test(req.path)) { + next() + } else { + serveStaticFiles(req, res, next) + } + }) + } if (config.extendServer) { config.extendServer(app) diff --git a/lib/config.js b/lib/config.js index db57255..cc7653c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -8,6 +8,7 @@ module.exports = { favicon: './public/favicon.ico', skipRequests: req => req.originalUrl === '/graphql', nodeExternalsWhitelist: [/\.css$/, /\?vue&type=style/], + applyDefaultServer: true, extendServer: null, // Paths distPath: null,