最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Next.js app folder: is there some "entry point"? A direct replacement for _app.tsx and _document.

programmeradmin3浏览0评论

Previously I had a custom _app.tsx that did some bootstrapping for my application: useEffect(() => void defineCustomElements(), []); It loaded custom elements/web ponents and made sure they can be hydrated.

Where should I put this effect now with the app router that 1. doesn't have a "global" _app.tsx and secondly is "server ponent first"?

Previously I had a custom _app.tsx that did some bootstrapping for my application: useEffect(() => void defineCustomElements(), []); It loaded custom elements/web ponents and made sure they can be hydrated.

Where should I put this effect now with the app router that 1. doesn't have a "global" _app.tsx and secondly is "server ponent first"?

Share Improve this question edited Aug 25, 2023 at 21:40 Youssouf Oumar 46.1k16 gold badges100 silver badges104 bronze badges asked Apr 6, 2023 at 15:16 sandroocosandrooco 8,73611 gold badges55 silver badges98 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 13

On the Upgrade Guide page, they say "pages/_app.js and pages/_document.js have been replaced with a single app/layout.js root layout".

In your case where you need client interactions in the layout, you could do as below, for example, using a separate client ponent that you import in the layout:

// DefineCustomElements.js

"use client";

export default function DefineCustomElements() {
  useEffect(() => defineCustomElements(), []);

  return <></>;
}

// app/layout.js

import DefineCustomElements from "./DefineCustomElements";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <DefineCustomElements />
        {children}
      </body>
    </html>
  );
}
发布评论

评论列表(0)

  1. 暂无评论