正在加载视频...

视频加载失败

How to show a dynamic user profile in the header while keeping a statically generated page in Next with Auth.js (without PPR)

61,186 次观看 • 1 年前 •via X (Twitter)

10 条评论

Alex Sidorenko 的头像
Alex Sidorenko1 年前

Explanation: Server side auth check - "await auth()" opts the entire page into a dynamic rendering. Most of the time it's fine, as you likely use some user specific content on the page, so it will be dynamic anyway. However, sometimes you might want to combine the dynamic user profile in the header with a static page (example: landing page). The simplest way to do this right now, is to use the client version of auth. In case of Auth.js it's useSession hook. By using this hook you tell the component to send a request to "api/auth/session" on the client to get user data. This way we can keep the underlying page static. P.S. You can use server side auth - "await auth()" with static page using PPR. Just wrap the dynamic part with Suspense and Next will statically generate entire page and dynamically stream in the Suspense bit. But PPR is still experimental.

eSignatures.com 的头像
eSignatures.com1 年前

Discover Add secure, embedded signing to your WordPress site in seconds. Try now!

Morpheus 的头像
Morpheus1 年前

How do you get that static route notification at the bottom left?

Manu 的头像
Manu1 年前

must admit that it took me a while to understand that passing a server component as a prop/child to a client component is a trick to avoid moving the client boundary.

Madat Bayramov 的头像
Madat Bayramov1 年前

What page type you get after building next app? In my case I’m also using the same logic. Root layout has session provider component that wraps all children. Even static pages under that layout are built as dynamic (with f flag instead of cycle)

夜奏花 的头像
夜奏花1 年前

What’s the difference between

Alex Sidorenko 的头像
Alex Sidorenko1 年前

Between what and what?

Raqib Nur 的头像
Raqib Nur1 年前

It’s a must-have. I’ve seen someone raise an issue about it.

Abdalrahman Mahmoud 的头像
Abdalrahman Mahmoud1 年前

hey a quick question why does it matter if the page is statically or dynamically generated?

Alex Sidorenko 的头像
Alex Sidorenko1 年前

Static pages are faster and cheaper. Next, pre-generate HTML (during the build time) that can be served via CDN close to your users. Plus, it reduces the server load/hosting costs, as you don't need to re-render a page on the server on every request.

相关视频