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 Nov 27, 2023
1 parent 667abeb commit a6e23d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- [#2382: Make any npm module a plugin via a proxy plugin config](https://github.com/alphagov/govuk-prototype-kit/pull/2382)

### Fixes

- [#2362: Delay adding filters and functions until last](https://github.com/alphagov/govuk-prototype-kit/pull/2362)

## 13.15.3

### Fixes
Expand Down
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 a6e23d8

Please sign in to comment.