javascript - use an array from another js file in nextjs page
Solution:
I recommend using getServerSideProps
import { items } from "../../public/assets/ArticlesList.js";
const ArticlePage = ({ data }) => {
return (
<>
<Head>
<title> {data.title} </title>
</Head>
<div>
....
</div>
</>
);
}
export async function getServerSideProps(context) {
const { id } = context.query;
const data = items[id];
return {
props: { data }
}
}
export default ArticlePage;
Solution:
Make a common component like below: and import on top of the page. Then you won't have any problem of undefined data.
import Head from 'next/head';
const MetaData = (props) => {
const title = props.title || '';
const description = props.description || '';
const keywords = props.keywords || '';
const image = props.image || '';
return (
<Head>
<title>{title}</title>
<meta name="description" content={description} />
<meta name="og:description" content={description} />
<meta name="twitter:description" content={description} />
<meta name="twitter:site" content="" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content={title} />
<meta name="og:site_name" content="" />
<meta name="og:title" content={title} />
<meta name="keywords" content={keywords} />
<meta name="og:image" content={image} />
</Head>
);
};
export default MetaData;
SEO Tools
DA Rank
Domain Authority by MOZ. Find out domain authority for free. Site analysis, seo audit.
DA RankJoomla theme detector
With supporting text below as a natural lead-in to additional content.
Joomla theme detectorWP theme detector
Find out online which WordPress template is installed on the site.
WP theme detectorXML sitemap validator
Check online if the sitemap was made correctly XML for Google.
XML sitemap validator