Blog

Twoja wymarzona praca? Lets Git IT.
Interaktywna platforma przygotowująca do rozmów technicznych dla nowoczesnych programistów.

XGitHub

Platforma

  • Kategorie

Zasoby

  • Blog
  • O aplikacji
  • FAQ
  • Sugestie

Prawne

  • Polityka prywatności
  • Regulamin

© 2025 LetsGit.IT. Wszelkie prawa zastrzeżone.

LetsGit.IT/Kategorie/Next.js
Next.jsmedium

Jak odczytać cookies lub headers w Server Component w Next.js?

Tagi
#cookies#headers#server-components
Wróć do kategoriiPrzejdź do quizu

Odpowiedź

Użyj `cookies()` i `headers()` z `next/headers` w Server Component. Czytają one dane z requestu po stronie serwera; w przeglądarce cookies czyta się inaczej (a headerów nie masz w ten sam sposób).

import { cookies, headers } from 'next/headers'

export default function Page() {
  const cookieStore = cookies()
  const theme = cookieStore.get('theme')?.value
  const ua = headers().get('user-agent')
  return <div>{theme} {ua}</div>
}

Powiązane pytania

Next.js
Server Components vs Client Components: kiedy wybrać które?
#nextjs#server-components#client-components
Next.js
Biblioteka tylko w przeglądarce w App Router: jak użyć jej bez psucia SSR/Server Components?
#nextjs#server-components#client-components
Next.js
Auth w Next.js App Router: jak zrobić go bezpiecznie i SSR-friendly?
#nextjs#auth
#cookies
Next.js
Renderowanie statyczne vs dynamiczne — jak wymusić dynamiczne i po co?
#ssr#dynamic#cache
Next.js
Server Components vs Client Components w Next.js App Router?
#app-router#server-components#client-components