Repository navigation
Sync document head metadata on SPA navigations #807
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
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
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,133 @@ | ||
| import { | ||
| absolutizeDocumentHead, | ||
| DOCUMENT_HEAD_ATTR, | ||
| resolveDocumentHead, | ||
| type ResolvedDocumentHead, | ||
| } from '#app/document-head.ts' | ||
| import { type AppLoaderData } from '#app/loader-data.ts' | ||
|
|
||
| export { | ||
| DEFAULT_DOCUMENT_TITLE, | ||
| NOT_FOUND_DOCUMENT_TITLE, | ||
| resolveDocumentHead, | ||
| resolveDocumentTitle, | ||
| } from '#app/document-head.ts' | ||
|
|
||
| function removeManagedHeadNodes(head: HTMLHeadElement) { | ||
| for (const node of head.querySelectorAll(`[${DOCUMENT_HEAD_ATTR}]`)) { | ||
| node.remove() | ||
| } | ||
| } | ||
|
|
||
| function appendMeta( | ||
| head: HTMLHeadElement, | ||
| key: string, | ||
| attrs: Record<string, string>, | ||
| ) { | ||
| const element = document.createElement('meta') | ||
| element.setAttribute(DOCUMENT_HEAD_ATTR, key) | ||
| for (const [name, value] of Object.entries(attrs)) { | ||
| element.setAttribute(name, value) | ||
| } | ||
| head.append(element) | ||
| } | ||
|
|
||
| function appendLink( | ||
| head: HTMLHeadElement, | ||
| key: string, | ||
| attrs: Record<string, string | undefined>, | ||
| ) { | ||
| const element = document.createElement('link') | ||
| element.setAttribute(DOCUMENT_HEAD_ATTR, key) | ||
| for (const [name, value] of Object.entries(attrs)) { | ||
| if (value === undefined) continue | ||
| element.setAttribute(name, value) | ||
| } | ||
| head.append(element) | ||
| } | ||
|
|
||
| function applyResolvedDocumentHead(resolved: ResolvedDocumentHead) { | ||
| const head = document.head | ||
| document.title = resolved.title | ||
|
|
||
| // Drop previous managed tags first so leaving a public page also clears | ||
| // stale OG/canonical/RSS nodes that no longer apply. | ||
| removeManagedHeadNodes(head) | ||
|
|
||
| if (resolved.og && resolved.canonicalUrl) { | ||
| appendMeta(head, 'og:title', { | ||
| property: 'og:title', | ||
| content: resolved.og.title, | ||
| }) | ||
| appendMeta(head, 'og:description', { | ||
| property: 'og:description', | ||
| content: resolved.og.description, | ||
| }) | ||
| appendMeta(head, 'og:image', { | ||
| property: 'og:image', | ||
| content: resolved.og.imageUrl, | ||
| }) | ||
| appendMeta(head, 'og:type', { | ||
| property: 'og:type', | ||
| content: 'website', | ||
| }) | ||
| appendMeta(head, 'og:url', { | ||
| property: 'og:url', | ||
| content: resolved.canonicalUrl, | ||
| }) | ||
| appendMeta(head, 'twitter:card', { | ||
| name: 'twitter:card', | ||
| content: 'summary_large_image', | ||
| }) | ||
| appendMeta(head, 'twitter:title', { | ||
| name: 'twitter:title', | ||
| content: resolved.og.title, | ||
| }) | ||
| appendMeta(head, 'twitter:description', { | ||
| name: 'twitter:description', | ||
| content: resolved.og.description, | ||
| }) | ||
| appendMeta(head, 'twitter:image', { | ||
| name: 'twitter:image', | ||
| content: resolved.og.imageUrl, | ||
| }) | ||
| appendLink(head, 'canonical', { | ||
| rel: 'canonical', | ||
| href: resolved.canonicalUrl, | ||
| }) | ||
| } | ||
|
|
||
| for (const [index, link] of (resolved.links ?? []).entries()) { | ||
| appendLink(head, `link:${index}`, { | ||
| rel: link.rel, | ||
| href: link.href, | ||
| type: link.type, | ||
| title: link.title, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sync managed document head tags from the current route + optional loader | ||
| * data. Title, OG/Twitter, canonical, and registry links are updated/cleared | ||
| * together so SPA navigations do not leave the previous page's metadata behind. | ||
| */ | ||
| export function applyDocumentHead( | ||
| pathname: string, | ||
| loaderData?: Partial<AppLoaderData>, | ||
| ): void { | ||
| if (typeof document === 'undefined') return | ||
| const resolved = absolutizeDocumentHead( | ||
| resolveDocumentHead(pathname, loaderData), | ||
| window.location.origin, | ||
| ) | ||
| applyResolvedDocumentHead(resolved) | ||
| } | ||
|
|
||
| /** @deprecated Prefer `applyDocumentHead` — titles are part of document head. */ | ||
| export function applyDocumentTitle( | ||
| pathname: string, | ||
| loaderData?: Partial<AppLoaderData>, | ||
| ): void { | ||
| applyDocumentHead(pathname, loaderData) | ||
| } | ||
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 |
|---|---|---|
| @@ -1,17 +1,11 @@ | ||
| import { resolveDocumentTitle } from '#app/document-title.ts' | ||
| import { type AppLoaderData } from '#app/loader-data.ts' | ||
|
|
||
| /** | ||
| * Back-compat title helpers. Prefer `#client/document-head.ts` for new work. | ||
| */ | ||
| export { | ||
| DEFAULT_DOCUMENT_TITLE, | ||
| NOT_FOUND_DOCUMENT_TITLE, | ||
| applyDocumentHead, | ||
| applyDocumentTitle, | ||
| resolveDocumentHead, | ||
| resolveDocumentTitle, | ||
| } from '#app/document-title.ts' | ||
|
|
||
| /** Sync `document.title` from the current route + optional loader data. */ | ||
| export function applyDocumentTitle( | ||
| pathname: string, | ||
| loaderData?: Partial<AppLoaderData>, | ||
| ): void { | ||
| if (typeof document === 'undefined') return | ||
| document.title = resolveDocumentTitle(pathname, loaderData) | ||
| } | ||
| } from './document-head.ts' |
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
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,130 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { | ||
| absolutizeDocumentHead, | ||
| DEFAULT_DOCUMENT_TITLE, | ||
| NOT_FOUND_DOCUMENT_TITLE, | ||
| resolveDocumentHead, | ||
| resolveDocumentTitle, | ||
| } from './document-head.ts' | ||
|
|
||
| test('resolveDocumentTitle returns static titles for known routes', () => { | ||
| expect(resolveDocumentTitle('/')).toBe(DEFAULT_DOCUMENT_TITLE) | ||
| expect(resolveDocumentTitle('/account')).toBe('Account') | ||
| expect(resolveDocumentTitle('/blog')).toBe('Blog') | ||
| expect(resolveDocumentTitle('/community')).toBe('Community packages') | ||
| expect(resolveDocumentTitle('/timeline')).toBe('Timeline') | ||
| expect(resolveDocumentTitle('/admin/users')).toBe('Admin users') | ||
| expect(resolveDocumentTitle('/privacy')).toBe('Privacy') | ||
| }) | ||
|
|
||
| test('resolveDocumentHead attaches OG metadata for public registry pages', () => { | ||
| const home = resolveDocumentHead('/') | ||
| expect(home.og?.title).toBe("Kody — your assistant's home") | ||
| expect(home.og?.imagePath).toBe('/og/home.png') | ||
| expect(home.canonicalPath).toBe('/') | ||
|
|
||
| const login = resolveDocumentHead('/login') | ||
| expect(login.og?.title).toBe('Sign in — Kody') | ||
| expect(login.title).toBe(DEFAULT_DOCUMENT_TITLE) | ||
|
|
||
| const blog = resolveDocumentHead('/blog') | ||
| expect(blog.links).toEqual([ | ||
| { | ||
| rel: 'alternate', | ||
| type: 'application/rss+xml', | ||
| title: 'Kody Blog RSS', | ||
| hrefPath: '/blog/rss.xml', | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('resolveDocumentHead derives dynamic OG from loader data', () => { | ||
| const blogPost = resolveDocumentHead('/blog/hello-world', { | ||
| blogPost: { | ||
| ok: true, | ||
| slug: 'hello-world', | ||
| title: 'Hello World', | ||
| date: '2026-01-01', | ||
| description: 'A post', | ||
| body: '# Hello', | ||
| }, | ||
| }) | ||
| expect(blogPost.title).toBe('Hello World — Kody Blog') | ||
| expect(blogPost.og).toEqual({ | ||
| title: 'Hello World — Kody Blog', | ||
| description: 'A post', | ||
| imagePath: '/blog/hello-world/og.png', | ||
| }) | ||
|
|
||
| const community = resolveDocumentHead('/community/listing-1', { | ||
| communityDetailShell: { | ||
| ok: true, | ||
| listingId: 'listing-1', | ||
| name: 'Calendar helper', | ||
| description: 'Helps with calendars.', | ||
| forkPrompt: 'fork', | ||
| loggedIn: false, | ||
| viewerIsAdmin: false, | ||
| trusted: false, | ||
| featured: false, | ||
| readmeContent: null, | ||
| starCount: 0, | ||
| starredByViewer: false, | ||
| }, | ||
| }) | ||
| expect(community.title).toBe('Calendar helper — Kody community package') | ||
| expect(community.og?.description).toBe('Helps with calendars.') | ||
| expect(community.og?.imagePath).toBe('/community/listing-1/og.png') | ||
|
|
||
| const publicProfile = resolveDocumentHead('/@kent', { | ||
| profileShell: { | ||
| ok: true, | ||
| username: 'kent', | ||
| displayName: 'Kent C. Dodds', | ||
| bio: 'Builder of things.', | ||
| isSelf: false, | ||
| loggedIn: false, | ||
| isFollowing: false, | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| expect(publicProfile.title).toBe('Kent C. Dodds') | ||
| expect(publicProfile.og?.title).toBe('Kent C. Dodds (@kent) — Kody') | ||
| expect(publicProfile.og?.description).toBe('Builder of things.') | ||
|
|
||
| const privateProfile = resolveDocumentHead('/@kent', { | ||
| profileShell: { | ||
| ok: true, | ||
| username: 'kent', | ||
| displayName: 'Kent C. Dodds', | ||
| bio: 'Hidden bio', | ||
| isSelf: false, | ||
| loggedIn: false, | ||
| isFollowing: false, | ||
| visibility: 'private', | ||
| }, | ||
| }) | ||
| expect(privateProfile.title).toBe('Kent C. Dodds') | ||
| expect(privateProfile.og).toBeUndefined() | ||
| }) | ||
|
|
||
| test('resolveDocumentHead falls back when dynamic loader data is missing', () => { | ||
| expect(resolveDocumentHead('/blog/hello-world')).toEqual({ title: 'Blog' }) | ||
| expect(resolveDocumentHead('/community/listing-1')).toEqual({ | ||
| title: 'Community packages', | ||
| }) | ||
| expect(resolveDocumentHead('/@kent').title).toBe('@kent') | ||
| expect(resolveDocumentHead('/this-route-does-not-exist').title).toBe( | ||
| NOT_FOUND_DOCUMENT_TITLE, | ||
| ) | ||
| }) | ||
|
|
||
| test('absolutizeDocumentHead expands origin-relative paths', () => { | ||
| const resolved = absolutizeDocumentHead( | ||
| resolveDocumentHead('/blog', undefined), | ||
| 'https://heykody.dev', | ||
| ) | ||
| expect(resolved.canonicalUrl).toBe('https://heykody.dev/blog') | ||
| expect(resolved.og?.imageUrl).toBe('https://heykody.dev/og/blog.png') | ||
| expect(resolved.links?.[0]?.href).toBe('https://heykody.dev/blog/rss.xml') | ||
| }) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Decouple canonical URL and Open Graph metadata.
The current logic strictly couples Open Graph tags and canonical URLs. If a route provides a
canonicalUrlbut noogmetadata, the canonical link is completely skipped. Conversely, ifogis provided without acanonicalUrl, all OG and Twitter tags are skipped.These should be evaluated and appended independently, as a canonical URL is an independent SEO concept from Open Graph.
♻️ Proposed fix to evaluate them independently
📝 Committable suggestion
🤖 Prompt for AI Agents