Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
40 changes: 21 additions & 19 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down