Skip to content

feat: use generic default meta tags unless not overridden #4274

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

Open
wants to merge 12 commits into
base: alpha
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/react-router/src/HeadContent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { applyDefaultMeta } from '@tanstack/router-core'
import { Asset } from './Asset'
import { useRouter } from './useRouter'
import { useRouterState } from './useRouterState'
Expand All @@ -17,6 +18,8 @@ export const useTags = () => {
const resultMeta: Array<RouterManagedTag> = []
const metaByAttribute: Record<string, true> = {}
let title: RouterManagedTag | undefined

applyDefaultMeta(routeMeta, resultMeta, metaByAttribute)
;[...routeMeta].reverse().forEach((metas) => {
;[...metas].reverse().forEach((m) => {
if (!m) return
Expand Down
4 changes: 3 additions & 1 deletion packages/react-router/tests/Scripts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ describe('ssr HeadContent', () => {
head: ({ loaderData }) => {
return {
meta: [
{ charSet: 'ascii' },
{
title: 'Root',
},
Expand Down Expand Up @@ -202,6 +203,7 @@ describe('ssr HeadContent', () => {
await router.load()

expect(router.state.matches.map((d) => d.meta).flat(1)).toEqual([
{ charSet: 'ascii' },
{ title: 'Root' },
{ name: 'description', content: 'Root' },
{ name: 'image', content: 'image.jpg' },
Expand All @@ -217,7 +219,7 @@ describe('ssr HeadContent', () => {
<RouterProvider router={router} />,
)
expect(html).toEqual(
`<title>Index</title><meta name="image" content="image.jpg"/><meta property="og:description" content="Root description"/><meta name="description" content="Index"/><meta name="last-modified" content="2021-10-10"/><meta property="og:image" content="index-image.jpg"/>`,
`<meta charSet="ascii"/><meta name="viewport" content="width=device-width, initial-scale=1"/><title>Index</title><meta name="image" content="image.jpg"/><meta property="og:description" content="Root description"/><meta name="description" content="Index"/><meta name="last-modified" content="2021-10-10"/><meta property="og:image" content="index-image.jpg"/>`,
)
})
})
38 changes: 38 additions & 0 deletions packages/router-core/src/head-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { RouterManagedTag } from './manifest'

const defaultMeta: Array<{
key: string
tag: 'meta'
matcher: (m: any) => boolean
attrs: Record<string, string>
}> = [
{
key: 'charSet',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the line specifically i'm thinking won't work with solid

tag: 'meta',
matcher: (m) => m?.charset != null || m?.charSet != null,
attrs: { charset: 'utf-8' },
},
{
key: 'viewport',
tag: 'meta',
matcher: (m) => m?.name === 'viewport',
attrs: {
name: 'viewport',
content: 'width=device-width, initial-scale=1',
},
},
]

export function applyDefaultMeta(
routeMeta: Array<Array<any>>,
resultMeta: Array<RouterManagedTag>,
metaByAttribute: Record<string, boolean>,
) {
for (const { key, matcher, tag, attrs } of defaultMeta) {
const already = routeMeta.flat(1).some(matcher)
if (!already) {
resultMeta.push({ tag, attrs })
metaByAttribute[key] = true
}
}
}
2 changes: 2 additions & 0 deletions packages/router-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,5 @@ export type {
ValidateUseSearchResult,
ValidateUseParamsResult,
} from './typePrimitives'

export { applyDefaultMeta } from './head-content'
3 changes: 3 additions & 0 deletions packages/solid-router/src/HeadContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Solid from 'solid-js'
import { MetaProvider } from '@solidjs/meta'
import { applyDefaultMeta } from '@tanstack/router-core'
import { Asset } from './Asset'
import { useRouter } from './useRouter'
import { useRouterState } from './useRouterState'
Expand All @@ -18,6 +19,8 @@ export const useTags = () => {
const resultMeta: Array<RouterManagedTag> = []
const metaByAttribute: Record<string, true> = {}
let title: RouterManagedTag | undefined

applyDefaultMeta(routeMeta(), resultMeta, metaByAttribute)
;[...routeMeta()].reverse().forEach((metas) => {
;[...metas].reverse().forEach((m) => {
if (!m) return
Expand Down
2 changes: 2 additions & 0 deletions packages/solid-router/tests/Scripts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe('ssr HeadContent', () => {
head: ({ loaderData }) => {
return {
meta: [
{ charSet: 'ascii' },
{
title: 'Root',
},
Expand Down Expand Up @@ -186,6 +187,7 @@ describe('ssr HeadContent', () => {
await router.load()

expect(router.state.matches.map((d) => d.meta).flat(1)).toEqual([
{ charSet: 'ascii' },
{ title: 'Root' },
{ name: 'description', content: 'Root' },
{ name: 'image', content: 'image.jpg' },
Expand Down