diff --git a/CHANGELOG.md b/CHANGELOG.md
index 307f75d747..698621ee9c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
### Fixes
+- [#2362: Delay adding filters and functions until last](https://github.com/alphagov/govuk-prototype-kit/pull/2362)
- [#2364: Fix plugin update detection](https://github.com/alphagov/govuk-prototype-kit/pull/2364)
## 13.13.5
diff --git a/lib/filters/api.js b/lib/filters/api.js
index 4570e33418..f1565299fd 100644
--- a/lib/filters/api.js
+++ b/lib/filters/api.js
@@ -56,5 +56,6 @@ module.exports = {
addFilter,
getFilter
},
- setEnvironment
+ setEnvironment,
+ runWhenEnvIsAvailable
}
diff --git a/lib/filters/core-filters.js b/lib/filters/core-filters.js
index ce55f0c479..7dacb78091 100644
--- a/lib/filters/core-filters.js
+++ b/lib/filters/core-filters.js
@@ -1,8 +1,7 @@
// local dependencies
-const { addFilter, getFilter } = require('../../').views
-
-const nunjucksSafe = getFilter('safe')
+const { runWhenEnvIsAvailable, external } = require('./api')
+const { addFilter, getFilter } = external
/**
* Logs an object in the template to the console in the browser.
@@ -11,4 +10,7 @@ const nunjucksSafe = getFilter('safe')
* @example {{ "hello world" | log }}
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
*/
-addFilter('log', a => nunjucksSafe(''))
+runWhenEnvIsAvailable(() => {
+ const nunjucksSafe = getFilter('safe')
+ addFilter('log', a => nunjucksSafe(''))
+})
diff --git a/server.js b/server.js
index b801d1a0c5..f211ef8048 100644
--- a/server.js
+++ b/server.js
@@ -31,6 +31,19 @@ routesApi.setApp(app)
// Set up configuration variables
const releaseVersion = packageJson.version
+// Find GOV.UK Frontend (via project, internal package fallback)
+const govukFrontend = govukFrontendPaths([projectDir, packageDir])
+
+// Find GOV.UK Frontend (via internal package, project fallback)
+const govukFrontendInternal = govukFrontendPaths([packageDir, projectDir])
+
+// Finds GOV.UK Frontend via `getAppViews()` only if installed
+// but uses the internal package as a backup if uninstalled
+const nunjucksAppEnv = getNunjucksAppEnv(
+ plugins.getAppViews([appViewsDir, finalBackupNunjucksDir]),
+ govukFrontendInternal
+)
+
// Force HTTPS on production. Do this before using basicAuth to avoid
// asking for username/password twice (for `http`, then `https`).
const isSecure = (config.isProduction && config.useHttps)
@@ -39,12 +52,6 @@ if (isSecure) {
app.set('trust proxy', 1) // needed for secure cookies on heroku
}
-// Find GOV.UK Frontend (via project, internal package fallback)
-const govukFrontend = govukFrontendPaths([projectDir, packageDir])
-
-// Find GOV.UK Frontend (via internal package, project fallback)
-const govukFrontendInternal = govukFrontendPaths([packageDir, projectDir])
-
// Add variables that are available in all views
app.locals.asset_path = '/public/'
app.locals.useAutoStoreData = config.useAutoStoreData
@@ -90,23 +97,6 @@ if (config.isDevelopment) {
nunjucksConfig.watch = true
}
-nunjucksConfig.express = app
-
-// Finds GOV.UK Frontend via `getAppViews()` only if installed
-// but uses the internal package as a backup if uninstalled
-const nunjucksAppEnv = getNunjucksAppEnv(
- plugins.getAppViews([appViewsDir, finalBackupNunjucksDir]),
- govukFrontendInternal
-)
-
-expressNunjucks(nunjucksAppEnv, app)
-
-// Add Nunjucks filters
-utils.addNunjucksFilters(nunjucksAppEnv)
-
-// Add Nunjucks functions
-utils.addNunjucksFunctions(nunjucksAppEnv)
-
// Set views engine
app.set('view engine', 'njk')
@@ -120,12 +110,6 @@ app.use(bodyParser.urlencoded({
extended: true
}))
-// Automatically store all data users enter
-if (config.useAutoStoreData) {
- app.use(sessionUtils.autoStoreData)
- sessionUtils.addCheckedFunction(nunjucksAppEnv)
-}
-
// Prevent search indexing
app.use((req, res, next) => {
// Setting headers stops pages being indexed even if indexed pages link to them.
@@ -133,6 +117,12 @@ app.use((req, res, next) => {
next()
})
+// Automatically store all data users enter
+if (config.useAutoStoreData) {
+ app.use(sessionUtils.autoStoreData)
+ sessionUtils.addCheckedFunction(nunjucksAppEnv)
+}
+
require('./lib/manage-prototype-routes.js')
require('./lib/plugins/plugins-routes.js')
const { getErrorModel } = require('./lib/utils/errorModel')
@@ -217,4 +207,14 @@ app.use((err, req, res, next) => {
app.close = stopWatchingNunjucks
+nunjucksConfig.express = app
+
+expressNunjucks(nunjucksAppEnv, app)
+
+// Add Nunjucks filters
+utils.addNunjucksFilters(nunjucksAppEnv)
+
+// Add Nunjucks functions
+utils.addNunjucksFunctions(nunjucksAppEnv)
+
module.exports = app