Skip to content

Commit d81e529

Browse files
authored
Prevent forwarding undefined options (#529)
1 parent 0f12cc1 commit d81e529

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
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.1",
45+
"@croct/plug": "^0.17.2",
4646
"@croct/sdk": "^0.18.0"
4747
},
4848
"devDependencies": {

src/hooks/useContent.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ describe('useContent (CSR)', () => {
309309
.calls[0][0]
310310
.loader();
311311

312-
expect(fetch).toHaveBeenCalledWith('slot-id', {
313-
preferredLocale: undefined,
314-
});
312+
expect(jest.mocked(fetch).mock.calls[0][1]).toStrictEqual({});
315313
});
316314
});

src/hooks/useContent.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ function useCsrContent<I, F>(
2525
fallback: fallbackContent,
2626
initial: initialContent,
2727
staleWhileLoading = false,
28+
preferredLocale,
2829
...fetchOptions
2930
} = options;
3031

31-
const preferredLocale = normalizePreferredLocale(fetchOptions.preferredLocale);
32+
const normalizedLocale = normalizePreferredLocale(preferredLocale);
3233
const defaultContent = useMemo(
33-
() => getSlotContent(id, preferredLocale) as SlotContent|null ?? undefined,
34-
[id, preferredLocale],
34+
() => getSlotContent(id, normalizedLocale) as SlotContent|null ?? undefined,
35+
[id, normalizedLocale],
3536
);
3637
const fallback = fallbackContent === undefined ? defaultContent : fallbackContent;
3738
const [initial, setInitial] = useState<SlotContent | I | F | undefined>(
@@ -44,13 +45,13 @@ function useCsrContent<I, F>(
4445
cacheKey: hash(
4546
`useContent:${cacheKey ?? ''}`
4647
+ `:${id}`
47-
+ `:${preferredLocale ?? ''}`
48+
+ `:${normalizedLocale ?? ''}`
4849
+ `:${JSON.stringify(fetchOptions.attributes ?? {})}`,
4950
),
5051
loader: () => croct.fetch(id, {
5152
...fetchOptions,
52-
preferredLocale: preferredLocale,
53-
fallback: fallback,
53+
...(normalizedLocale !== undefined ? {preferredLocale: normalizedLocale} : {}),
54+
...(fallback !== undefined ? {fallback: fallback} : {}),
5455
}).then(({content}) => content),
5556
initial: initial,
5657
expiration: expiration,

0 commit comments

Comments
 (0)