Skip to content

Commit ef05370

Browse files
committed
WIP
1 parent d955e27 commit ef05370

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

src/Helmet.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ function reduceChildrenAndProps(props: HelmetProps): Omit<HelmetProps, 'children
8686
for (const item of Object.values(props)) {
8787
if (Array.isArray(item)) {
8888
for (const it of item) {
89-
for (const key of Object.keys(it)) {
90-
const p = getPropName(key);
91-
if (p !== key) {
92-
it[p] = it[key as keyof HelmetChildProps] as unknown;
93-
delete it[key as keyof HelmetChildProps];
89+
if (it) {
90+
for (const key of Object.keys(it)) {
91+
const p = getPropName(key);
92+
if (p !== key) {
93+
it[p] = it[key as keyof HelmetChildProps] as unknown;
94+
delete it[key as keyof HelmetChildProps];
95+
}
9496
}
9597
}
9698
}
@@ -162,7 +164,7 @@ function reduceChildrenAndProps(props: HelmetProps): Omit<HelmetProps, 'children
162164
case TAG_NAMES.LINK:
163165
case TAG_NAMES.META:
164166
if (nestedChildren) throw Error(
165-
`<${type} /> elements are self-closing and cannot contain children`,
167+
`<${type} /> elements are self-closing and can not contain children. Refer to our API for more information.`,
166168
);
167169
pushToPropArray(res, type, childProps as LinkProps | MetaProps);
168170
break;
@@ -227,6 +229,7 @@ const Helmet: FunctionComponent<HelmetProps> = (props) => {
227229
// for now to ensure backward compatibility.
228230
useEffect(() => {
229231
context.update(id, reduceChildrenAndProps(props));
232+
context.clientApply();
230233
});
231234

232235
useEffect(() => () => context.update(id, undefined), [context, id]);

src/Provider.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const HelmetProvider: FunctionComponent<ProviderProps> = ({
3131
context,
3232
}) => {
3333
const { current: heap } = useRef<HelmetProviderHeap>({
34+
firstRender: true,
3435
helmets: [],
3536
state: undefined,
3637
});
@@ -46,7 +47,8 @@ const HelmetProvider: FunctionComponent<ProviderProps> = ({
4647
if (heap.nextAnimFrameId === undefined) {
4748
heap.nextAnimFrameId = requestAnimationFrame(() => {
4849
heap.state ??= calcAggregatedState(heap.helmets);
49-
commitTagChanges(heap.state);
50+
commitTagChanges(heap.state, heap.firstRender);
51+
heap.firstRender = false;
5052
delete heap.nextAnimFrameId;
5153
});
5254
}
@@ -55,7 +57,8 @@ const HelmetProvider: FunctionComponent<ProviderProps> = ({
5557
cancelAnimationFrame(heap.nextAnimFrameId);
5658
delete heap.nextAnimFrameId;
5759
}
58-
commitTagChanges(heap.state);
60+
commitTagChanges(heap.state, heap.firstRender);
61+
heap.firstRender = false;
5962
}
6063
}
6164
},
@@ -69,24 +72,6 @@ const HelmetProvider: FunctionComponent<ProviderProps> = ({
6972
delete heap.state;
7073
heap.helmets.push([id, props]);
7174
}
72-
if (IS_DOM_ENVIRONMENT && !heap.state) {
73-
heap.state = calcAggregatedState(heap.helmets);
74-
if (heap.state.defer) {
75-
if (heap.nextAnimFrameId === undefined) {
76-
heap.nextAnimFrameId = requestAnimationFrame(() => {
77-
heap.state ??= calcAggregatedState(heap.helmets);
78-
commitTagChanges(heap.state);
79-
delete heap.nextAnimFrameId;
80-
});
81-
}
82-
} else {
83-
if (heap.nextAnimFrameId !== undefined) {
84-
cancelAnimationFrame(heap.nextAnimFrameId);
85-
delete heap.nextAnimFrameId;
86-
}
87-
commitTagChanges(heap.state);
88-
}
89-
}
9075
},
9176
};
9277
}

src/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ function updateTitle(title: string | undefined, attributes: Attributes) {
144144
updateAttributes(TAG_NAMES.TITLE, attributes);
145145
}
146146

147-
export function commitTagChanges(newState: AggregatedState) {
147+
export function commitTagChanges(
148+
newState: AggregatedState,
149+
firstRender: boolean,
150+
) {
148151
const {
149152
base,
150153
bodyAttributes,
@@ -191,5 +194,7 @@ export function commitTagChanges(newState: AggregatedState) {
191194
}
192195
});
193196

194-
newState?.onChangeClientState?.(resultTags, addedTags, removedTags);
197+
if (firstRender || Object.keys(addedTags).length || Object.keys(removedTags).length) {
198+
newState?.onChangeClientState?.(resultTags, addedTags, removedTags);
199+
}
195200
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export type ContextValue = {
233233
};
234234

235235
export type HelmetProviderHeap = {
236+
// TODO: Temporary, to keep legacy behavior to call onChange client-side
237+
// callback on the first render.
238+
firstRender: boolean;
239+
236240
helmets: RegisteredHelmetPropsArray;
237241
nextAnimFrameId?: number;
238242
serverState?: HelmetServerState;

0 commit comments

Comments
 (0)