Next.js 在 Vercel 上构建时,当包含元数据时会失败。为什么?

huangapple go评论71阅读模式
英文:

Next js build in vercel fails when with meta data. Why?

问题

以下是翻译好的部分:

这是layout.js。我已经注释掉了导出的metadata常量,这样做可以成功部署到Vercel。如果不注释掉metadata,部署将失败。

"use client";
import "./globals.css";
import { store } from "./store/store";
import { Provider } from "react-redux";

/*export const metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};*/

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <Provider store={store}>
        <body
          style={{
            minHeight: "100vh",
            maxHeight: "fit-content",
            boxSizing: "border-box",
            backgroundColor: "#f2f5fd",
          }}
        >
          {children}
        </body>
      </Provider>
    </html>
  );
}

当取消注释metadata时,构建部署会失败,并出现以下错误:

Failed to compile.
14:35:07.115

14:35:07.115
./app/layout.js
14:35:07.115
ReactServerComponentsError:
14:35:07.115

14:35:07.115
You are attempting to export "metadata" from a component marked with "use client", which is disallowed. Either remove the export, or the "use client" directive. Read more: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive
14:35:07.115

14:35:07.115
   ,-/vercel/path0/app/layout.js:3:1]
14:35:07.115
3 | import { store } from "./store/store";
14:35:07.115
4 | import { Provider } from "react-redux";
14:35:07.115
5 | 
14:35:07.115
6 | export const metadata = {
14:35:07.116
   :              ^^^^^^^^
14:35:07.116
7 |   title: "Create Next App",
14:35:07.116
8 |   description: "Generated by create next app",
14:35:07.116
9 | };
14:35:07.116
   `----
14:35:07.116

14:35:07.116
File path:
14:35:07.116
  ./app/layout.js
14:35:07.116

14:35:07.116

14:35:07.116
> Build failed because of webpack errors
14:35:07.147
Error: Command "npm run build" exited with 1
14:35:07.372
BUILD_UTILS_SPAWN_1: Command "npm run build" exited with 1

为什么metadata会导致错误,以及解决方法是什么?

英文:

Here is layout.js. I have commented out the export const metadata, doing this makes the deployment to Vercel successful. Without commenting out the metadata, the deployment will fail.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&quot;use client&quot;;
import &quot;./globals.css&quot;;
import { store } from &quot;./store/store&quot;;
import { Provider } from &quot;react-redux&quot;;

/*export const metadata = {
  title: &quot;Create Next App&quot;,
  description: &quot;Generated by create next app&quot;,
};*/

export default function RootLayout({ children }) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;Provider store={store}&gt;
        &lt;body
          style={{
            minHeight: &quot;100vh&quot;,
            maxHeight: &quot;fit-content&quot;,
            boxSizing: &quot;border-box&quot;,
            backgroundColor: &quot;#f2f5fd&quot;,
          }}
        &gt;
          {children}
        &lt;/body&gt;
      &lt;/Provider&gt;
    &lt;/html&gt;
  );
}

<!-- end snippet -->

When I un-comment the metadata, the build deployment fails with the following errors.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Failed to compile.
14:35:07.115

14:35:07.115
./app/layout.js
14:35:07.115
ReactServerComponentsError:
14:35:07.115

14:35:07.115
You are attempting to export &quot;metadata&quot; from a component marked with &quot;use client&quot;, which is disallowed. Either remove the export, or the &quot;use client&quot; directive. Read more: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive
14:35:07.115

14:35:07.115
   ,-[/vercel/path0/app/layout.js:3:1]
14:35:07.115
3 | import { store } from &quot;./store/store&quot;;
14:35:07.115
4 | import { Provider } from &quot;react-redux&quot;;
14:35:07.115
5 | 
14:35:07.115
6 | export const metadata = {
14:35:07.116
   :              ^^^^^^^^
14:35:07.116
7 |   title: &quot;Create Next App&quot;,
14:35:07.116
8 |   description: &quot;Generated by create next app&quot;,
14:35:07.116
9 | };
14:35:07.116
   `----
14:35:07.116

14:35:07.116
File path:
14:35:07.116
  ./app/layout.js
14:35:07.116

14:35:07.116

14:35:07.116
&gt; Build failed because of webpack errors
14:35:07.147
Error: Command &quot;npm run build&quot; exited with 1
14:35:07.372
BUILD_UTILS_SPAWN_1: Command &quot;npm run build&quot; exited with 1

<!-- end snippet -->

Why does having the metadata cause the errors and what's the solution?

答案1

得分: 1

After &lt;HTML&gt; starting tag on Layout I added Head tag to fix this problem!

    &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
        &lt;title&gt;Title&lt;/title&gt;
        &lt;meta name=&#39;description&#39; content=&#39;Description&#39; /&gt;
      &lt;/head&gt;
      &lt;body className={`${inter.className} max-w-[1580px] mx-auto`}&gt;
        
      &lt;/body&gt;
    &lt;/html&gt;
英文:

After &lt;HTML&gt; starting tag on Layout I added Head tag to fix this problem!

    &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
        &lt;title&gt;Title&lt;/title&gt;
        &lt;meta name=&#39;description&#39; content=&#39;Description&#39; /&gt;
      &lt;/head&gt;
      &lt;body className={`${inter.className} max-w-[1580px] mx-auto`}&gt;
        
      &lt;/body&gt;
    &lt;/html&gt;

答案2

得分: 0

从这里开始:

> 新的Metadata API 允许您在服务器组件中使用显式的元数据配置来定义元数据(例如在HTML head元素内部的meta和link标签)。

元数据仅在服务器组件中起作用。

英文:

from here

> The new Metadata API allows you to define metadata (e.g. meta and link
> tags inside your HTML head element) with an explicit metadata
> configuration in any layout or page that is a Server Component.

Metadata works only in server components

答案3

得分: 0

在Next.js中,“use client”指令用于从组件直接导出数据以执行服务器端渲染(SSR)。当您使用“use client”时,组件旨在仅在客户端执行代码。这意味着您不应该从标有“use client”的组件导出任何变量或数据,因为这会导致服务器端渲染出现问题。

错误消息正在通知您有关此违规行为,并建议两种可能的解决方法:

  1. 从组件中移除“metadata”的导出。
  2. 如果需要从组件导出数据,则移除“use client”指令。

通过遵循这两种解决方案中的任何一种,您可以确保组件符合Next.js关于服务器端渲染和数据导出的指南,从而解决ReactServerComponentsError。

英文:

In Next.js, the "use client" directive is used when exporting data directly from the component to perform server-side rendering (SSR). When you use "use client," the component is intended to execute code only on the client-side. It means that you should not export any variables or data from a component marked with "use client" because it would cause issues with server-side rendering.

The error message is notifying you about this violation, suggesting two possible solutions:

  1. Remove the export of "metadata" from the component.
  2. Remove the "use client" directive if you need to export data from the component.

By following either of these solutions, you ensure that the component adheres to Next.js' guidelines for server-side rendering and exporting data, resolving the ReactServerComponentsError.

huangapple
  • 本文由 发表于 2023年5月29日 07:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定