NextJs 项目版本从 9.0.1 升级到 9.0.4 的问题

Fre*_*d A 0 reactjs next.js

我将我的项目版本从 package.json 从 9.0.1 升级到 9.0.4

"next": "9.0.4"
Run Code Online (Sandbox Code Playgroud)

此项目升级的目的是使用 NextJs 9.0.4 版中包含的内置压缩。

而且我已经确保,根据 NextJs 文档,来自 next/document 的 Head 仅在 _document 内部使用,而来自 next/head 的 Head 用于其他任何地方。

import Document, { Html, Head, Main, NextScript } from "next/document"; // For _document.js use only
import Head from "next/head"; // For every other pages and _app
Run Code Online (Sandbox Code Playgroud)

在这个项目版本升级后,我注意到了几件事

首先,缺少下一个人数统计标签。当我在开发模式下运行它时,它弹出了这个控制台错误

index.js:1 Warning: next-head-count is missing. https://err.sh/next.js/next-head-count-missing
Run Code Online (Sandbox Code Playgroud)

我检查了这个,发现 next-head-count 是在 body 标签内渲染的,而它应该在 head 标签内渲染。

其次,我注意到链接标签和标题都在 head 和 body 标签内呈现。

<head>
  // All the link tags rendered in here
</head>
<body>
  // next-head-count rendered in here 
  // Title tag in here
  // All the link tags rendered in here too
</body>
Run Code Online (Sandbox Code Playgroud)

双重渲染问题

这些在 NextJs 中正常吗?我害怕在实时模式下,下一个人头数错误会让我损失 SEO、功能和其他东西。

Fre*_*d A 6

好的,经过数小时的调试和谷歌搜索,终于找到了根本原因。

事实证明,Head 标签内不允许有 div。Head 中的任何 div(或生成 div 的 React 组件)都会导致该 Head 在 Body 中呈现。Head 中有一个 GoogleTagManager 组件,它返回一个 div 块。删除它后,一切顺利,头部按预期正确渲染。