文档头部管理是管理文档的 head、title、meta、link 和 script 标签的过程,TanStack Router 为使用 Start 的全栈应用程序和使用 @tanstack/react-router 的单页应用程序提供了一种强大的文档头部管理方式。它提供:
对于使用 Start 的全栈应用程序,甚至对于使用 @tanstack/react-router 的单页应用程序,管理文档头部是任何应用程序的关键部分,原因如下:
要管理文档头部,需要渲染 <HeadContent /> 和 <Scripts /> 组件,并使用 routeOptions.head 属性来管理路由的头部,该属性返回一个包含 title、meta、links、styles 和 scripts 属性的对象。
export const Route = createRootRoute({
head: () => ({
meta: [
{
name: 'description',
content: 'My App is a web application',
},
{
title: 'My App',
},
],
links: [
{
rel: 'icon',
href: '/favicon.ico',
},
],
styles: [
{
media: 'all and (max-width: 500px)',
children: `p {
color: blue;
background-color: yellow;
}`
}
]
scripts: [
{
src: 'https://www.google-analytics.com/analytics.js',
},
],
}),
})
export const Route = createRootRoute({
head: () => ({
meta: [
{
name: 'description',
content: 'My App is a web application',
},
{
title: 'My App',
},
],
links: [
{
rel: 'icon',
href: '/favicon.ico',
},
],
styles: [
{
media: 'all and (max-width: 500px)',
children: `p {
color: blue;
background-color: yellow;
}`
}
]
scripts: [
{
src: 'https://www.google-analytics.com/analytics.js',
},
],
}),
})
开箱即用,TanStack Router 将对 title 和 meta 标签进行去重,优先选择在嵌套路由中找到的每个标签的最后一次出现。
<HeadContent /> 组件是必需的,用于渲染文档的 head、title、meta、link 和与头部相关的 script 标签。
如果您的应用程序不能或无法管理 <head> 标签,它应该在根布局的 <head> 标签中渲染,或者在组件树中尽可能高的位置渲染。
import { HeadContent } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => (
<html>
<head>
<HeadContent />
</head>
<body>
<Outlet />
</body>
</html>
),
})
import { HeadContent } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => (
<html>
<head>
<HeadContent />
</head>
<body>
<Outlet />
</body>
</html>
),
})
首先,如果您设置了任何 <title> 标签,请从 index.html 中删除它。
import { HeadContent } from '@tanstack/react-router'
const rootRoute = createRootRoute({
component: () => (
<>
<HeadContent />
<Outlet />
</>
),
})
import { HeadContent } from '@tanstack/react-router'
const rootRoute = createRootRoute({
component: () => (
<>
<HeadContent />
<Outlet />
</>
),
})
除了可以在 <head> 标签中渲染的脚本外,您还可以使用 routeOptions.scripts 属性在 <body> 标签中渲染脚本。这对于加载需要 DOM 加载但在应用程序主入口点之前的脚本(甚至内联脚本)很有用(如果您使用 Start 或 TanStack Router 的全栈实现,这包括水合)。
要做到这一点,您必须:
export const Route = createRootRoute({
scripts: [
{
children: 'console.log("Hello, world!")',
},
],
})
export const Route = createRootRoute({
scripts: [
{
children: 'console.log("Hello, world!")',
},
],
})
<Scripts /> 组件是必需的,用于渲染文档的 body 脚本。如果您的应用程序不能或无法管理 <body> 标签,它应该在根布局的 <body> 标签中渲染,或者在组件树中尽可能高的位置渲染。
import { createFileRoute, Scripts } from '@tanstack/react-router'
export const Router = createFileRoute('/')({
component: () => (
<html>
<head />
<body>
<Outlet />
<Scripts />
</body>
</html>
),
})
import { createFileRoute, Scripts } from '@tanstack/react-router'
export const Router = createFileRoute('/')({
component: () => (
<html>
<head />
<body>
<Outlet />
<Scripts />
</body>
</html>
),
})
import { Scripts, createRootRoute } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => (
<>
<Outlet />
<Scripts />
</>
),
})
import { Scripts, createRootRoute } from '@tanstack/react-router'
export const Route = createRootRoute({
component: () => (
<>
<Outlet />
<Scripts />
</>
),
})