Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to initialize the theme by fetching theme (on the server side) from a database? #48

Open
animeshchat opened this issue Jun 19, 2021 · 2 comments
Labels
enhancement New feature or request v1 Planned for v1

Comments

@animeshchat
Copy link

Thanks for the great library.

My app has different themes that are stored in the user's account. How can I fetch the theme on the server side and then set the theme in next-themes? Or does this need to be set into local storage directly?

@pacocoursey
Copy link
Owner

Good question. Next-themes doesn't support this in a nice way right now, and it may not need to.

It sounds like you're server-rendering all your pages, so you can just include the theme as a body class with the initial HTML payload (getInitialProps in _document I believe). Then you don't need localStorage or the render-blocking inline script that next-themes injects.

@animeshchat
Copy link
Author

animeshchat commented Jun 25, 2021

@pacocoursey - Thanks for your reply!

My web app has a combination of server side and client side rendering, and supports multiple themes. The user may have saved theme preference, which needs to be fetched.

If no themes preferences are present for the user, the app will rely on the system defaults.
You're right - maybe I don't need to use localStorage but its useful when it comes to propagating changes across tabs/sessions.

next-themes was really useful for the above reasons. Saved me from the task of building in all that logic.

If only it could support a way to set initial theme. For eg.

**<ThemeProvider initTheme="dark" attribute="class">** 

Currently., I am fetching the preference on the server side and setting the theme (setTheme()) on the client side, which is causing the frontend to flash.

@pacocoursey pacocoursey added the enhancement New feature or request label Feb 1, 2022
@pacocoursey pacocoursey added the v1 Planned for v1 label Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v1 Planned for v1
Projects
None yet
Development

No branches or pull requests

2 participants