Next.js Discord

Discord Forum

Best way to handle theming in my layout?

Unanswered
Atlantic salmon posted this in #help-forum
Open in Discord
Atlantic salmonOP
I'm writing a website for hosting competitions. Both users and competitions can have their own themes, and the site uses the following rules to figure out which one to use:

- /comp/[cid]: use the comp theme
- /user/[uid]: use uid's user theme
- /...: anywhere else, use the currently authenticated user's theme

To do this, I've got a really complicated middleware that sets a header based on the URL, and then my main layout makes a database call to fetch the theme depending on the value of the header. This is not only really ugly, but also not very performant.

What would be a better approach to this? I'm using Radix UI's theming if that makes any difference/

5 Replies

i'm curious if you found a better and more performant solution
Atlantic salmonOP
I have not, that's why I'm here...
yea well now i will get notifications : D
Atlantic salmonOP
So I was able to get my load times marginally better by moving the responsibility of managing theme over to the client. It keeps the user's theme cached, grabbing it from a server action when needed. This isn't the best approach in my eyes because it relies on a useEffect which means the site blinks. If anyone has any better ideas, please do let me know.
Spectacled bear
You can try useLayoutEffect if blinking is the issue