Skip to content

Commit c3189a5

Browse files
fix(content): load content resources inside the resource loader (#2776)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2d23234 commit c3189a5

6 files changed

Lines changed: 113 additions & 17 deletions

File tree

‎packages/content/resources/src/content-file-resource.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,42 @@ Body content`),
6060
});
6161
});
6262

63+
it('waits for the content file loader instead of failing first', async () => {
64+
let resolveFiles!: (files: Record<string, () => Promise<string>>) => void;
65+
const files = new Promise<Record<string, () => Promise<string>>>(
66+
(resolve) => (resolveFiles = resolve),
67+
);
68+
TestBed.configureTestingModule({
69+
providers: [
70+
{
71+
provide: ActivatedRoute,
72+
useValue: { paramMap: of(convertToParamMap({ slug: 'test' })) },
73+
},
74+
{ provide: ContentRenderer, useClass: TestContentRenderer },
75+
{ provide: CONTENT_FILE_LOADER, useValue: () => files },
76+
{
77+
provide: TEST_RESOURCE_TOKEN,
78+
useFactory: () => contentFileResource(),
79+
},
80+
],
81+
});
82+
83+
const result = TestBed.inject(TEST_RESOURCE_TOKEN);
84+
result.value();
85+
TestBed.tick();
86+
await new Promise((resolve) => setTimeout(resolve));
87+
88+
expect(result.error()).toBeUndefined();
89+
expect(result.status()).toBe('loading');
90+
91+
resolveFiles({
92+
'/src/content/test.md': () => Promise.resolve('# Test'),
93+
});
94+
await settleResource(result);
95+
96+
expect(result.value()?.content).toBe('# Test');
97+
});
98+
6399
it('returns empty toc for module/object content', async () => {
64100
const contentFiles = {
65101
'/src/content/object-content.md': () =>

‎packages/content/resources/src/content-file-resource.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { ActivatedRoute } from '@angular/router';
1010

1111
import { toSignal } from '@angular/core/rxjs-interop';
12-
import { from } from 'rxjs';
1312
import { map } from 'rxjs/operators';
1413
import type { ContentFile } from '../../src/lib/content-file';
1514
import { injectContentLocale } from '../../src/lib/content-locale';
@@ -212,7 +211,6 @@ export function contentFileResource(
212211
const loaderPromise = injectContentFileLoader();
213212
const contentRenderer = inject(ContentRenderer);
214213
const locale = injectContentLocale();
215-
const contentFilesMap = toSignal(from(loaderPromise()));
216214
const input =
217215
params ||
218216
toSignal(
@@ -223,14 +221,14 @@ export function contentFileResource(
223221
);
224222

225223
return resource({
226-
params: computed(() => ({ input: input(), files: contentFilesMap() })),
227-
loader: async ({ params: resourceParams }) => {
228-
const { input: param, files } = resourceParams;
224+
params: computed(() => ({ input: input() })),
225+
loader: async ({ params: { input: param } }) => {
226+
const files = await loaderPromise();
229227

230228
if (typeof param === 'string') {
231229
if (param) {
232230
const file = await getContentFile(
233-
files!,
231+
files,
234232
param,
235233
fallback,
236234
locale,
@@ -260,7 +258,7 @@ export function contentFileResource(
260258
};
261259
} else {
262260
const file = await getContentFile(
263-
files!,
261+
files,
264262
param.customFilename,
265263
fallback,
266264
locale,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { ApplicationRef } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { describe, expect, it, vi } from 'vitest';
4+
5+
import { contentFilesResource } from './content-files-resource';
6+
import { CONTENT_FILES_LIST_TOKEN } from '../../src/lib/content-files-list-token';
7+
import {
8+
CONTENT_LIST_LOADER,
9+
withContentListLoader,
10+
} from '../../src/lib/content-list-loader';
11+
import { RenderTaskService } from '../../src/lib/render-task.service';
12+
13+
describe('contentFilesResource', () => {
14+
it('runs the list loader again on reload', async () => {
15+
const files = [
16+
{ filename: '/src/content/a.md', slug: 'a', attributes: {} },
17+
{ filename: '/src/content/b.md', slug: 'b', attributes: {} },
18+
];
19+
const loader = vi
20+
.fn()
21+
.mockRejectedValueOnce(new Error('offline'))
22+
.mockResolvedValue(files);
23+
TestBed.configureTestingModule({
24+
providers: [{ provide: CONTENT_LIST_LOADER, useValue: loader }],
25+
});
26+
27+
const result = TestBed.runInInjectionContext(() =>
28+
contentFilesResource((file) => file.slug === 'b'),
29+
);
30+
await settle();
31+
32+
expect(result.error()?.message).toBe('offline');
33+
34+
result.reload();
35+
await settle();
36+
37+
expect(loader).toHaveBeenCalledTimes(2);
38+
expect(result.value()).toEqual([files[1]]);
39+
});
40+
41+
it('loads through withContentListLoader outside an injection context', async () => {
42+
const files = [
43+
{ filename: '/src/content/a.md', slug: 'a', attributes: {} },
44+
];
45+
TestBed.configureTestingModule({
46+
providers: [
47+
withContentListLoader(),
48+
RenderTaskService,
49+
{ provide: CONTENT_FILES_LIST_TOKEN, useValue: files },
50+
],
51+
});
52+
53+
const result = TestBed.runInInjectionContext(() => contentFilesResource());
54+
await settle();
55+
56+
expect(result.value()).toEqual(files);
57+
});
58+
});
59+
60+
async function settle() {
61+
TestBed.tick();
62+
await TestBed.inject(ApplicationRef).whenStable();
63+
}

‎packages/content/resources/src/content-files-resource.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ export function contentFilesResource<Attributes extends Record<string, any>>(
1212
): ResourceRef<ContentFile<Attributes>[] | undefined> {
1313
const contentListLoader = injectContentListLoader<Attributes>();
1414
const locale = injectContentLocale();
15-
const contentList = contentListLoader().then((items) => {
16-
let results = locale ? filterByLocale(items, locale) : items;
17-
if (filterFn) {
18-
results = results.filter(filterFn);
19-
}
20-
return results;
21-
});
2215

2316
return resource({
24-
loader: () => contentList,
17+
loader: async () => {
18+
const items = await contentListLoader();
19+
const results = locale ? filterByLocale(items, locale) : items;
20+
return filterFn ? results.filter(filterFn) : results;
21+
},
2522
});
2623
}

‎packages/content/src/lib/content-file-loader.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export function withContentFileLoader(): Provider {
2121
return {
2222
provide: CONTENT_FILE_LOADER,
2323
useFactory() {
24-
return async () => injectContentFilesMap();
24+
const files = injectContentFilesMap();
25+
return async () => files;
2526
},
2627
};
2728
}

‎packages/content/src/lib/content-list-loader.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function withContentListLoader(): Provider {
2323
return {
2424
provide: CONTENT_LIST_LOADER,
2525
useFactory() {
26-
return async () => injectContentFiles();
26+
const files = injectContentFiles();
27+
return async () => files;
2728
},
2829
};
2930
}

0 commit comments

Comments
 (0)