Next.js Discord

Discord Forum

How to easily have dynamic title in next.js 14.

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
For every page I want a different title.

21 Replies

are you thinking about [generateMetadata](https://nextjs.org/docs/app/api-reference/functions/generate-metadata) function?
@riský are you thinking about [`generateMetadata`](<https://nextjs.org/docs/app/api-reference/functions/generate-metadata>) function?
Northeast Congo LionOP
i tried that it shows error cause "use client"
i tried NextSEO TOO
but its showing the title of layout.js always
@riský have a read of: <https://nextjs-faq.com/metadata-client-components>
Northeast Congo LionOP
thank you
thank u
np, let me know when it works!
@riský np, let me know when it works!
Northeast Congo LionOP
this is not working <title> {blogData.title}| Everyday Echoes</title>
i want when i click a specific blog
the page that will open
will have this title
1. you should try making the server component invoking the client then
2. what do you mean by not working? you put <title>...</title> in the page body right
like how do you do get your blog pages?
@riský 1. you should try making the server component invoking the client then 2. what do you mean by not working? you put <title>...</title> in the page body right
Northeast Congo LionOP
export default function BlogTitle({ params }) {

  const [blogData, setBlogData] = useState(null);

  const router = useRouter(); // Initialize router for navigation

  // console.log("params", params.title);


  const title = params.title; // Directly use params.title

   const fetchData = async () => {

      try {

        if (!title) return; // Avoid calling the API if title is not present



        const response = await fetch(`http://localhost:3000/api/blog/${title}`);

        if (!response.ok) {

          throw new Error('Network response was not ok');

        }

        const data = await response.json();

        // console.log("data", data);




        setBlogData(data.blog || null);

        setRelatedPosts(data.relatedPosts || []);

      } catch (error) {

        setError(error.message);

      } finally {

        setLoading(false);

      }

    };

  useEffect(() => {

 




    fetchData();

  }, [title]);



        if (!response.ok) {

          throw new Error(`Network response was not ok: ${response.statusText}`);

        }


        const data = await response.json();

        setBlogData(prevData => ({

          ...prevData,

          like: data.like

        }));

      } catch (error) {

        console.error('Failed to update like count:', error);




        // Revert like count if the server request fails

        setBlogData(prevData => ({

          ...prevData,

          like: isLiked ? prevData.like + 1 : prevData.like - 1

        }));

      }

    }

  };



  if (!blogData) {

    return <div className='w-full  flex items-center justify-center my-4 '>No blog post found.</div>;

  }




  return (

    <div>

      <title>  {blogData.title}| Everyday Echoes</title>
1. this looks legit so far (i personally havent tried <title> in body but i have heard good things about it)
* when you say it doesnt work, you mean it shows prev page or the layout?
2. however i personally think you should make use of server component to do fetching, then run your client component with the data props (this way you can make use of generateMetadata and have it static)
thats why you need to make a seperate client component for your page
and then have server for the server fetching and metadata
@riský and then have server for the server fetching and metadata
Northeast Congo LionOP
can u gimmi any resource to follow
the link i sent