Skip to content
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
15 changes: 8 additions & 7 deletions packages/worker/client/client-router.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addEventListeners, type Handle } from 'remix/ui'
import { createMultiMatcher } from 'remix/route-pattern/match'
import { type AppLoaderData } from '#app/loader-data.ts'
import { applyDocumentTitle } from './document-title.ts'
import { applyDocumentHead } from './document-head.ts'
import {
abortIntentPrefetch,
prefetchRouteOnIntent,
Expand Down Expand Up @@ -503,11 +503,12 @@ async function runNavigationWithLoader(
setPreloadedNavigationData(nextPath, loadedData)
}

// Document title lives outside the hydrated `#root` tree, so SSR
// `<title>` would otherwise stick across SPA navigations. Resolve from
// the shared registry (+ loader data for dynamic routes) here so every
// route gets a title update without per-page wiring.
applyDocumentTitle(destination.pathname, loadedData)
// Document head lives outside the hydrated `#root` tree, so SSR
// `<title>` / OG / canonical tags would otherwise stick across SPA
// navigations. Resolve from the shared registry (+ loader data for
// dynamic routes) here so every route stays in sync without per-page
// wiring.
applyDocumentHead(destination.pathname, loadedData)

if (options?.skipPushState) {
notify()
Expand All @@ -522,7 +523,7 @@ async function runNavigationWithLoader(
// current URL) have no href change to trigger a route's fallback
// refetch, so mark the destination stale for routes to consume.
markNavigationDataStale(nextPath)
applyDocumentTitle(destination.pathname)
applyDocumentHead(destination.pathname)
if (options?.skipPushState) {
notify()
} else {
Expand Down
133 changes: 133 additions & 0 deletions packages/worker/client/document-head.ts
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,
})
}
Comment on lines +57 to +98

Copy link
Copy Markdown

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 canonicalUrl but no og metadata, the canonical link is completely skipped. Conversely, if og is provided without a canonicalUrl, 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
-	if (resolved.og && resolved.canonicalUrl) {
+	if (resolved.canonicalUrl) {
+		appendLink(head, 'canonical', {
+			rel: 'canonical',
+			href: resolved.canonicalUrl,
+		})
+	}
+
+	if (resolved.og) {
 		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,
-		})
+		if (resolved.canonicalUrl) {
+			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,
-		})
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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,
})
}
if (resolved.canonicalUrl) {
appendLink(head, 'canonical', {
rel: 'canonical',
href: resolved.canonicalUrl,
})
}
if (resolved.og) {
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',
})
if (resolved.canonicalUrl) {
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,
})
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/worker/client/document-head.ts` around lines 57 - 98, Decouple the
metadata logic in the head-generation flow: update the conditional around the
visible Open Graph/Twitter appends so they run whenever resolved.og exists, and
handle the canonical link independently whenever resolved.canonicalUrl exists.
Preserve each existing tag’s values and ensure either configuration works
without requiring the other.


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)
}
20 changes: 7 additions & 13 deletions packages/worker/client/document-title.ts
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'
1 change: 1 addition & 0 deletions packages/worker/client/routes/community-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export async function communityDetailRouteLoader(
ok: true,
listingId,
name: payload.listing.name,
description: payload.listing.description,
forkPrompt: payload.forkPrompt,
loggedIn: payload.loggedIn,
viewerIsAdmin: payload.viewerIsAdmin,
Expand Down
2 changes: 2 additions & 0 deletions packages/worker/client/routes/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function profileRouteLoader(
ok: true,
username: payload.profile.username,
displayName: payload.profile.displayName,
bio: payload.profile.bio,
isSelf: payload.isSelf,
loggedIn: payload.loggedIn,
isFollowing: payload.isFollowing,
Expand Down Expand Up @@ -146,6 +147,7 @@ export function ProfileRoute(handle: Handle) {
ok: true,
username: payload.profile.username,
displayName: payload.profile.displayName,
bio: payload.profile.bio,
isSelf: payload.isSelf,
loggedIn: payload.loggedIn,
isFollowing: payload.isFollowing,
Expand Down
130 changes: 130 additions & 0 deletions packages/worker/src/app/document-head.node.test.ts
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')
})
Loading
Loading