Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8ca12d2

Browse files
committedDec 1, 2023
Delay adding filters and functions until last
1 parent 03d1bc4 commit 8ca12d2

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- [#2384: Add format items filter to core filters](https://github.com/alphagov/govuk-prototype-kit/pull/2384)
1010
- [#2382: Make any npm module a plugin via a proxy plugin config](https://github.com/alphagov/govuk-prototype-kit/pull/2382)
1111

12+
### Fixes
13+
14+
- [#2362: Delay adding filters and functions until last](https://github.com/alphagov/govuk-prototype-kit/pull/2362)
15+
1216
## 13.15.3
1317

1418
### Fixes

‎server.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ routesApi.setApp(app)
3131
// Set up configuration variables
3232
const releaseVersion = packageJson.version
3333

34+
// Find GOV.UK Frontend (via project, internal package fallback)
35+
const govukFrontend = govukFrontendPaths([projectDir, packageDir])
36+
37+
// Find GOV.UK Frontend (via internal package, project fallback)
38+
const govukFrontendInternal = govukFrontendPaths([packageDir, projectDir])
39+
40+
// Finds GOV.UK Frontend via `getAppViews()` only if installed
41+
// but uses the internal package as a backup if uninstalled
42+
const nunjucksAppEnv = getNunjucksAppEnv(
43+
plugins.getAppViews([appViewsDir, finalBackupNunjucksDir]),
44+
govukFrontendInternal
45+
)
46+
3447
// Force HTTPS on production. Do this before using basicAuth to avoid
3548
// asking for username/password twice (for `http`, then `https`).
3649
const isSecure = (config.isProduction && config.useHttps)
@@ -39,12 +52,6 @@ if (isSecure) {
3952
app.set('trust proxy', 1) // needed for secure cookies on heroku
4053
}
4154

42-
// Find GOV.UK Frontend (via project, internal package fallback)
43-
const govukFrontend = govukFrontendPaths([projectDir, packageDir])
44-
45-
// Find GOV.UK Frontend (via internal package, project fallback)
46-
const govukFrontendInternal = govukFrontendPaths([packageDir, projectDir])
47-
4855
// Add variables that are available in all views
4956
app.locals.asset_path = '/public/'
5057
app.locals.useAutoStoreData = config.useAutoStoreData
@@ -90,23 +97,6 @@ if (config.isDevelopment) {
9097
nunjucksConfig.watch = true
9198
}
9299

93-
nunjucksConfig.express = app
94-
95-
// Finds GOV.UK Frontend via `getAppViews()` only if installed
96-
// but uses the internal package as a backup if uninstalled
97-
const nunjucksAppEnv = getNunjucksAppEnv(
98-
plugins.getAppViews([appViewsDir, finalBackupNunjucksDir]),
99-
govukFrontendInternal
100-
)
101-
102-
expressNunjucks(nunjucksAppEnv, app)
103-
104-
// Add Nunjucks filters
105-
utils.addNunjucksFilters(nunjucksAppEnv)
106-
107-
// Add Nunjucks functions
108-
utils.addNunjucksFunctions(nunjucksAppEnv)
109-
110100
// Set views engine
111101
app.set('view engine', 'njk')
112102

@@ -120,19 +110,19 @@ app.use(bodyParser.urlencoded({
120110
extended: true
121111
}))
122112

123-
// Automatically store all data users enter
124-
if (config.useAutoStoreData) {
125-
app.use(sessionUtils.autoStoreData)
126-
sessionUtils.addCheckedFunction(nunjucksAppEnv)
127-
}
128-
129113
// Prevent search indexing
130114
app.use((req, res, next) => {
131115
// Setting headers stops pages being indexed even if indexed pages link to them.
132116
res.setHeader('X-Robots-Tag', 'noindex')
133117
next()
134118
})
135119

120+
// Automatically store all data users enter
121+
if (config.useAutoStoreData) {
122+
app.use(sessionUtils.autoStoreData)
123+
sessionUtils.addCheckedFunction(nunjucksAppEnv)
124+
}
125+
136126
require('./lib/manage-prototype-routes.js')
137127
require('./lib/plugins/plugins-routes.js')
138128
const { getErrorModel } = require('./lib/utils/errorModel')
@@ -217,4 +207,14 @@ app.use((err, req, res, next) => {
217207

218208
app.close = stopWatchingNunjucks
219209

210+
nunjucksConfig.express = app
211+
212+
expressNunjucks(nunjucksAppEnv, app)
213+
214+
// Add Nunjucks filters
215+
utils.addNunjucksFilters(nunjucksAppEnv)
216+
217+
// Add Nunjucks functions
218+
utils.addNunjucksFunctions(nunjucksAppEnv)
219+
220220
module.exports = app

0 commit comments

Comments
 (0)
Please sign in to comment.