How to use generateMetaData or just metadata with page.js?
Answered
Somali posted this in #help-forum
SomaliOP
i have this structure (screenshot)
i cant use generateMetaData inside page or layout, cuz this not dynamic routes, but i dont want hardcode const metadata cuz i cant fetching inside it.
How to fetch seo data inside const metadata? or how to use generateMetadata inside all page.js files and dynamic routes?
i dont wanna duplicate code inside every page.js
i cant use generateMetaData inside page or layout, cuz this not dynamic routes, but i dont want hardcode const metadata cuz i cant fetching inside it.
How to fetch seo data inside const metadata? or how to use generateMetadata inside all page.js files and dynamic routes?
i dont wanna duplicate code inside every page.js
Answered by joulev
not possible with the metadata api
technically you can do that with a client component in the root layout and write your own <head> content, but i don't recommend that
technically you can do that with a client component in the root layout and write your own <head> content, but i don't recommend that
9 Replies
@Somali i have this structure (screenshot)
i cant use generateMetaData inside page or layout, cuz this not dynamic routes, but i dont want hardcode const metadata cuz i cant fetching inside it.
How to fetch seo data inside const metadata? or how to use generateMetadata inside all page.js files and dynamic routes?
i dont wanna duplicate code inside every page.js
you can generateMetadata inside non-dynamic routes
@joulev you can generateMetadata inside non-dynamic routes
SomaliOP
yes, but anyway i need duplicate call function inside all page.js , i can use it inside layout, but i cant get params to fetch correct page
i mean root layout
i just wanna create template for all pages (dynamic and not) like <NextSeo />
well i don't understand your question
but yes, you can do generateMetadata anywhere including static pages
@joulev well i don't understand your question
SomaliOP
Sorry, I'll try to explain again.
I have several pages.
Regular (For example about/page.js , contacts/page.js, etc)
And dynamic ([slug]/page.js)
I want to call generateMetaData on all these pages in one place (As if I was calling it all in the root layout)
I need it so that I don't open every file and write generateMetaData(.....) there
Perhaps everything would be simpler if I could get params.slug inside layout.js, but I don’t get it.
For example, in the pages dir I simply used <NextSeo /> at the layout.js level
I have several pages.
Regular (For example about/page.js , contacts/page.js, etc)
And dynamic ([slug]/page.js)
I want to call generateMetaData on all these pages in one place (As if I was calling it all in the root layout)
I need it so that I don't open every file and write generateMetaData(.....) there
Perhaps everything would be simpler if I could get params.slug inside layout.js, but I don’t get it.
For example, in the pages dir I simply used <NextSeo /> at the layout.js level
@Somali Sorry, I'll try to explain again.
I have several pages.
Regular (For example about/page.js , contacts/page.js, etc)
And dynamic ([slug]/page.js)
I want to call generateMetaData on all these pages in one place (As if I was calling it all in the root layout)
I need it so that I don't open every file and write generateMetaData(.....) there
Perhaps everything would be simpler if I could get params.slug inside layout.js, but I don’t get it.
For example, in the pages dir I simply used <NextSeo /> at the layout.js level
not possible with the metadata api
technically you can do that with a client component in the root layout and write your own <head> content, but i don't recommend that
technically you can do that with a client component in the root layout and write your own <head> content, but i don't recommend that
Answer