Skip to content

Commit c4e10ca

Browse files
committed
Fix fallback behaviour
1 parent 174e435 commit c4e10ca

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

package-lock.json

Lines changed: 3 additions & 3 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": "https://pkg.pr.new/@croct/plug@321",
45+
"@croct/plug": "https://pkg.pr.new/@croct/plug@322",
4646
"@croct/content": "https://pkg.pr.new/@croct/content@3"
4747
},
4848
"devDependencies": {

src/hooks/useContent.test.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ describe('useContent (CSR)', () => {
6363
expect(useCroct).toHaveBeenCalled();
6464
expect(useLoader).toHaveBeenCalledWith({
6565
cacheKey: hash(`useContent:${cacheKey}:${slotId}:${preferredLocale}:${JSON.stringify(attributes)}`),
66-
fallback: {
67-
title: 'error',
68-
},
6966
expiration: 50,
7067
loader: expect.any(Function),
7168
});
@@ -76,6 +73,7 @@ describe('useContent (CSR)', () => {
7673
.loader();
7774

7875
expect(fetch).toHaveBeenCalledWith(slotId, {
76+
fallback: {title: 'error'},
7977
preferredLocale: 'en',
8078
attributes: attributes,
8179
});
@@ -234,6 +232,12 @@ describe('useContent (CSR)', () => {
234232
const slotId = 'slot-id';
235233
const preferredLocale = 'en';
236234

235+
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
236+
content: {},
237+
});
238+
239+
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
240+
237241
jest.mocked(getSlotContent).mockReturnValue(content);
238242

239243
renderHook(
@@ -245,18 +249,28 @@ describe('useContent (CSR)', () => {
245249

246250
expect(getSlotContent).toHaveBeenCalledWith(slotId, preferredLocale);
247251

248-
expect(useLoader).toHaveBeenCalledWith(
249-
expect.objectContaining({
250-
fallback: content,
251-
}),
252-
);
252+
jest.mocked(useLoader)
253+
.mock
254+
.calls[0][0]
255+
.loader();
256+
257+
expect(fetch).toHaveBeenCalledWith(slotId, {
258+
fallback: content,
259+
preferredLocale: preferredLocale,
260+
});
253261
});
254262

255263
it('should use the provided fallback value', () => {
256264
const fallback = null;
257265
const slotId = 'slot-id';
258266
const preferredLocale = 'en';
259267

268+
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
269+
content: {},
270+
});
271+
272+
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
273+
260274
jest.mocked(getSlotContent).mockReturnValue(null);
261275

262276
renderHook(
@@ -266,10 +280,14 @@ describe('useContent (CSR)', () => {
266280
}),
267281
);
268282

269-
expect(useLoader).toHaveBeenCalledWith(
270-
expect.objectContaining({
271-
fallback: fallback,
272-
}),
273-
);
283+
jest.mocked(useLoader)
284+
.mock
285+
.calls[0][0]
286+
.loader();
287+
288+
expect(fetch).toHaveBeenCalledWith(slotId, {
289+
fallback: fallback,
290+
preferredLocale: preferredLocale,
291+
});
274292
});
275293
});

src/hooks/useContent.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {useCroct} from './useCroct';
88
import {isSsr} from '../ssr-polyfills';
99
import {hash} from '../hash';
1010

11-
export type UseContentOptions<I, F> = FetchOptions & {
11+
export type UseContentOptions<I, F> = FetchOptions<F> & {
1212
fallback?: F,
1313
initial?: I,
1414
cacheKey?: string,
@@ -21,20 +21,20 @@ function useCsrContent<I, F>(
2121
options: UseContentOptions<I, F> = {},
2222
): SlotContent | I | F {
2323
const {
24-
fallback,
2524
cacheKey,
2625
expiration,
26+
fallback: fallbackContent,
2727
initial: initialContent,
2828
staleWhileLoading = false,
2929
...fetchOptions
3030
} = options;
3131

3232
const {preferredLocale} = fetchOptions;
3333
const defaultContent = useMemo(
34-
() => getSlotContent(id, preferredLocale) as F|null ?? undefined,
34+
() => getSlotContent(id, preferredLocale) as SlotContent|null ?? undefined,
3535
[id, preferredLocale],
3636
);
37-
37+
const fallback = fallbackContent === undefined ? defaultContent : fallbackContent;
3838
const [initial, setInitial] = useState<SlotContent | I | F | undefined>(
3939
() => (initialContent === undefined ? defaultContent : initialContent),
4040
);
@@ -48,9 +48,8 @@ function useCsrContent<I, F>(
4848
+ `:${preferredLocale ?? ''}`
4949
+ `:${JSON.stringify(fetchOptions.attributes ?? {})}`,
5050
),
51-
loader: () => croct.fetch(id, fetchOptions).then(({content}) => content),
51+
loader: () => croct.fetch(id, {...fetchOptions, fallback: fallback}).then(({content}) => content),
5252
initial: initial,
53-
fallback: fallback === undefined ? defaultContent : fallback,
5453
expiration: expiration,
5554
});
5655

0 commit comments

Comments
 (0)