Skip to content

fix: serialize less vnode data #7632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 13 additions & 1 deletion packages/qwik/src/core/client/vnode-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,12 @@ function getComponentHash(vNode: VNode | null, getObject: (id: string) => any):
function Projection() {}

function propsDiffer(src: Record<string, any>, dst: Record<string, any>): boolean {
if (!src || !dst) {
const srcEmpty = isPropsEmpty(src);
const dstEmpty = isPropsEmpty(dst);
if (srcEmpty && dstEmpty) {
return false;
}
if (srcEmpty || dstEmpty) {
return true;
}
let srcKeys = removePropsKeys(Object.keys(src), ['children', QBackRefs]);
Expand All @@ -1257,6 +1262,13 @@ function propsDiffer(src: Record<string, any>, dst: Record<string, any>): boolea
return false;
}

function isPropsEmpty(props: Record<string, any>): boolean {
if (!props) {
return true;
}
return Object.keys(props).length === 0;
}

function removePropsKeys(keys: string[], propKeys: string[]): string[] {
for (let i = propKeys.length - 1; i >= 0; i--) {
const propKey = propKeys[i];
Expand Down
19 changes: 15 additions & 4 deletions packages/qwik/src/core/shared/shared-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type { DeserializeContainer, HostElement, ObjToProxyMap } from './types';
import { _CONST_PROPS, _VAR_PROPS } from './utils/constants';
import { isElement, isNode } from './utils/element';
import { EMPTY_ARRAY, EMPTY_OBJ } from './utils/flyweight';
import { ELEMENT_ID } from './utils/markers';
import { ELEMENT_ID, QBackRefs } from './utils/markers';
import { isPromise } from './utils/promises';
import { SerializerSymbol, fastSkipSerialize } from './utils/serialize-utils';
import {
Expand Down Expand Up @@ -843,12 +843,16 @@ function $discoverRoots$(
const isSsrAttrs = (value: number | SsrAttrs): value is SsrAttrs =>
Array.isArray(value) && value.length > 0;

const discoverValuesForVNodeData = (vnodeData: VNodeData, callback: (value: unknown) => void) => {
const discoverValuesForVNodeData = (
vnodeData: VNodeData,
callback: (value: unknown) => void,
filter: (key: string, value: unknown) => boolean = (_, attrValue) => typeof attrValue === 'string'
) => {
for (const value of vnodeData) {
if (isSsrAttrs(value)) {
for (let i = 1; i < value.length; i += 2) {
const attrValue = value[i];
if (typeof attrValue === 'string') {
if (filter(value[i - 1] as string, attrValue)) {
continue;
}
callback(attrValue);
Expand Down Expand Up @@ -1198,7 +1202,14 @@ async function serialize(serializationContext: SerializationContext): Promise<vo
}
if (value.childrenVNodeData) {
for (const vNodeData of value.childrenVNodeData) {
discoverValuesForVNodeData(vNodeData, (vNodeDataValue) => $addRoot$(vNodeDataValue));
discoverValuesForVNodeData(
vNodeData,
(vNodeDataValue) => {
$addRoot$(vNodeDataValue);
},
(key, value) => typeof value === 'string' && key !== QBackRefs
);

vNodeData[0] |= VNodeDataFlag.SERIALIZE;
}
}
Expand Down
70 changes: 70 additions & 0 deletions packages/qwik/src/core/tests/component.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,76 @@ describe.each([
);
});

it('should not rerender component with empty props', async () => {
(globalThis as any).componentExecuted = [];
const Component1 = component$<PropsOf<any>>(() => {
(globalThis as any).componentExecuted.push('Component1');
return <div></div>;
});
const Parent = component$(() => {
(globalThis as any).componentExecuted.push('Parent');
const show = useSignal(true);
return (
<main class="parent" onClick$={() => (show.value = !show.value)}>
{show.value && <Component1 />}
<Component1 />
</main>
);
});
const { vNode, container } = await render(<Parent />, { debug });
expect((globalThis as any).componentExecuted).toEqual(['Parent', 'Component1', 'Component1']);
expect(vNode).toMatchVDOM(
<Component ssr-required>
<main class="parent">
<Component ssr-required>
<div></div>
</Component>
<Component ssr-required>
<div></div>
</Component>
</main>
</Component>
);
await trigger(container.element, 'main.parent', 'click');
expect((globalThis as any).componentExecuted).toEqual([
'Parent',
'Component1',
'Component1',
'Parent',
]);
expect(vNode).toMatchVDOM(
<Component ssr-required>
<main class="parent">
{''}
<Component ssr-required>
<div></div>
</Component>
</main>
</Component>
);
await trigger(container.element, 'main.parent', 'click');
expect((globalThis as any).componentExecuted).toEqual([
'Parent',
'Component1',
'Component1',
'Parent',
'Parent',
'Component1',
]);
expect(vNode).toMatchVDOM(
<Component ssr-required>
<main class="parent">
<Component ssr-required>
<div></div>
</Component>
<Component ssr-required>
<div></div>
</Component>
</main>
</Component>
);
});

it('should remove children from component$', async () => {
const log: string[] = [];
const MyComp = component$((props: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/tests/render-namespace.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe.each([
<Component>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="15" cy="15" r="50"></circle>
<Signal>
<Signal ssr-required>
<line x1="0" y1="80" x2="100" y2="20" stroke="black" key="1"></line>
</Signal>
</svg>
Expand Down