I want to use an array that is on another page. I need the array information for the title and meta tags, but it seems that in next js it takes time to load the information, so I encounter undefined at first. What solution should I use to load this array?
(I can’t put the array in the same file because I need it in some other pages as well.)
I also tried get static props but it returns undefined as props.
Should I use swr? What is the best solution for seo?
import { items } from "../../public/assets/ArticlesList.js";
const ArticlePage = () => {
const router = useRouter();
const { id } = router.query;
let data = items[id];
return (
<>
<Head>
<title> {data.title} </title>
</Head>
<div>
....
</div>
</>
)
}
2
Answers
I recommend using getServerSideProps
Make a common component like below: and import on top of the page. Then you won’t have any problem of undefined data.