Skip to content

Commit

Permalink
Delay adding filters and functions until last
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Oct 27, 2023
1 parent 3da894c commit adb43eb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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
Expand Down
3 changes: 2 additions & 1 deletion lib/filters/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ module.exports = {
addFilter,
getFilter
},
setEnvironment
setEnvironment,
runWhenEnvIsAvailable
}
10 changes: 6 additions & 4 deletions lib/filters/core-filters.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.
Expand All @@ -10,4 +9,7 @@ const nunjucksSafe = getFilter('safe')
* @example {{ "hello world" | log }}
* @example {{ "hello world" | log | safe }} [for environments with autoescaping turned on]
*/
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
runWhenEnvIsAvailable(() => {
const nunjucksSafe = getFilter('safe')
addFilter('log', a => nunjucksSafe('<script>console.log(' + JSON.stringify(a, null, '\t') + ');</script>'))
})
58 changes: 29 additions & 29 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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')

Expand All @@ -120,19 +110,19 @@ 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.
res.setHeader('X-Robots-Tag', 'noindex')
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')
Expand Down Expand Up @@ -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

0 comments on commit adb43eb

Please sign in to comment.