diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index 898dfda..41b7f46 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -31,6 +31,8 @@ module.exports = { lruCacheOptions: { // See https://ssr.vuejs.org/guide/caching.html }, + // 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 e48d59e..af99bea 100644 --- a/lib/app.js +++ b/lib/app.js @@ -73,27 +73,29 @@ module.exports = (app, options) => { }) } - // Serve static files - const serve = (filePath, cache) => express.static(filePath, { - maxAge: cache && isProd ? config.staticCacheTtl : 0, - index: false, - }) + if(config.applyDefaultServer){ + // Serve static files + const serve = (filePath, cache) => express.static(filePath, { + maxAge: cache && isProd ? config.staticCacheTtl : 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) - const { publicPath } = config.service.projectOptions - app.use(publicPath, (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) + const { publicPath } = config.service.projectOptions + app.use(publicPath, (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 d9a2156..9f4de02 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, staticCacheTtl: 1000 * 60 * 60 * 24 * 30, directives: {},