Skip to content

Commit c4d3bd7

Browse files
committed
chore(router): head is sync so can be entirely wrapped in withLocale()
1 parent 4107646 commit c4d3bd7

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

packages/qwik-router/src/runtime/src/head.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,53 @@ export const resolveHead = (
2121
contentModules: ContentModule[],
2222
locale: string,
2323
defaults?: DocumentHeadValue
24-
) => {
25-
const head = createDocumentHead(defaults);
26-
const getData = ((loaderOrAction: LoaderInternal | ActionInternal) => {
27-
const id = loaderOrAction.__id;
28-
if (loaderOrAction.__brand === 'server_loader') {
29-
if (!(id in endpoint.loaders)) {
30-
throw new Error(
31-
'You can not get the returned data of a loader that has not been executed for this request.'
32-
);
24+
) =>
25+
withLocale(locale, () => {
26+
const head = createDocumentHead(defaults);
27+
const getData = ((loaderOrAction: LoaderInternal | ActionInternal) => {
28+
const id = loaderOrAction.__id;
29+
if (loaderOrAction.__brand === 'server_loader') {
30+
if (!(id in endpoint.loaders)) {
31+
throw new Error(
32+
'You can not get the returned data of a loader that has not been executed for this request.'
33+
);
34+
}
3335
}
34-
}
35-
const data = endpoint.loaders[id];
36-
if (isPromise(data)) {
37-
throw new Error('Loaders returning a promise can not be resolved for the head function.');
38-
}
39-
return data;
40-
}) as any as ResolveSyncValue;
36+
const data = endpoint.loaders[id];
37+
if (isPromise(data)) {
38+
throw new Error('Loaders returning a promise can not be resolved for the head function.');
39+
}
40+
return data;
41+
}) as any as ResolveSyncValue;
4142

42-
const fns: Extract<ContentModuleHead, Function>[] = [];
43-
for (const contentModule of contentModules) {
44-
const contentModuleHead = contentModule?.head;
45-
if (contentModuleHead) {
46-
if (typeof contentModuleHead === 'function') {
47-
// Functions are executed inner before outer
48-
fns.unshift(contentModuleHead);
49-
} else if (typeof contentModuleHead === 'object') {
50-
// Objects are merged inner over outer
51-
resolveDocumentHead(head, contentModuleHead);
43+
const fns: Extract<ContentModuleHead, Function>[] = [];
44+
for (const contentModule of contentModules) {
45+
const contentModuleHead = contentModule?.head;
46+
if (contentModuleHead) {
47+
if (typeof contentModuleHead === 'function') {
48+
// Functions are executed inner before outer
49+
fns.unshift(contentModuleHead);
50+
} else if (typeof contentModuleHead === 'object') {
51+
// Objects are merged inner over outer
52+
resolveDocumentHead(head, contentModuleHead);
53+
}
5254
}
5355
}
54-
}
55-
if (fns.length) {
56-
const headProps: DocumentHeadProps = {
57-
head,
58-
withLocale: (fn) => withLocale(locale, fn),
59-
resolveValue: getData,
60-
...routeLocation,
61-
};
56+
if (fns.length) {
57+
const headProps: DocumentHeadProps = {
58+
head,
59+
withLocale: (fn) => fn(),
60+
resolveValue: getData,
61+
...routeLocation,
62+
};
6263

63-
withLocale(locale, () => {
6464
for (const fn of fns) {
6565
resolveDocumentHead(head, fn(headProps));
6666
}
67-
});
68-
}
67+
}
6968

70-
return head;
71-
};
69+
return head;
70+
});
7271

7372
const resolveDocumentHead = (
7473
resolvedHead: Editable<ResolvedDocumentHead>,

packages/qwik-router/src/runtime/src/qwik-router.runtime.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface DocumentHeadProps extends RouteLocation {
123123
readonly head: ResolvedDocumentHead;
124124
// (undocumented)
125125
readonly resolveValue: ResolveSyncValue;
126-
// (undocumented)
126+
// @deprecated (undocumented)
127127
readonly withLocale: <T>(fn: () => T) => T;
128128
}
129129

packages/qwik-router/src/runtime/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export type DocumentScript = (
221221
/** @public */
222222
export interface DocumentHeadProps extends RouteLocation {
223223
readonly head: ResolvedDocumentHead;
224+
/** @deprecated This is not necessary, it works correctly without */
224225
readonly withLocale: <T>(fn: () => T) => T;
225226
readonly resolveValue: ResolveSyncValue;
226227
}

0 commit comments

Comments
 (0)