Skip to content

Commit 8af7775

Browse files
committed
fix: defer reading QScopedStyle until promise resolves for SSR
1 parent de5e864 commit 8af7775

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

.changeset/all-poets-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik.dev/core': patch
3+
---
4+
5+
fix: defer setting scoped style until jsx is resolved

packages/qwik/src/core/ssr/ssr-render-jsx.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,22 @@ function processJSXNode(
279279
);
280280

281281
const jsxOutput = applyQwikComponentBody(ssr, jsx, type);
282-
const compStyleComponentId = addComponentStylePrefix(host.getProp(QScopedStyle));
283282
enqueue(new ParentComponentData(options.styleScoped, options.parentComponentFrame));
284283
enqueue(ssr.closeComponent);
285-
enqueue(jsxOutput);
286-
isPromise(jsxOutput) && enqueue(Promise);
287-
enqueue(new ParentComponentData(compStyleComponentId, componentFrame));
284+
if (isPromise(jsxOutput)) {
285+
// Defer reading QScopedStyle until after the promise resolves
286+
enqueue(async () => {
287+
const resolvedOutput = await jsxOutput;
288+
const compStyleComponentId = addComponentStylePrefix(host.getProp(QScopedStyle));
289+
290+
enqueue(resolvedOutput);
291+
enqueue(new ParentComponentData(compStyleComponentId, componentFrame));
292+
});
293+
} else {
294+
enqueue(jsxOutput);
295+
const compStyleComponentId = addComponentStylePrefix(host.getProp(QScopedStyle));
296+
enqueue(new ParentComponentData(compStyleComponentId, componentFrame));
297+
}
288298
} else {
289299
const inlineComponentProps = [ELEMENT_KEY, jsx.key];
290300
ssr.openFragment(

packages/qwik/src/core/tests/use-styles-scoped.spec.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import {
66
Fragment as Projection,
77
Fragment as Signal,
88
Slot,
9+
useAsyncComputed$,
910
useSignal,
11+
useStylesScoped$,
1012
useStylesScopedQrl,
1113
} from '@qwik.dev/core';
1214
import { renderToString } from '@qwik.dev/core/server';
1315
import { createDocument, domRender, ssrRenderToDom, trigger } from '@qwik.dev/core/testing';
1416
import { cleanupAttrs } from 'packages/qwik/src/testing/element-fixture';
15-
import { afterEach, describe, expect, it, vi } from 'vitest';
17+
import { afterEach, describe, expect, it } from 'vitest';
1618
import { useStore } from '..';
1719
import { getPlatform, setPlatform } from '../shared/platform/platform';
1820
import { QStyleSelector } from '../shared/utils/markers';
@@ -21,11 +23,6 @@ import { getScopedStyles } from '../shared/utils/scoped-stylesheet';
2123
const debug = false; //true;
2224
Error.stackTraceLimit = 100;
2325

24-
vi.hoisted(() => {
25-
vi.stubGlobal('QWIK_LOADER_DEFAULT_MINIFIED', 'min');
26-
vi.stubGlobal('QWIK_LOADER_DEFAULT_DEBUG', 'debug');
27-
});
28-
2926
describe.each([
3027
{ render: ssrRenderToDom }, //
3128
{ render: domRender }, //
@@ -602,6 +599,26 @@ describe.each([
602599
);
603600
});
604601

602+
it('should await for async component jsx output before setting style scoped id', async () => {
603+
(globalThis as any).rawStyleId = '';
604+
const Cmp = component$(() => {
605+
const sig = useAsyncComputed$(async () => {
606+
return 'computed';
607+
});
608+
sig.value;
609+
(globalThis as any).rawStyleId = useStylesScoped$(`.red {color: red;}`).scopeId;
610+
611+
return <div class="red">this should be red</div>;
612+
});
613+
614+
const { vNode } = await render(<Cmp />, { debug });
615+
expect(vNode).toMatchVDOM(
616+
<Component>
617+
<div class={(globalThis as any).rawStyleId + ' red'}>this should be red</div>
618+
</Component>
619+
);
620+
});
621+
605622
describe('regression', () => {
606623
it('#1945 - should add styles to conditionally rendered slots', async () => {
607624
(globalThis as any).rawStyleId1 = '';

packages/qwik/src/core/tests/use-styles.spec.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ import {
1010
} from '@qwik.dev/core';
1111
import { renderToString } from '@qwik.dev/core/server';
1212
import { createDocument, domRender, ssrRenderToDom, trigger } from '@qwik.dev/core/testing';
13-
import { afterEach, describe, expect, it, vi } from 'vitest';
13+
import { afterEach, describe, expect, it } from 'vitest';
1414
import { getPlatform, setPlatform } from '../shared/platform/platform';
1515
import { QStyleSelector } from '../shared/utils/markers';
1616

1717
const debug = false; //true;
1818
Error.stackTraceLimit = 100;
1919

20-
vi.hoisted(() => {
21-
vi.stubGlobal('QWIK_LOADER_DEFAULT_MINIFIED', 'min');
22-
vi.stubGlobal('QWIK_LOADER_DEFAULT_DEBUG', 'debug');
23-
});
24-
2520
describe.each([
2621
{ render: ssrRenderToDom }, //
2722
{ render: domRender }, //

0 commit comments

Comments
 (0)