Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.8.x] Fallback pages #271

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/inertia-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
const page = pages[path]
export async function resolvePageComponent<T>(path: string|string[], pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
for (const p of (Array.isArray(path) ? path : [path])) {
const page = pages[p]

if (typeof page === 'undefined') {
throw new Error(`Page not found: ${path}`)
if (typeof page === 'undefined') {
continue
}

return typeof page === 'function' ? page() : page
}

return typeof page === 'function' ? page() : page
throw new Error(`Page not found: ${path}`)
}
5 changes: 5 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,9 @@ describe('inertia-helpers', () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
expect(file.default).toBe('Dummy File')
})

it('accepts array of paths', async () => {
const file = await resolvePageComponent<{ default: string }>(['missing-page', path], import.meta.glob('./__data__/*.ts', { eager: true }), path)
expect(file.default).toBe('Dummy File')
})
})