-
Notifications
You must be signed in to change notification settings - Fork 1
feat: SSR redirects for index pages and folder navigation #69
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3dd0b3f
feat: SSR redirects for index pages and folder navigation
rsbh 3c64d64
refactor: use NodeType constants instead of string literals
rsbh 3f1d29b
fix: only redirect to index_page at content root, not all missing slugs
rsbh 0a1d6e1
fix: handle versioned site redirects in SSR
rsbh 64ea997
refactor: use StatusCodes for 404 and 200 in entry-server
rsbh b1286e3
refactor: use StatusCodes.NOT_FOUND in DocsPage
rsbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import type { Node } from 'fumadocs-core/page-tree' | ||
| import { getFirstPageUrl, findFolderFirstPage, resolveDocsRedirect } from './tree-utils' | ||
|
|
||
| function page(url: string, name = 'Page'): Node { | ||
| return { type: 'page', name, url } as Node | ||
| } | ||
|
|
||
| function folder(name: string, children: Node[], indexUrl?: string): Node { | ||
| return { | ||
| type: 'folder', | ||
| name, | ||
| children, | ||
| ...(indexUrl ? { index: { url: indexUrl } } : {}), | ||
| } as Node | ||
| } | ||
|
|
||
| describe('getFirstPageUrl', () => { | ||
| test('returns first page url', () => { | ||
| expect(getFirstPageUrl([page('/docs/intro')])).toBe('/docs/intro') | ||
| }) | ||
|
|
||
| test('returns first page from nested folder', () => { | ||
| const nodes = [folder('Guides', [page('/docs/guides/install')])] | ||
| expect(getFirstPageUrl(nodes)).toBe('/docs/guides/install') | ||
| }) | ||
|
|
||
| test('skips empty folders', () => { | ||
| const nodes = [folder('Empty', []), page('/docs/hello')] | ||
| expect(getFirstPageUrl(nodes)).toBe('/docs/hello') | ||
| }) | ||
|
|
||
| test('returns null for empty list', () => { | ||
| expect(getFirstPageUrl([])).toBeNull() | ||
| }) | ||
|
|
||
| test('returns null for folders with no pages', () => { | ||
| expect(getFirstPageUrl([folder('Empty', [])])).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('findFolderFirstPage', () => { | ||
| test('finds folder by index url', () => { | ||
| const nodes = [ | ||
| folder('Guides', [page('/docs/guides/install'), page('/docs/guides/config')], '/docs/guides'), | ||
| ] | ||
| expect(findFolderFirstPage(nodes, '/docs/guides')).toBe('/docs/guides/install') | ||
| }) | ||
|
|
||
| test('finds folder without index by child page path', () => { | ||
| const nodes = [ | ||
| folder('Guides', [page('/docs/guides/install'), page('/docs/guides/config')]), | ||
| ] | ||
| expect(findFolderFirstPage(nodes, '/docs/guides')).toBe('/docs/guides/install') | ||
| }) | ||
|
|
||
| test('finds nested folder', () => { | ||
| const nodes = [ | ||
| folder('Docs', [ | ||
| folder('Advanced', [page('/docs/advanced/perf'), page('/docs/advanced/debug')]), | ||
| ]), | ||
| ] | ||
| expect(findFolderFirstPage(nodes, '/docs/advanced')).toBe('/docs/advanced/perf') | ||
| }) | ||
|
|
||
| test('returns null for non-matching path', () => { | ||
| const nodes = [folder('Guides', [page('/docs/guides/install')])] | ||
| expect(findFolderFirstPage(nodes, '/docs/api')).toBeNull() | ||
| }) | ||
|
|
||
| test('returns null for empty folder', () => { | ||
| const nodes = [folder('Empty', [])] | ||
| expect(findFolderFirstPage(nodes, '/docs/empty')).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('resolveDocsRedirect', () => { | ||
| const tree = { | ||
| children: [ | ||
| page('/docs/intro'), | ||
| folder('Guides', [page('/docs/guides/install')]), | ||
| ] as Node[], | ||
| } | ||
|
|
||
| test('redirects to index_page when set', () => { | ||
| expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs', index_page: 'getting-started' })) | ||
| .toBe('/docs/getting-started') | ||
| }) | ||
|
|
||
| test('redirects content root to first page', () => { | ||
| expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs' })) | ||
| .toBe('/docs/intro') | ||
| }) | ||
|
|
||
| test('redirects folder to first child', () => { | ||
| expect(resolveDocsRedirect(['docs', 'guides'], tree, { dir: 'docs' })) | ||
| .toBe('/docs/guides/install') | ||
| }) | ||
|
|
||
| test('returns null for non-matching path', () => { | ||
| expect(resolveDocsRedirect(['docs', 'nonexistent'], tree, { dir: 'docs' })) | ||
| .toBeNull() | ||
| }) | ||
|
|
||
| test('returns null without content config', () => { | ||
| expect(resolveDocsRedirect(['other'], tree)).toBeNull() | ||
| }) | ||
|
|
||
| test('index_page takes priority over first page', () => { | ||
| expect(resolveDocsRedirect(['docs'], tree, { dir: 'docs', index_page: 'custom' })) | ||
| .toBe('/docs/custom') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { Node } from 'fumadocs-core/page-tree'; | ||
|
|
||
| export const NodeType = { | ||
| Page: 'page', | ||
| Folder: 'folder', | ||
| Separator: 'separator', | ||
| } as const; | ||
|
|
||
| export function getFirstPageUrl(nodes: Node[]): string | null { | ||
| for (const node of nodes) { | ||
| if (node.type === NodeType.Page) return node.url; | ||
| if (node.type === NodeType.Folder) { | ||
| const url = getFirstPageUrl(node.children); | ||
| if (url) return url; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function getFolderPath(node: Node): string | null { | ||
| if (node.type !== NodeType.Folder) return null; | ||
| if (node.index) return node.index.url; | ||
| const firstPage = getFirstPageUrl(node.children); | ||
| if (!firstPage) return null; | ||
| const parts = firstPage.split('/').filter(Boolean); | ||
| parts.pop(); | ||
| return '/' + parts.join('/'); | ||
| } | ||
|
|
||
| export function findFolderFirstPage(nodes: Node[], pathname: string): string | null { | ||
| for (const node of nodes) { | ||
| if (node.type === NodeType.Folder) { | ||
| const folderPath = getFolderPath(node); | ||
| if (folderPath === pathname) return getFirstPageUrl(node.children); | ||
| const found = findFolderFirstPage(node.children, pathname); | ||
| if (found) return found; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function resolveDocsRedirect( | ||
| slug: string[], | ||
| tree: { children: Node[] }, | ||
| contentConfig?: { dir: string; index_page?: string }, | ||
| ): string | null { | ||
| const isContentRoot = slug.length === 1 && slug[0] === contentConfig?.dir; | ||
|
|
||
| if (isContentRoot) { | ||
| if (contentConfig?.index_page) { | ||
| return `/${contentConfig.dir}/${contentConfig.index_page}`; | ||
| } | ||
| return getFirstPageUrl(tree.children); | ||
| } | ||
|
|
||
| return findFolderFirstPage(tree.children, `/${slug.join('/')}`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.