How can we set the page title/metadata differently depending on client state?
Answered
English Shepherd posted this in #help-forum
English ShepherdOP
Hello all,
We're using the new app router and attempting to use the new
Side note: just to be certain, the
We're using the new app router and attempting to use the new
generateMetadata function/Metadata API to set the page title for SEO purposes. For static public pages, this is fine -- we're making a call to our graphql server and getting the page title via the ID. However, for private pages, we'd like to only display the title if the user is authenticated and has access. We're currently using auth.js but this applies to any auth system we might use (we're considering switching due to unrelated limitations/suboptimal DX with that library) -- how do we use current client state in the generateMetadata function in general? Another way to phrase this: how do we set the page title for client components (the next step we'll need after this anyway)?Side note: just to be certain, the
generateMetadata function is only exposed server-side, right? So we can use server-side secrets safely?Answered by James4u
Hey @English Shepherd So just have normal title and description for private pages when they are pre-rendered on the server side, right?
and after user authentication, you can change title
Those new metadata after user authentication won't affect the SEO
and after user authentication, you can change title
window.title = "new title"Those new metadata after user authentication won't affect the SEO
7 Replies
Hey @English Shepherd So just have normal title and description for private pages when they are pre-rendered on the server side, right?
and after user authentication, you can change title
Those new metadata after user authentication won't affect the SEO
and after user authentication, you can change title
window.title = "new title"Those new metadata after user authentication won't affect the SEO
Answer
@James4u Hey <@365809874468208640> So just have normal title and description for private pages when they are pre-rendered on the server side, right?
and after user authentication, you can change title `window.title = "new title"`
Those new metadata after user authentication won't affect the SEO
English ShepherdOP
ooh okay that's perfect thank you! didn't realize I could still just set it like that
yeah, as it's after user authentication, it doesn't need to be on the server side, and it won't work for SEO.
that's why you can simply use
window.titlemark solution if it works for your 🙂
@James4u mark solution if it works for your 🙂
English ShepherdOP
sorry how do I do that?
just close the post?