Next.js Discord

Discord Forum

Set class to body on first render SSR [Page router]

Unanswered
Lhasa Apso posted this in #help-forum
Open in Discord
Lhasa ApsoOP
Is it possible with Next to set the class attribute on first render? Let's say the siteTheme variable will be provided from getServerSideProps so I'll get this value from server.

Can I set this value to a body on first render instead of this current solution where I set it on client (in useEffect). This solution causes small flash before class gets applied on the client.

Code:
 useEffect(() => {
    document.body.classList.add(siteTheme);
  }, [siteTheme]);

export const getServerSideProps = () => {
  return { props: { siteTheme: "red" } };
}

0 Replies