@moxy/next-seo
v1.0.0
Published
SEO for Next.js
Downloads
23
Readme
next-seo
Manage document head SEO metadata in your Next.js projects with a simple data structure.
Installation
$ npm install @moxy/next-seo
This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.
Motivation
SEO helps to improve a website’s overall searchability and visibility. Therefore, an efficient SEO is a must have in every website and there should be a good and easy way to manage all the metadata. These metadata can come from multiple sources, from static files to CMS based approaches, in completely different formats.
There are already different ways of managing all of your changes to the document head, but it can be tough to manage the possibility of duplicates. An example of a duplicate would be two meta tags defining the description of a page with different values:
<meta name="description" content="desc1">
<meta name="description" content="desc2">
Which one should a web crawler assume as the source of truth?
This library aims to solve these problems by analysing common attributes and generating an unique key to exclude duplicates. It also works with a simple data structure that is not bound to any specific origin.
Usage
import React from 'react';
import Seo from '@moxy/next-seo';
const MyPage = (props) => {
const metadata = {
title: 'MyPage Title',
meta: [
{
name: 'description',
content:'MyPage Description',
},
{
property: 'og:title',
content: 'MyPage Title',
},
],
};
return (
<>
<Seo data={ metadata } />
<div className="container">
...
</div>
</>
);
};
export default MyPage;
The example above will add to the document head:
<head>
...
<title>MyPage Title</title>
<meta name="description" content="MyPage Description" />
<meta property="og:title" content="MyPage Title" />
</head>
API
The <Seo>
component supports the following props:
data
Type: object
Required: true
The data
has the following shape:
data: PropTypes.shape({
title: PropTypes.string,
meta: PropTypes.arrayOf(PropTypes.object),
link: PropTypes.arrayOf(PropTypes.object),
}).isRequired,
The title
is a simple key-value pair that will define the page title with the title tag.
Both meta
and link
are an array of objects where each object represents a single entity (meta or link). Any key-value pair of these objects will serve as an attribute.
Example:
data: {
title: 'my title',
meta: [
{
name: 'description',
content: 'my description',
},
{
property: 'og:title',
content: 'my title',
},
{
itemprop: 'name',
content: 'my name',
},
{
'http-equiv': 'content-type',
content: '30',
},
{
foo: 'bar',
},
],
link: [
{
rel: 'icon',
href: '/favicon.ico',
},
{
rel: 'stylesheet',
href: '/styles.css',
},
],
},
};
Result:
<title>my title</title>
<meta name="description" content="my description" />
<meta property="og:title" content="my title" />
<meta itemprop="name" content="my name" />
<meta http-equiv="content-type" content="30" />
<meta foo="bar" />
<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" href="/styles.css" />
Tests
$ npm test
$ npm test -- --watch # during development
License
Released under the MIT License.