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

Add canonical link to tanstack/query page #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/routes/query.$version.docs.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { seo } from '~/utils/seo'
import { redirect, useLoaderData, useParams } from '@remix-run/react'
import { loadDocs } from '~/utils/docs'
import { Doc } from '~/components/Doc'
import { mergeMeta } from '~/utils/mergeMeta'

export const loader = async (context: LoaderFunctionArgs) => {
const { '*': docsPath, version } = context.params
Expand All @@ -29,12 +30,12 @@ export const loader = async (context: LoaderFunctionArgs) => {
})
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
export const meta: MetaFunction<typeof loader> = mergeMeta(({ data }) => {
return seo({
title: `${data?.title} | TanStack Query Docs`,
description: data?.description,
})
}
})

export const ErrorBoundary = DefaultErrorBoundary

Expand Down
5 changes: 3 additions & 2 deletions app/routes/query.$version.docs.framework.$framework.$.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { seo } from '~/utils/seo'
import { useLoaderData, useParams } from '@remix-run/react'
import { Doc } from '~/components/Doc'
import { loadDocs } from '~/utils/docs'
import { mergeMeta } from '~/utils/mergeMeta'

export const loader = async (context: LoaderFunctionArgs) => {
const { '*': docsPath, framework, version } = context.params
Expand All @@ -19,12 +20,12 @@ export const loader = async (context: LoaderFunctionArgs) => {
})
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
export const meta: MetaFunction<typeof loader> = mergeMeta(({ data }) => {
return seo({
title: `${data?.title} | TanStack Query Docs`,
description: data?.description,
})
}
})

export const ErrorBoundary = DefaultErrorBoundary

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { repo, getBranch } from '~/projects/query'
import { seo } from '~/utils/seo'
import { capitalize, slugToTitle } from '~/utils/utils'
import { FaExternalLinkAlt } from 'react-icons/fa'
import { mergeMeta } from '~/utils/mergeMeta'

export const loader = async (context: LoaderFunctionArgs) => {
const { version, framework, '*': name } = context.params

return json({ version, framework, name })
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
export const meta: MetaFunction<typeof loader> = mergeMeta(({ data }) => {
return seo({
title: `${capitalize(data.framework)} Query ${slugToTitle(
data.name
Expand All @@ -23,7 +24,7 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
data.name
)} in ${capitalize(data.framework)} Query`,
})
}
})

export default function RouteExamples() {
const { version, framework, name } = useLoaderData<typeof loader>()
Expand Down
10 changes: 9 additions & 1 deletion app/routes/query.$version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DefaultErrorBoundary } from '~/components/DefaultErrorBoundary'
import { availableVersions, latestVersion } from '~/projects/query'
import { RedirectVersionBanner } from '~/components/RedirectVersionBanner'
import { useClientOnlyRender } from '~/utils/useClientOnlyRender'
import type { LoaderFunctionArgs } from '@remix-run/node'
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node'

export const loader = async (context: LoaderFunctionArgs) => {
const { version } = context.params
Expand All @@ -20,6 +20,14 @@ export const loader = async (context: LoaderFunctionArgs) => {
})
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return [{
tagName: "link",
rel: "canonical",
href: data?.redirectUrl,
}]
}

export const ErrorBoundary = DefaultErrorBoundary

export default function RouteVersionParam() {
Expand Down
39 changes: 39 additions & 0 deletions app/utils/mergeMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// https://gist.github.com/ryanflorence/ec1849c6d690cfbffcb408ecd633e069
import type { MetaFunction } from "@remix-run/node";

export const mergeMeta = (
overrideFn: MetaFunction,
appendFn?: MetaFunction,
): MetaFunction => {
return arg => {
// get meta from parent routes
let mergedMeta = arg.matches.reduce((acc, match) => {
return acc.concat(match.meta || []);
}, [] as ReturnType<MetaFunction>);

// replace any parent meta with the same name or property with the override
let overrides = overrideFn(arg);
for (let override of overrides) {
let index = mergedMeta.findIndex(
meta =>
("name" in meta &&
"name" in override &&
meta.name === override.name) ||
("property" in meta &&
"property" in override &&
meta.property === override.property) ||
("title" in meta && "title" in override),
);
if (index !== -1) {
mergedMeta.splice(index, 1, override);
}
}

// append any additional meta
if (appendFn) {
mergedMeta = mergedMeta.concat(appendFn(arg));
}

return mergedMeta;
};
};