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
1 change: 1 addition & 0 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ declare module '@tanstack/react-router' {
baseParent?: boolean
Title?: () => any
showNavbar?: boolean
includeSearchInCanonical?: boolean
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/routes/$libraryId/$version.docs.npm-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export const Route = createFileRoute('/$libraryId/$version/docs/npm-stats')({
height: v.fallback(v.optional(v.number(), 400), 400),
}),
component: RouteComponent,
staticData: {
includeSearchInCanonical: true,
},
})

type NpmStatsSearch = {
Expand Down
20 changes: 18 additions & 2 deletions src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,21 @@ function ShellComponent({ children }: { children: React.ReactNode }) {
select: (s) => s.location?.pathname || '/',
})

const canonicalSearchStr = useRouterState({
select: (s) => s.location?.searchStr || '',
})

const includeSearchInCanonical = useMatches({
select: (s) =>
s.some((d) => d.staticData?.includeSearchInCanonical === true),
})

const preferredCanonicalPath = getCanonicalPath(canonicalPath)
const pageUrl = canonicalUrl(preferredCanonicalPath ?? canonicalPath)
const canonicalSearch = includeSearchInCanonical ? canonicalSearchStr : ''
const pageUrl = canonicalUrl(
preferredCanonicalPath ?? canonicalPath,
canonicalSearch,
)

const showDevtools = import.meta.env.DEV && canShowDevtools

Expand All @@ -199,7 +212,10 @@ function ShellComponent({ children }: { children: React.ReactNode }) {
<html lang="en" className={htmlClass} suppressHydrationWarning>
<head>
{preferredCanonicalPath ? (
<link rel="canonical" href={canonicalUrl(preferredCanonicalPath)} />
<link
rel="canonical"
href={canonicalUrl(preferredCanonicalPath, canonicalSearch)}
/>
) : null}
<meta property="og:url" content={pageUrl} />
<meta name="twitter:url" content={pageUrl} />
Expand Down
1 change: 1 addition & 0 deletions src/routes/builder.index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const Route = createFileRoute('/builder/')({
validateSearch: builderSearchSchema,
component: RouteComponent,
staticData: {
includeSearchInCanonical: true,
Title: () => (
<Link
to="/builder"
Expand Down
3 changes: 3 additions & 0 deletions src/routes/intent/registry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export const Route = createFileRoute('/intent/registry/')({
}),
}),
component: IntentRegistryPage,
staticData: {
includeSearchInCanonical: true,
},
})

function IntentRegistryPage() {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/maintainers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const searchSchema = v.object({
export const Route = createFileRoute('/maintainers')({
component: RouteComponent,
validateSearch: searchSchema,
staticData: {
includeSearchInCanonical: true,
},
head: () => ({
meta: seo({
title: 'Maintainers | TanStack',
Expand Down
3 changes: 3 additions & 0 deletions src/routes/partners.index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function getPartnerFilterAnalytics(search: PartnersSearch) {
export const Route = createFileRoute('/partners/')({
component: PartnersIndexPage,
validateSearch: searchSchema,
staticData: {
includeSearchInCanonical: true,
},
head: () => ({
meta: seo({
title: 'Partners',
Expand Down
3 changes: 3 additions & 0 deletions src/routes/shop.collections.$handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const Route = createFileRoute('/shop/collections/$handle')({
}
},
component: CollectionPage,
staticData: {
includeSearchInCanonical: true,
},
})

function CollectionPage() {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/shop.index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const Route = createFileRoute('/shop/')({
return { page, sortId: sortOptionId(sortOption) }
},
component: ShopIndex,
staticData: {
includeSearchInCanonical: true,
},
})

function ShopIndex() {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/shop.search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export const Route = createFileRoute('/shop/search')({
return { query: q, totalCount: page.totalCount, page }
},
component: SearchPage,
staticData: {
includeSearchInCanonical: true,
},
})

function SearchPage() {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/stats/npm/$packages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export const Route = createFileRoute('/stats/npm/$packages')({
}
},
component: RouteComponent,
staticData: {
includeSearchInCanonical: true,
},
})

function RouteComponent() {
Expand Down
1 change: 1 addition & 0 deletions src/routes/stats/npm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export const Route = createFileRoute('/stats/npm/')({
},
component: RouteComponent,
staticData: {
includeSearchInCanonical: true,
Title: () => {
return (
<Link
Expand Down
6 changes: 4 additions & 2 deletions src/utils/seo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ export function shouldIndexPath(path: string) {
return getCanonicalPath(path) !== null
}

export function canonicalUrl(path: string) {
export function canonicalUrl(path: string, search?: string) {
const origin = trimTrailingSlash(
env.URL ||
(import.meta.env.SSR ? env.SITE_URL : undefined) ||
DEFAULT_SITE_URL,
)

return `${origin}${normalizePath(path)}`
const normalizedSearch = search && search !== '?' ? search : ''

return `${origin}${normalizePath(path)}${normalizedSearch}`
}

type SeoOptions = {
Expand Down
Loading