-
Notifications
You must be signed in to change notification settings - Fork 12
chore: add usage docs for seo #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ayushnirwal
wants to merge
6
commits into
rtCamp:develop
Choose a base branch
from
ayushnirwal:docs/seo
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4dfcb9a
chore: add usage docs for seo
ayushnirwal 94b8d47
chore: format
ayushnirwal 8d60bc4
chore: format
justlevine 7e0e7e4
Merge branch 'develop' into docs/seo
justlevine 9f86249
Merge branch 'develop' into docs/seo
justlevine 8167546
Merge branch 'develop' into docs/seo
justlevine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.