Skip to content

Commit 5000d42

Browse files
authored
[0.8.x] Fallback pages (#271)
* Allow path to be a list * Add test
1 parent 0344788 commit 5000d42

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/inertia-helpers/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
2-
const page = pages[path]
1+
export async function resolvePageComponent<T>(path: string|string[], pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
2+
for (const p of (Array.isArray(path) ? path : [path])) {
3+
const page = pages[p]
34

4-
if (typeof page === 'undefined') {
5-
throw new Error(`Page not found: ${path}`)
5+
if (typeof page === 'undefined') {
6+
continue
7+
}
8+
9+
return typeof page === 'function' ? page() : page
610
}
711

8-
return typeof page === 'function' ? page() : page
12+
throw new Error(`Page not found: ${path}`)
913
}

tests/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,9 @@ describe('inertia-helpers', () => {
376376
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
377377
expect(file.default).toBe('Dummy File')
378378
})
379+
380+
it('accepts array of paths', async () => {
381+
const file = await resolvePageComponent<{ default: string }>(['missing-page', path], import.meta.glob('./__data__/*.ts', { eager: true }), path)
382+
expect(file.default).toBe('Dummy File')
383+
})
379384
})

0 commit comments

Comments
 (0)