如何优化第三方库
使用 @next/third-parties 包优化应用程序中第三方库的性能。
@next/third-parties 是一个库,提供了一系列组件和工具,可提升在 Next.js 应用程序中加载热门第三方库的性能和开发者体验。
@next/third-parties 提供的所有第三方集成均经过性能优化,并且易于使用。
快速开始
首先,安装 @next/third-parties 库:
pnpm
pnpm add @next/third-parties@latest next@latest
npm
npm install @next/third-parties@latest next@latest
yarn
yarn add @next/third-parties@latest next@latest
bun
bun add @next/third-parties@latest next@latest
@next/third-parties 目前是一个正在积极开发的实验性库。在我们努力添加更多第三方集成的期间,建议使用 latest 或 canary 标签进行安装。
Google 第三方
Google 所有受支持的第三方库都可以从 @next/third-parties/google 导入。
Google Tag Manager
可以使用 GoogleTagManager 组件在页面上实例化 Google Tag Manager 容器。默认情况下,它会在页面发生水合后获取原始内联脚本。
要为所有路由加载 Google Tag Manager,请将该组件直接包含在根布局中,并传入你的 GTM 容器 ID:
TypeScript
import { GoogleTagManager } from '@next/third-parties/google'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<GoogleTagManager gtmId="GTM-XYZ" />
<body>{children}</body>
</html>
)
}
JavaScript
import { GoogleTagManager } from '@next/third-parties/google'
export default function RootLayout({ children }) {
return (
<html lang="en">
<GoogleTagManager gtmId="GTM-XYZ" />
<body>{children}</body>
</html>
)
}
要为单个路由加载 Google Tag Manager,请将该组件包含在你的页面文件中:
import { GoogleTagManager } from '@next/third-parties/google'
export default function Page() {
return <GoogleTagManager gtmId="GTM-XYZ" />
}
发送事件
可以使用 sendGTMEvent 函数,通过 dataLayer 对象发送事件来跟踪页面上的用户交互。要使该函数正常工作,<GoogleTagManager /> 组件必须包含在父级布局、页面或组件中,或者直接包含在同一文件中。
'use client'
import { sendGTMEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<div>
<button
onClick={() => sendGTMEvent({ event: 'buttonClicked', value: 'xyz' })}
>
Send Event
</button>
</div>
)
}
请参阅 Tag Manager 开发者文档,了解可以传入该函数的各种变量和事件。
服务器端标记
如果你使用的是服务器端标记管理器,并从标记服务器提供 gtm.js 脚本,则可以使用 gtmScriptUrl 选项来指定脚本的 URL。
选项
传递给 Google Tag Manager 的选项。如需完整的选项列表,请阅读 Google Tag Manager 文档。
| 名称 | 类型 | 描述 |
|---|---|---|
gtmId |
必填* | 你的 GTM 容器 ID。通常以 GTM- 开头。 |
gtmScriptUrl |
可选* | GTM 脚本 URL。默认为 https://www.googletagmanager.com/gtm.js。 |
dataLayer |
可选 | 用于实例化容器的数据层对象。 |
dataLayerName |
可选 | 数据层的名称。默认为 dataLayer。 |
auth |
可选 | 用于环境代码段的身份验证参数(gtm_auth)的值。 |
preview |
可选 | 用于环境代码段的预览参数(gtm_preview)的值。 |
*当提供 gtmScriptUrl 以支持 Google tag gateway for advertisers 时,可以省略 gtmId。
Google Analytics
GoogleAnalytics 组件可用于通过 Google tag(gtag.js)将 Google Analytics
4 包含到你的页面中。默认情况下,它会在页面上发生水合后获取原始脚本。
建议:
如果你的应用中已经包含 Google Tag Manager,你可以 直接使用它来配置 Google Analytics,而不是将 Google Analytics 作为单独的 组件包含。请参阅 文档 以了解更多关于 Tag Manager 与
gtag.js之间差异的信息。
要为所有路由加载 Google Analytics,请直接在根布局中包含该组件,并传入你的衡量 ID:
TypeScript
import { GoogleAnalytics } from '@next/third-parties/google'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
<GoogleAnalytics gaId="G-XYZ" />
</html>
)
}
JavaScript
import { GoogleAnalytics } from '@next/third-parties/google'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
<GoogleAnalytics gaId="G-XYZ" />
</html>
)
}
要为单个路由加载 Google Analytics,请在该页面的文件中包含该组件:
import { GoogleAnalytics } from '@next/third-parties/google'
export default function Page() {
return <GoogleAnalytics gaId="G-XYZ" />
}
发送事件
sendGAEvent 函数可用于通过 dataLayer 对象发送事件,以衡量你页面上的用户交互。要使此函数正常工作,<GoogleAnalytics /> 组件必须包含在父级布局、页面或组件中,或直接包含在同一文件中。
'use client'
import { sendGAEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<div>
<button
onClick={() => sendGAEvent('event', 'buttonClicked', { value: 'xyz' })}
>
Send Event
</button>
</div>
)
}
请参阅 Google Analytics 开发者文档,以了解更多关于事件参数的信息。
跟踪页面浏览量
当浏览器历史状态发生变化时,Google Analytics 会自动跟踪页面浏览量。这意味着 Next.js 路由之间的客户端导航无需任何配置就会发送页面浏览数据。
为确保客户端导航被正确衡量,请验证你的管理面板中已启用 “增强衡量” 属性,并勾选了 “页面更改基于浏览器历史事件” 复选框。
注意:
如果你决定手动发送页面浏览事件,请务必禁用默认的 页面浏览衡量,以避免产生重复数据。请参阅 Google Analytics 开发者 文档 以了解更多信息。
选项
传递给 <GoogleAnalytics> 组件的选项。
| 名称 | 类型 | 描述 |
|---|---|---|
gaId |
必填 | 你的 衡量 ID。通常以 G- 开头。 |
dataLayerName |
可选 | 数据层的名称。默认值为 dataLayer。 |
debugMode |
可选 | 启用 Google Analytics 调试模式。 |
nonce |
可选 | 一个 nonce。 |
Google Maps Embed
GoogleMapsEmbed 组件可用于向你的页面添加 Google Maps
Embed。默认情况下,它使用 loading 属性在首屏以下懒加载该嵌入内容。
import { GoogleMapsEmbed } from '@next/third-parties/google'
export default function Page() {
return (
<GoogleMapsEmbed
apiKey="XYZ"
height={200}
width="100%"
mode="place"
q="Brooklyn+Bridge,New+York,NY"
/>
)
}
选项
传递给 Google Maps Embed 的选项。如需查看完整的选项列表,请阅读 Google Map Embed docs。
| 名称 | 类型 | 描述 |
|---|---|---|
apiKey |
必填 | 你的 api key。 |
mode |
必填 | 地图模式 |
height |
可选 | 嵌入内容的高度。默认值为 auto。 |
width |
可选 | 嵌入内容的宽度。默认值为 auto。 |
style |
可选 | 向 iframe 传递样式。 |
allowfullscreen |
可选 | 允许地图的某些部分进入全屏的属性。 |
loading |
可选 | 默认为 lazy。如果你知道嵌入内容会位于首屏,可考虑更改。 |
q |
可选 | 定义地图标记的位置。根据地图模式的不同,此项可能是必需的。 |
center |
可选 | 定义地图视图的中心。 |
zoom |
可选 | 设置地图的初始缩放级别。 |
maptype |
可选 | 定义要加载的地图瓦片类型。 |
language |
可选 | 定义用于 UI 元素以及显示地图瓦片标签的语言。 |
region |
可选 | 根据地缘政治敏感性,定义要显示的合适边界和标签。 |
YouTube 嵌入
YouTubeEmbed 组件可用于加载并显示 YouTube 嵌入内容。该组件在底层使用 lite-youtube-embed,因而加载更快。
import { YouTubeEmbed } from '@next/third-parties/google'
export default function Page() {
return <YouTubeEmbed videoid="ogfYd705cRs" height={400} params="controls=0" />
}
选项
| 名称 | 类型 | 描述 |
|---|---|---|
videoid |
必需 | YouTube 视频 ID。 |
width |
可选 | 视频容器的宽度。默认为 auto |
height |
可选 | 视频容器的高度。默认为 auto |
playlabel |
可选 | 用于无障碍访问的播放按钮的视觉隐藏标签。 |
params |
可选 | 视频播放器参数定义在此处。 参数以查询参数字符串形式传递。 例如: params="controls=0&start=10&end=30" |
style |
可选 | 用于将样式应用到视频容器。 |