๐Ÿ’ง Hydration, Miss Match (Next 13)

์ด๋„๊ฒฝยท2023๋…„ 10์›” 24์ผ
0

fRONTeND

๋ชฉ๋ก ๋ณด๊ธฐ
12/14
post-thumbnail

SSR ํ™˜๊ฒฝ (Next 13)์—์„œ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚ฌ์–ด์š”.
next ๊ณต์‹ doc ๊ณผ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํ•ด๊ฒฐ๋ฒ•์„ ์ฐธ๊ณ ํ•˜์—ฌ ํ•ด๊ฒฐ ํ–ˆ์–ด์š”.

์—๋Ÿฌ ๋‚ด์šฉ์€...

(Warning: Expected server HTML to contain a matching next js)

Hydration ์ด๋ž€? (hwahae)

UseEffect๋กœ ์ดˆ๊ธฐํ™” โŒ

const [isClient, setIsClient] = useState(false);

useEffect(() => {
  setIsClient(true);
}, []);

if(!isClient) return <div/>;

return (
 ...

๊ฐ•์ œ๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ์‹คํ–‰ ๋˜๋„๋ก ํ•˜๋ผ๋Š” ์ฝ”๋“œ์ธ๋ฐ ์‚ฌ์‹ค Next์—์„œ๋Š” use client ๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฐ ๋„์›€์ด ๋˜์ง€ ๋ชปํ–ˆ์–ด์š”. ์ด๋ฏธ ํ•ด๋‹น ํŒŒ์ผ์— ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ๋„ ํ–ˆ๊ตฌ์š”.

Disable SSR โœ…โ“

import dynamic from 'next/dynamic'
 
// ์›๋ž˜ ์‚ฌ์šฉํ•˜๋ ค ํ–ˆ๋˜ ์ปดํฌ๋„ŒํŠธ import
const NoSSR = dynamic(() => import('../components/no-ssr'), { ssr: false })
 
export default function Page() {
  return (
    <div>
      <NoSSR />
    </div>
  )
}

๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด dynamic์œผ๋กœ ssr ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋น„ํ™œ์„ฑํ™” ํ•˜๊ธฐ๋กœ ํ–ˆ์–ด์š”. miss match ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ ํ–ˆ๋‹ต๋‹ˆ๋‹ค. ๐Ÿฅณ

ํ•˜์ง€๋งŒ... ๋‹ค๋ฅธ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด์š”..

(Warning: Extra attributes from the server: data-new-gr-c-s-check-loaded,data-gr-ext-installed)

๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ์—๋Ÿฌ ์›์ธ์€ ์ฐพ์•˜์–ด์š”.

grammerly ๋ผ๋Š” ํฌ๋กฌ ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•ด์š”..
๋‹ต๋ณ€๋“ค์„ ์ฐธ๊ณ ํ•˜์—ฌ suppressHydrationWarning=true ์˜ ๊ฐ’์„ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์˜ต์…˜ ์ถ”๊ฐ€๋ฅผ ํ•˜์—ฌ ํ•ด๊ฒฐ์€ ํ•˜์˜€์–ด์š”!
๊ทธ๋Ÿฐ๋ฐ Hydration์„ ๋ฌด์‹œํ•ด๋ฒ„๋ฆฌ๋ฉด ์ตœ์ ํ™”์— ์ข‹์ง€์•Š๋‹ค๋Š” ์˜๊ฒฌ์ด ์žˆ์–ด ์ฉ ์ข‹์ง„ ์•Š๋„ค์š”. ๐Ÿ˜’

๋ฌธ์ œ ํšŒํ”ผ โœ…

๊ตฌ๊ธ€๋ง ์ค‘ ๋กœ์ปฌ์—์„œ๋งŒ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ์˜๊ฒฌ๋“ค์ด ์žˆ์—ˆ์–ด์š”. ๋ฐฐํฌ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด๋‹ˆ ์‚ฌ์‹ค์ด๋”๋ผ๊ตฌ์š”.
๊ทธ๋ž˜์„œ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋“œ์— ๋”ฐ๋ผ ๊ฐ’์„ ์กฐ์ •ํ•˜๊ธฐ๋กœ ํ–ˆ์–ด์š”.

<body suppressHydrationWarning={process.env.NODE_ENV === 'development'}>
  ...

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋นŒ๋“œ ๋ชจ๋“œ์— ๋”ฐ๋ผ suppressHydrationWarning ๊ฐ’์ด ๋“ค์–ด๊ฐ€์š”.
์ด๋ ‡๊ฒŒ ์—๋Ÿฌ๋Š” ๊ต์ •ํ•˜๋ฉด์„œ ์ œ๊ฐ€ ์›ํ•˜๋˜ ์ตœ์ ํ™” ๋ฌธ์ œ๋„ ์–ด๋Š์ •๋„๋Š” ํ•ด๊ฒฐํ•œ๋“ฏ ํ•˜๋„ค์š”.

profile
์•ˆ๋…•ํ•˜์„ธ์šฉ

0๊ฐœ์˜ ๋Œ“๊ธ€