Skip to content

Commit 0f12cc1

Browse files
authored
Normalize preferred locale (#528)
1 parent f56b0ee commit 0f12cc1

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
4343
},
4444
"dependencies": {
45-
"@croct/plug": "^0.17.0",
45+
"@croct/plug": "^0.17.1",
4646
"@croct/sdk": "^0.18.0"
4747
},
4848
"devDependencies": {

src/hooks/useContent.ssr.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,21 @@ describe('useContent (SSR)', () => {
6464

6565
expect(result.current).toBe(initial);
6666
});
67+
68+
it('should normalize an empty preferred locale to undefined', () => {
69+
const slotId = 'slot-id';
70+
const preferredLocale = '';
71+
72+
jest.mocked(getSlotContent).mockReturnValue({
73+
foo: 'bar',
74+
});
75+
76+
renderHook(
77+
() => useContent(slotId, {
78+
preferredLocale: preferredLocale,
79+
}),
80+
);
81+
82+
expect(getSlotContent).toHaveBeenCalledWith(slotId, undefined);
83+
});
6784
});

src/hooks/useContent.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,27 @@ describe('useContent (CSR)', () => {
290290
preferredLocale: preferredLocale,
291291
});
292292
});
293+
294+
it('should normalize an empty preferred locale to undefined', () => {
295+
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
296+
content: {},
297+
});
298+
299+
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
300+
301+
renderHook(
302+
() => useContent('slot-id', {
303+
preferredLocale: '',
304+
}),
305+
);
306+
307+
jest.mocked(useLoader)
308+
.mock
309+
.calls[0][0]
310+
.loader();
311+
312+
expect(fetch).toHaveBeenCalledWith('slot-id', {
313+
preferredLocale: undefined,
314+
});
315+
});
293316
});

src/hooks/useContent.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function useCsrContent<I, F>(
2828
...fetchOptions
2929
} = options;
3030

31-
const {preferredLocale} = fetchOptions;
31+
const preferredLocale = normalizePreferredLocale(fetchOptions.preferredLocale);
3232
const defaultContent = useMemo(
3333
() => getSlotContent(id, preferredLocale) as SlotContent|null ?? undefined,
3434
[id, preferredLocale],
@@ -47,7 +47,11 @@ function useCsrContent<I, F>(
4747
+ `:${preferredLocale ?? ''}`
4848
+ `:${JSON.stringify(fetchOptions.attributes ?? {})}`,
4949
),
50-
loader: () => croct.fetch(id, {...fetchOptions, fallback: fallback}).then(({content}) => content),
50+
loader: () => croct.fetch(id, {
51+
...fetchOptions,
52+
preferredLocale: preferredLocale,
53+
fallback: fallback,
54+
}).then(({content}) => content),
5155
initial: initial,
5256
expiration: expiration,
5357
});
@@ -75,7 +79,7 @@ function useSsrContent<I, F>(
7579
{initial, preferredLocale}: UseContentOptions<I, F> = {},
7680
): SlotContent | I | F {
7781
const resolvedInitialContent = initial === undefined
78-
? getSlotContent(slotId, preferredLocale) as I|null ?? undefined
82+
? getSlotContent(slotId, normalizePreferredLocale(preferredLocale)) as I|null ?? undefined
7983
: initial;
8084

8185
if (resolvedInitialContent === undefined) {
@@ -88,6 +92,10 @@ function useSsrContent<I, F>(
8892
return resolvedInitialContent;
8993
}
9094

95+
function normalizePreferredLocale(preferredLocale: string|undefined): string|undefined {
96+
return preferredLocale !== undefined && preferredLocale !== '' ? preferredLocale : undefined;
97+
}
98+
9199
type UseContentHook = {
92100
<P extends JsonObject, I = P, F = P>(
93101
id: keyof VersionedSlotMap extends never ? string : never,

0 commit comments

Comments
 (0)