diff --git a/CHANGELOG.md b/CHANGELOG.md index 010160dc06..c1cf2f70af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ - [#2384: Add format items filter to core filters](https://github.com/alphagov/govuk-prototype-kit/pull/2384) - [#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 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