Adding dynamic MetaData
Answered
Sun bear posted this in #help-forum
Sun bearOP
Hello guys, hope u are doing great, I'm curious about smth.
How can I add the name of my car to the title of my page ( Metadata title ) ?
I'm kinda new to these types of things in nextJS and I would really like to have your help.
Exemple of code :
How can I add the name of my car to the title of my page ( Metadata title ) ?
I'm kinda new to these types of things in nextJS and I would really like to have your help.
Exemple of code :
import React from "react";
import { fetchCar } from "/api/Cars/fetchingDataOfCar";
import type { Metadata } from "next";
export const metadata: Metadata = {
title: "Car Details",
description: "Generated by create next app",
};
async function Cars({ params }: any) {
const car = await fetchCar(params.id);
return <>{car.name}</>;
}
export default Cars;Answered by B33fb0n3
you can do it like this:
See more here: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
import { Metadata, ResolvingMetadata } from 'next'
type Props = {
params: { id: string }
searchParams: { [key: string]: string | string[] | undefined }
}
export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
// read route params
const id = params.id
// fetch data
const product = await fetch(`https://.../${id}`).then((res) => res.json())
return {
title: product.title,
}
}
export default function Page({ params, searchParams }: Props) {
// do other stuff
}See more here: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
12 Replies
@Sun bear Hello guys, hope u are doing great, I'm curious about smth.
How can I add the name of my car to the title of my page ( Metadata title ) ?
I'm kinda new to these types of things in nextJS and I would really like to have your help.
Exemple of code :
tsx
import React from "react";
import { fetchCar } from "/api/Cars/fetchingDataOfCar";
import type { Metadata } from "next";
export const metadata: Metadata = {
title: "Car Details",
description: "Generated by create next app",
};
async function Cars({ params }: any) {
const car = await fetchCar(params.id);
return <>{car.name}</>;
}
export default Cars;
you can do it like this:
See more here: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
import { Metadata, ResolvingMetadata } from 'next'
type Props = {
params: { id: string }
searchParams: { [key: string]: string | string[] | undefined }
}
export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
// read route params
const id = params.id
// fetch data
const product = await fetch(`https://.../${id}`).then((res) => res.json())
return {
title: product.title,
}
}
export default function Page({ params, searchParams }: Props) {
// do other stuff
}See more here: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
Answer
@B33fb0n3 you can do it like this:
tsx
import { Metadata, ResolvingMetadata } from 'next'
type Props = {
params: { id: string }
searchParams: { [key: string]: string | string[] | undefined }
}
export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
// read route params
const id = params.id
// fetch data
const product = await fetch(`https://.../${id}`).then((res) => res.json())
return {
title: product.title,
}
}
export default function Page({ params, searchParams }: Props) {
// do other stuff
}
See more here: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
Sun bearOP
Ok so i tried and it worked ( for a mini exemple )
But now, when I try to use generateMetaData in a child component, it doesn't work anymore :
Parent component :
But now, when I try to use generateMetaData in a child component, it doesn't work anymore :
Parent component :
import React from 'react'
import Ex from '../(components)/Ex'
function page() {
return (
<div>
<Ex/>
</div>
)
}
export default pageChild component :
import React from "react";
import { Metadata, ResolvingMetadata } from "next";
export async function generateMetadata(
{ params, searchParams }: any,
parent: ResolvingMetadata
): Promise<Metadata> {
const exempleResponse = await fetch(
"https://jsonplaceholder.typicode.com/posts"
);
const exemple = await exempleResponse.json();
return {
title: exemple[0].title, // par exemple, prendre le titre du premier post
};
}
async function Ex() {
const exempleResponse = await fetch(
"https://jsonplaceholder.typicode.com/posts"
);
const exemples = await exempleResponse.json();
return <div>Exemple de titre : {exemples[0].title}</div>;
}
export default Ex;Do you know how can i make it works when it is used in a child component ?
it doesn't work anymoreCan you clarify?
@Sun bear Ok so i tried and it worked ( for a mini exemple )
But now, when I try to use generateMetaData in a child component, it doesn't work anymore :
Parent component :
tsx
import React from 'react'
import Ex from '../(components)/Ex'
function page() {
return (
<div>
<Ex/>
</div>
)
}
export default page
put generateMetadata in parent component
@B33fb0n3 > it doesn't work anymore
Can you clarify?
Sun bearOP
I had a page.tsx where I put your code ( modified ) and my title was rendered dynamicaly , then i tried to create a child component to see what it would do if I put this generateMetadata in my child component, and it doesn't worked anymore.
So I guess I must put this generateMetadata in my parent component.
So I guess I must put this generateMetadata in my parent component.
Thanks for your advices it helped me a lot ! @ᴉuɐpɹɐɐ @B33fb0n3
generateMetadata works since its exported in a file-convention like page.js and layout.js
cant just use it everywhere
@ᴉuɐpɹɐɐ cant just use it everywhere
Sun bearOP
oh nice didn't know it thanks ;)