0

I have a problem with set client cookies when I do request from server side NextJS 13.4

I want to do request to backend on server side component and set cookies that were returned from backend.

When I use client side component and do request from client then cookies are set on browser.

For example:

"use client";

import { useEffect } from "react";

export default function Home() {
  useEffect(() => {
    fetch("http://domain.loc/api/mirror?q=sss2", {
      next: { revalidate: 0 },
      method: "GET",
      credentials: "include",
      referrer: "http://domain.loc",
      headers: {
        "Content-Type": "application/json",
      },
    }).then();
  }, []);

  return <div>some page</div>;
}

But if I use Server Side Component, cookies are not set on browser.

For example:

export default async function Home() {
  const res = await fetch("http://domain.loc/api/mirror?q=sss2", {
    next: { revalidate: 0 },
    method: "GET",
    credentials: "include",
    referrer: "http://domain.loc",
    headers: {
      "Content-Type": "application/json",
    },
  });

  const data = await res.json();

  return (
    <div>
      DATA - {JSON.stringify(data)}
    </div>
  );
}
Jonas
  • 121,568
  • 97
  • 310
  • 388

0 Answers0