Change Head tags on the client (like meta tags)
Answered
Southeastern blueberry bee posted this in #help-forum
Southeastern blueberry beeOP
I have a theme context that switches the theme on runtime with a button, i wanted to update the meta tag
theme-color, how can i update it? i've seen only how to add static metadata so far using the export const metadata but not how to change this at runtime on the clientAnswered by Southeastern blueberry bee
yeah that's what i was searching but it doesn't seem to be possible, except the way u mentioned might work by chainging the layout to be a client component but i'm not even sure if that works
33 Replies
@Clown https://nextjs.org/docs/app/building-your-application/optimizing/metadata#dynamic-metadata
Southeastern blueberry beeOP
that is server side, i need it client side
I dont think you should be using metadata tags tbh
Atleast not for this
Is there some specific reason you are doing this?
@Clown Is there some specific reason you are doing this?
Southeastern blueberry beeOP
i've described the issue in the description, users decide to change the theme, so i want to change the
this is some code i used in another project that has pages router
theme-color meta tag propriety <Head>
<meta name="theme-color" content={theme.getColor('primary').toString()}/>
</Head>this is some code i used in another project that has pages router
Then just do it in the root layout
Southeastern blueberry beeOP
i mean i can just do dom access but would be better if there was a native way
@Clown Then just do it in the root layout
Southeastern blueberry beeOP
yeah but it won't change at runtime in the client, only server side
Im pretty sure there isnt any other way
Southeastern blueberry beeOP
i already have it in the root layout currently
but it's server only
This should just work
@Southeastern blueberry bee i've described the issue in the description, users decide to change the theme, so i want to change the `theme-color` meta tag propriety
ts
<Head>
<meta name="theme-color" content={theme.getColor('primary').toString()}/>
</Head>
this is some code i used in another project that has pages router
That theme variable is from a context right?
Southeastern blueberry beeOP
yup
Then just do the same in app router. It will work. Make the layout a client component
Southeastern blueberry beeOP
ah
And no the children wont be client components too
Southeastern blueberry beeOP
nono i know
just that i need to do server logic in that layout
Like?
Southeastern blueberry beeOP
fetching the user and pre populating a context, and in case invalidate the first token
anyway, it's not worth to go that route, i'll just access dom at this point
Move those to server actions or route handlers then
Its fine to use route handlers with client component
Also any specific reason to not use next-theme?
@Clown Also any specific reason to not use next-theme?
Southeastern blueberry beeOP
i have my own logic that creates layered colors from some basic theme colors, manages fonts, some other utilities and css variables
@Southeastern blueberry bee i've described the issue in the description, users decide to change the theme, so i want to change the `theme-color` meta tag propriety
ts
<Head>
<meta name="theme-color" content={theme.getColor('primary').toString()}/>
</Head>
this is some code i used in another project that has pages router
Welp, i dont think next natively provides what you want. You'll just have to go with the app router equivalent of this or something.
@Clown Welp, i dont think next natively provides what you want. You'll just have to go with the app router equivalent of this or something.
Southeastern blueberry beeOP
yeah that's what i was searching but it doesn't seem to be possible, except the way u mentioned might work by chainging the layout to be a client component but i'm not even sure if that works
Answer
Southeastern blueberry beeOP
i'll just access the dom, it's fine
i dont think i'm gonna need to do manual head edits anymore except this
@Southeastern blueberry bee yeah that's what i was searching but it doesn't seem to be possible, except the way u mentioned might work by chainging the layout to be a client component but i'm not even sure if that works
I mean if you are worried about refactoring the code, just create a client component with whatever meta tag and react magic you want, then wrap your layout with it.