I was trying to add structured data in my nextJS 13(app router) application but couldn’t find the correct method.
Next-seo package also gives errors
I tried next-seo but got this error:
Unhandled Runtime Error
Error: Cannot read properties of null (reading ‘useContext’)
While adding
To the layout.js in the app directory
export default function RootLayout({ children }) {
return (
<html lang="en">
<head>
<NextSeo useAppDir={true} />
</head>
<body className={inter.className}>
<Navbar />
{children}
{/* <GlobalContextProvider>{children}</GlobalContextProvider> */}
<Analytics />
</body>
</html>
2
Answers
What is the error you are getting kindly mentioned but as I understand hope It will help you
In Next.js 13, you can use structured data in JSON-LD format by leveraging the Head component and the built-in next-seo package. Here’s an example of how you can implement structured data using JSON-LD in a Next.js 13 app with the app-router:
Install the required dependencies by running the following command in your project directory:
lua
Create a new file, for example, Layout.js, where you will define the layout component for your app. In this component, you can set up the structured data using the next-seo package.
jsx
In your _app.js file, import and wrap your app with the Layout component.
jsx
In your individual pages, you can set specific SEO and structured data using the NextSeo component from next-seo. For example:
jsx
By following these steps, you can add structured data in JSON-LD format to your Next.js 13 app using the next-seo package and the Head component. This approach allows you to define default SEO configurations, set up global structured data, and override them on a per-page basis as needed.
This is the official solution for inserting schema.org JSON-LD. I tested this approach and it works.