Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/webui-framework/src/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('template registry helpers', () => {
}
});

test('getTemplate lazily loads webui-data and preserves eager runtime metadata', () => {
test('getTemplate loads webui-data without componentStyles and preserves eager runtime metadata', () => {
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
const previousDocument = Object.getOwnPropertyDescriptor(globalThis, 'document');
const fn = (): boolean => true;
Expand All @@ -175,7 +175,7 @@ describe('template registry helpers', () => {
getElementById(id: string) {
if (id !== 'webui-data') return null;
return {
textContent: '{"inventory":"0c","state":{"title":"Hello"},"componentStyles":{"version":1,"strategy":"style","resources":{},"closures":{}},"templates":{"greeting":{"h":"<p></p>","b":[{"h":"<span></span>"}],"c":[[[0,["ready"]],0,[[],0]]]}}}',
textContent: '{"inventory":"0c","state":{"title":"Hello"},"templates":{"greeting":{"h":"<p></p>","b":[{"h":"<span></span>"}],"c":[[[0,["ready"]],0,[[],0]]]}}}',
remove() { removed = true; },
};
},
Expand All @@ -188,6 +188,7 @@ describe('template registry helpers', () => {
assert.equal((registered.c![0][0][0] as unknown), fn);
assert.equal(window.__webui!.inventory, '0c');
assert.deepEqual(window.__webui!.state, { title: 'Hello' });
assert.equal(window.__webui!.componentStyles, undefined);
assert.deepEqual(
window.__webui!.componentAssetStyles,
{ 'lazy-panel': ['/lazy-panel.css'] },
Expand Down
6 changes: 4 additions & 2 deletions packages/webui-framework/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,16 @@ function loadWebUIDataBlock(): void {
const parsed = JSON.parse(text) as NonNullable<Window['__webui']>;
if (templateFns) parsed.templateFns = templateFns;
if (componentAssetStyles) parsed.componentAssetStyles = componentAssetStyles;
// Publish before registering styles. A malformed `componentStyles` throws,
// Publish before registering styles. A malformed present `componentStyles` throws,
// and doing it the other way round loses the templates and state that
// parsed fine — then re-parses the whole block on the next lookup, because
// nothing recorded that the work was already done.
window.__webui = parsed;
el.remove();
webuiDataLoaded = true;
registerComponentStyles(parsed.componentStyles);
if (parsed.componentStyles !== undefined) {
registerComponentStyles(parsed.componentStyles);
}
return;
}
el.remove();
Expand Down
Loading