TIL

2024.05.28 #Next.js #How are Client Components Rendered? #์ถ”๋ก 

inz1234 2024. 5. 29. 14:48

๐Ÿšจ  TroubleShotting

 ์ €๋ฒˆ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…์— ์ด์–ด์„œ ์‚ฌ์‹ค ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด์ž ์˜๋ฌธ์ ์ด ํ•˜๋‚˜ ๋” ์žˆ์—ˆ๋Š”๋ฐ..

ํ•ด๊ฒฐ์ด ๋˜์—ˆ์œผ๋‹ˆ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐˆ๊นŒ ํ•˜๋‹ค๊ฐ€ ๋„ˆ๋ฌด ์ฐ์ฐํ•ด์„œ!

์ €๋ฒˆ์— ๋‚ฌ๋˜ ์ด ์˜ค๋ฅ˜๋Š” Suspense๊ฐ€ SSR- streaming์˜ ๋Œ€์ƒ์ด ๋˜๊ณ ,
Suspense์™€ ์—ฎ์—ฌ์žˆ๋Š” useSuspenseQuery์˜ queryFn ๋‚ด์—์„œ clientSupabase()๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—
pre-rendering(SSR) ์‹œ์—๋Š” clientSupabase()๊ฐ€ ์ž‘๋™์„ ์•ˆ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค. 


๋ฌธ์ œ ๋ฐœ์ƒ์˜ ๋ฐฐ๊ฒฝ - As Is

๊ทธ๋Ÿฐ๋ฐ ํ•˜๋‚˜ ๋” ์˜์•„ํ–ˆ๋˜ ๋ถ€๋ถ„์€ ์ด ์˜ค๋ฅ˜๊ฐ€ A -> B ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ๋Š” ๋œจ์ง€ ์•Š์•˜๋Š”๋ฐ, B ํŽ˜์ด์ง€์—์„œ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๋ฉด ๋œฌ๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์„œ์นญ์„ ํ•˜๋˜ ๋„์ค‘, Next.js ๊ณต์‹๋ฌธ์„œ์— Rendering > Client Components ์นดํ…Œ๊ณ ๋ฆฌ์˜

How are Client Components are Rendered?

 

Rendering: Client Components | Next.js

Learn how to use Client Components to render parts of your application on the client.

nextjs.org

๋ถ€๋ถ„์— ๋ณด๋ฉด
- Client ์ปดํฌ๋„ŒํŠธ๋Š” 
์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฑด์ง€, ์›๋ž˜ ์žˆ๋˜ ํŽ˜์ด์ง€์—์„œ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜๋Š” ๊ฑด์ง€์— ๋”ฐ๋ผ ๋ Œ๋”๋ง ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค.

- ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋ฅผ ๋ฐฉ๋ฌธํ•  ๋•Œ: Server์—์„œ RSC๋ž‘ JS ๋ฒˆ๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ๋ฏธ๋ฆฌ HTML์„ ๋งŒ๋“ค๊ณ  ๊ทธ๊ฑธ Client๋กœ ๋„˜๊ธฐ๊ธฐ ๋•Œ๋ฌธ์—
-> Client๋Š” JS ๋ฒˆ๋“ค์„ ๋‹ค์šด๋กœ๋“œ ํ•  ํ•„์š”์—†์ด hydration๋งŒ ํ•ด์„œ user์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋™์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

- ์ƒˆ๋กœ๊ณ ์นจํ•  ๊ฒฝ์šฐ: Client ์ธก์—์„œ JS ๋ฒˆ๋“ค์„ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋‹ค์šด๋กœ๋“œ ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ ๋‹ค์Œ, ๊ทธ ๋‹ค์Œ ๊ณผ์ •๋“ค์ด ์ด๋ฃจ์–ด์ง„๋‹ค

OK ์•Œ๊ฒ ๋Š”๋ฐ 

์•„๋‹ˆ ๊ทธ๋ž˜์„œ A -> B ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€์„œ ๋ Œ๋”๋ง ๋˜๋Š”,
์ฆ‰ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€๋กœ ๋ฐฉ๋ฌธํ•  ๋•Œ์—๋Š” ์™œ ์ € ์˜ค๋ฅ˜๊ฐ€ ์•ˆ ๋œจ๋Š” ๊ฑธ๊นŒ?

How(๊ณผ์ •) ?

์›์ธ๊ฐ€์„ค1

์ƒˆ๋กœ๊ณ ์นจ์ด ์•„๋‹Œ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ๋ฐฉ๋ฌธ ์‹œ,
์„œ๋ฒ„์—์„œ JS ๋ฒˆ๋“ค์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์€ ๋’ค Client์— ๋„˜๊ฒจ์ค€๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ,
JS ๋ฒˆ๋“ค์„ ๋ฏธ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ๋ฉด SSR ์‹œ์—๋„ ClientSupabase()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜๋ณด๋‹ค!


Why(์ด์œ ) ?

๊ด€๋ จ URL
์•„๋ž˜ ๋งํฌ๋ฅผ ๋ณด๋ฉด Next.js์—์„œ ์™œ Client ์ปดํฌ๋„ŒํŠธ๊ฐ€ SSR'd ๋˜๋Š” ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
https://github.com/reactwg/server-components/discussions/4

 

Why do Client Components get SSR'd to HTML? · reactwg server-components · Discussion #4

If you're familiar with React, your mental model before RSC might look similar to this (blank space on the left is intentional): RSC does not change that mental model, but it adds a new layer befor...

github.com

์š”์•ฝํ•˜์ž๋ฉด,
- RSC(React-Server-Component payload)๋Š”
๊ธฐ์กด CSR์„ ๊ฐˆ์•„๋ผ์šฐ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ CSR ์ „์— ํ•˜๋‚˜์˜ ๊ณผ์ •์ด "์ถ”๊ฐ€"๋˜๋Š” ๊ฒƒ์ด๊ณ ,
- RSC์—์„œ์˜ "server" ์™€ "client"๋Š” ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๊ทธ ์žก์ฑ„๋ผ๊ธฐ ๋ณด๋‹ค๋Š” React-Server์™€ React-Client๋กœ์„œ,

 
Props๋“ค(fileSystem, database, internal services.. ๋“ฑ)์€ React-Server์—์„œ -> React-Client ๋ฐฉํ–ฅ์œผ๋กœ ํ๋ฅด๊ณ 
๊ทธ ์‚ฌ์ด์—๋Š” Serialization(= ์ง๋ ฌํ™”) ๊ด€๊ณ„๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
- ๊ฒฐ๊ณผ์ ์œผ๋กœ React-Client๋Š” ๋ธŒ๋ผ์šฐ์ €์™€ ์„œ๋ฒ„, ๋‘ ํ™˜๊ฒฝ ๋ชจ๋‘์—์„œ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.
(๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” DOM ๊ด€๋ฆฌ, ๊ทธ ์™ธ ํ™˜๊ฒฝ์—์„œ๋Š” HTML ์ƒ์„ฑ)


์ถ”๋ก ์˜ what(๊ฒฐ๊ณผ) - To Be

์œ„ ๊ณผ์ •์ด A->B ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์ด๊ณ ,
JS ๋ฒˆ๋“ค์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋‹ค ๋‹ค์šด๋ฐ›์•„์•ผ ํ•˜๋Š” ์ƒˆ๋กœ๊ณ ์นจ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ,
A  -> B ํŽ˜์ด์ง€ ๋ฐฉ๋ฌธ์€
JS ๋ฒˆ๋“ค์ด ์„œ๋ฒ„ ๋‹จ์—์„œ ๋ฏธ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋œ๋‹ค๋Š” ์  +
React-Client๋Š” ์ดˆ๊ธฐ HTML ์ƒ์„ฑ์— ๊ด€์—ฌํ•˜๊ณ  ์ž‘๋™ ํ•œ๋‹ค๋Š” ์ ์—์„œ,
pre-rendering ๊ณผ์ •์—์„œ Suspense๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค๊ณ  ํ•ด๋„ clientSupabase()๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋ฐ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚˜์™”๋‹ค.

๋ฐ˜๋ฉด, JS ๋ฒˆ๋“ค์„ Client์—์„œ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ณ  ๊ทธ ๋™์•ˆ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ์ƒˆ๋กœ๊ณ ์นจ์€
pre-rendering ๊ณผ์ •์—์„œ Suspense๊ฐ€ ์‹คํ–‰๋  ๋•Œ JS ๋ฒˆ๋“ค์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— clientSupabase()๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์—†์—ˆ๊ณ 
์˜ค๋ฅ˜๋ฅผ ๋ฑ‰๋Š” ๊ฒƒ์ด๋ผ๋Š” ์ถ”๋ก ์„ ํ–ˆ๋‹ค.


๐Ÿ’ก ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ์ 

Next.js์—์„œ Client ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์™œ SSR'd ๋˜๋Š”์ง€ ๊ทธ ์ด์œ ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.