Skip to content

Commit 47553b6

Browse files
authored
Serve web worker assets with cache-busting (#10831)
Serve code editor worker files with proper cache-busting via buildHash in the URL path, following the same pattern used for application bundles. Worker files are now served at /{buildHash}/editor/workers/* where buildHash is derived from env.packageInfo.buildNum: - Development: Number.MAX_SAFE_INTEGER (9007199254740991) - Distribution: Actual build number from package.json This ensures browsers and CDNs fetch fresh worker files when OSD is upgraded, preventing stale cache issues that could cause version mismatches between application code and worker threads. Changes: - Add worker_config.ts with buildHash-based URL generation helpers - Register static worker route at /{buildHash}/editor/workers/* - Add test coverage for worker route registration Signed-off-by: Daniel Rowe <[email protected]>
1 parent 288ca5a commit 47553b6

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

packages/osd-monaco/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ import * as BarePluginApi from 'monaco-editor/esm/vs/editor/editor.api';
3838
export { BarePluginApi };
3939
import './monaco_environment';
4040
export * from './worker_store';
41+
export { WORKER_FILES, getWorkerUrl, getWorkerUrls, WorkerId } from './worker_config';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* Worker filenames (relative paths).
8+
* Monaco package only defines its worker files, not server routing.
9+
*/
10+
export const WORKER_FILES = {
11+
ppl: 'ppl.editor.worker.js',
12+
json: 'json.editor.worker.js',
13+
xjson: 'xjson.editor.worker.js',
14+
} as const;
15+
16+
export type WorkerId = keyof typeof WORKER_FILES;
17+
18+
/**
19+
* Get the full URL for a worker by its ID.
20+
*
21+
* @param workerId - The worker identifier
22+
* @param buildHash - The build hash for cache-busting (e.g., buildNum.toString())
23+
* @returns The full URL for the worker, or undefined if not found
24+
*/
25+
export function getWorkerUrl(workerId: string, buildHash: string): string | undefined {
26+
const filename = WORKER_FILES[workerId as WorkerId];
27+
return filename ? `/${buildHash}/editor/workers/${filename}` : undefined;
28+
}
29+
30+
/**
31+
* Helper to generate all worker URLs with the given buildHash.
32+
*
33+
* @param buildHash - The build hash for cache-busting (e.g., buildNum.toString())
34+
* @returns Object with all worker URLs
35+
*/
36+
export function getWorkerUrls(buildHash: string) {
37+
return {
38+
ppl: `/${buildHash}/editor/workers/${WORKER_FILES.ppl}`,
39+
json: `/${buildHash}/editor/workers/${WORKER_FILES.json}`,
40+
xjson: `/${buildHash}/editor/workers/${WORKER_FILES.xjson}`,
41+
} as const;
42+
}

src/core/server/core_app/core_app.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,15 @@ describe('CoreApp', () => {
7979
);
8080
});
8181
});
82+
83+
describe('static routes', () => {
84+
it('registers code editor worker static directory with buildHash', () => {
85+
coreApp.setup(internalCoreSetup);
86+
87+
expect(internalCoreSetup.http.registerStaticDir).toHaveBeenCalledWith(
88+
expect.stringMatching(/^\/\d+\/editor\/workers\/\{path\*\}$/),
89+
expect.stringContaining('node_modules/@osd/monaco/target/public')
90+
);
91+
});
92+
});
8293
});

src/core/server/core_app/core_app.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ import { Logger } from '../logging';
3838
/** @internal */
3939
export class CoreApp {
4040
private readonly logger: Logger;
41+
private readonly coreContext: CoreContext;
4142
constructor(core: CoreContext) {
43+
this.coreContext = core;
4244
this.logger = core.logger.get('core-app');
4345
}
4446
setup(coreSetup: InternalCoreSetup) {
@@ -94,5 +96,14 @@ export class CoreApp {
9496
'/node_modules/@osd/ui-framework/dist/{path*}',
9597
fromRoot('node_modules/@osd/ui-framework/dist')
9698
);
99+
100+
// Register worker files directory for code editor with buildHash for cache-busting
101+
// Serves from: build/opensearch-dashboards/node_modules/@osd/monaco/target/public/
102+
// Uses buildNum for cache invalidation on version changes (same pattern as bundles)
103+
const buildHash = this.coreContext.env.packageInfo.buildNum.toString();
104+
coreSetup.http.registerStaticDir(
105+
`/${buildHash}/editor/workers/{path*}`,
106+
fromRoot('node_modules/@osd/monaco/target/public')
107+
);
97108
}
98109
}

0 commit comments

Comments
 (0)