Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/seo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SEO Metadata

SnapWP exports two main functions to add meta tags for SEO:

- `getLayoutMetadata`: Gets SEO data for the site.
- `getPageMetadata`: Gets SEO data related to the template. Adds update icon

## Registering a new plugin

The SEO functionality can be extended by registering new plugins. Below is an example of updated root layout.

```typescript
import { RootLayout } from '@snapwp/next';
import { getLayoutMetadata, Seo } from '@snapwp/next/seo';
import type { Metadata } from 'next';
import type { PropsWithChildren } from 'react';

// Register a plugin before calling function `getLayoutMetadata`
Seo.registerPlugin(
{
fragment: PluginFragmentDoc,
parseMetadata: parserFunction,
location:'page'
}
);

export default function Layout( { children }: PropsWithChildren ) {
return (
<RootLayout>
<>{ children }</>
</RootLayout>
);
}

/**
* Generate site meta data.
* @return Metadata for SEO.
*/
export async function generateMetadata(): Promise< Metadata > {
const metadata = await getLayoutMetadata();

return {
...metadata,
};
}
```

## Anatomy of a SEO plugin

An plugin requires 3 things to be registered.

- **Fragment** to define the data needed from the WP server.
- **Parser** to parse the fragment data to be consumable by NextJS.
- **Location** has value of either `page` or `layout`. This defines if the parsed meta data should be added to the returned value of `getPageMetadata` or `getLayoutMetadata`.
Comment on lines +52 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should provide the actual shape, if not an example of these. See the level of fidelity in query-engine.md


> [!Note]
> If a plugin is defined to have the location `page` the fragment should be on `RootQuery` and for `layout` it should be a fragment of `RenderedTemplate`.