Skip to content

Commit 066acca

Browse files
committed
Move devtool view context into separate function
1 parent f712fbd commit 066acca

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

src/server/plugins/engine/configureEnginePlugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type PluginOptions
1010
} from '~/src/server/plugins/engine/plugin.js'
1111
import { findPackageRoot } from '~/src/server/plugins/engine/plugin.js'
12+
import { devtoolContext } from '~/src/server/plugins/nunjucks/context.js'
1213
import { type RouteConfig } from '~/src/server/types.js'
1314

1415
export const configureEnginePlugin = async ({
@@ -36,9 +37,7 @@ export const configureEnginePlugin = async ({
3637
nunjucks: {
3738
paths: [join(findPackageRoot(), 'src/server/devserver')] // custom layout to make it really clear this is not the same as the runner
3839
},
39-
viewContext: () => ({
40-
baseLayoutPath: 'dxt-devtool-baselayout.html' // from plugin.options.nunjucks.paths
41-
})
40+
viewContext: devtoolContext
4241
}
4342
}
4443
}

src/server/plugins/nunjucks/context.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ let webpackManifest
2222
* @param {FormRequest | FormRequestPayload | null} request
2323
*/
2424
export function context(request) {
25-
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
26-
27-
if (!webpackManifest) {
28-
try {
29-
// eslint-disable-next-line -- Allow JSON type 'any'
30-
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
31-
} catch {
32-
logger.error(`Webpack ${basename(manifestPath)} not found`)
33-
}
34-
}
35-
3625
const { params, path, response } = request ?? {}
3726

3827
const isPreviewMode = path?.startsWith(PREVIEW_PATH_PREFIX)
@@ -54,7 +43,6 @@ export function context(request) {
5443
// take consumers props first so we can override it
5544
...consumerViewContext,
5645
appVersion: pkg.version,
57-
assetPath: '/assets',
5846
config: {
5947
cdpEnvironment: config.get('cdpEnvironment'),
6048
designerUrl: config.get('designerUrl'),
@@ -66,14 +54,34 @@ export function context(request) {
6654
crumb: safeGenerateCrumb(request),
6755
currentPath: request ? `${request.path}${request.url.search}` : undefined,
6856
previewMode: isPreviewMode ? params?.state : undefined,
69-
slug: isResponseOK ? params?.slug : undefined,
57+
slug: isResponseOK ? params?.slug : undefined
58+
}
59+
60+
return ctx
61+
}
7062

63+
/**
64+
* Returns the context for the devtool. Consumers won't have access to this.
65+
*/
66+
export function devtoolContext() {
67+
const manifestPath = join(config.get('publicDir'), 'assets-manifest.json')
68+
69+
if (!webpackManifest) {
70+
try {
71+
// eslint-disable-next-line -- Allow JSON type 'any'
72+
webpackManifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
73+
} catch {
74+
logger.error(`Webpack ${basename(manifestPath)} not found`)
75+
}
76+
}
77+
78+
return {
79+
baseLayoutPath: 'dxt-devtool-baselayout.html', // from plugin.options.nunjucks.paths
80+
assetPath: '/assets',
7181
getDxtAssetPath: (asset = '') => {
7282
return `/${webpackManifest?.[asset] ?? asset}`
7383
}
7484
}
75-
76-
return ctx
7785
}
7886

7987
/**

src/server/plugins/nunjucks/types.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
/**
1313
* @typedef {object} ViewContext - Nunjucks view context
1414
* @property {string} appVersion - Application version
15-
* @property {string} assetPath - Asset path
1615
* @property {Partial<Config>} config - Application config properties
1716
* @property {string} [crumb] - Cross-Site Request Forgery (CSRF) token
1817
* @property {string} [cspNonce] - Content Security Policy (CSP) nonce
1918
* @property {string} [currentPath] - Current path
2019
* @property {string} [previewMode] - Preview mode
2120
* @property {string} [slug] - Form slug
22-
* @property {(asset?: string) => string} getDxtAssetPath - Asset path resolver
2321
* @property {FormContext} [context] - the current form context
2422
* @property {PluginOptions['viewContext']} [injectedViewContext] - the current form context
2523
*/

0 commit comments

Comments
 (0)