Skip to content

Commit 7d129ec

Browse files
yekverAkryum
authored andcommitted
feat: Allow extending context object (#58)
* add extendContext property to config * fix
1 parent b7c9394 commit 7d129ec

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

docs/guide/configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = {
3939
},
4040
// apply default middleware like compression, serving static files
4141
applyDefaultServer: true,
42+
// Function to extend app context object
43+
extendContext: (req, res, process) => ({ appMode: process.env.APP_MODE }),
4244
// Function to connect custom middlewares
4345
extendServer: app => {
4446
const cookieParser = require('cookie-parser')
@@ -53,7 +55,7 @@ module.exports = {
5355
},
5456
// Paths
5557
distPath: path.resolve(__dirname, './dist'),
56-
error500Html: null,
58+
error500Html: path.resolve(__dirname, './dist/500.html'),
5759
templatePath: path.resolve(__dirname, './dist/index.html'),
5860
serviceWorkerPath: path.resolve(__dirname, './dist/service-worker.js'),
5961
}

lib/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ module.exports = (app, options) => {
113113
const renderApp = (req, res) => {
114114
res.setHeader('Content-Type', 'text/html')
115115

116-
const context = {
116+
const context = Object.assign({
117117
req,
118118
url: req.url,
119119
title: config.defaultTitle,
120120
httpCode: 200,
121-
}
121+
}, config.extendContext && config.extendContext(req, res, process))
122+
122123
renderer.renderToString(context, (err, renderedHtml) => {
123124
let html = renderedHtml
124125

lib/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
skipRequests: req => req.originalUrl === '/graphql',
1313
nodeExternalsWhitelist: [/\.css$/, /\?vue&type=style/],
1414
applyDefaultServer: true,
15+
extendContext: null,
1516
extendServer: null,
1617
staticCacheTtl: 1000 * 60 * 60 * 24 * 30,
1718
directives: {},

0 commit comments

Comments
 (0)