Unverified Commit 15915c34 authored by Zach Jaensch's avatar Zach Jaensch Committed by GitHub

Add pagination SEO link tags (#74737)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

-->

Apologies in advance if I have missed something in the contributing
process.

Fixes #52925


### What?

Adds metadata config option for pagination link tags.

### Why?

The `<link rel="prev"` and `<link rel="next"` are used by search
crawlers and can help with SEO performance.
For more information:
https://developers.google.com/search/blog/2011/09/pagination-with-relnext-and-relprev

### How?

Uses existing patterns for metadata config.
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent c8251c30
...@@ -55,6 +55,10 @@ export function createDefaultMetadata(): ResolvedMetadata { ...@@ -55,6 +55,10 @@ export function createDefaultMetadata(): ResolvedMetadata {
bookmarks: null, bookmarks: null,
category: null, category: null,
classification: null, classification: null,
pagination: {
previous: null,
next: null,
},
other: {}, other: {},
} }
} }
...@@ -99,6 +99,16 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { ...@@ -99,6 +99,16 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) {
<link rel="bookmarks" href={bookmark} /> <link rel="bookmarks" href={bookmark} />
)) ))
: []), : []),
...(metadata.pagination
? [
metadata.pagination.previous ? (
<link rel="prev" href={metadata.pagination.previous} />
) : null,
metadata.pagination.next ? (
<link rel="next" href={metadata.pagination.next} />
) : null,
]
: []),
Meta({ name: 'category', content: metadata.category }), Meta({ name: 'category', content: metadata.category }),
Meta({ name: 'classification', content: metadata.classification }), Meta({ name: 'classification', content: metadata.classification }),
...(metadata.other ...(metadata.other
......
...@@ -46,6 +46,7 @@ import { ...@@ -46,6 +46,7 @@ import {
resolveVerification, resolveVerification,
resolveItunes, resolveItunes,
resolveFacebook, resolveFacebook,
resolvePagination,
} from './resolvers/resolve-basics' } from './resolvers/resolve-basics'
import { resolveIcons } from './resolvers/resolve-icons' import { resolveIcons } from './resolvers/resolve-icons'
import { getTracer } from '../../server/lib/trace/tracer' import { getTracer } from '../../server/lib/trace/tracer'
...@@ -252,6 +253,14 @@ function mergeMetadata({ ...@@ -252,6 +253,14 @@ function mergeMetadata({
) )
break break
} }
case 'pagination': {
target.pagination = resolvePagination(
source.pagination,
metadataBase,
metadataContext
)
break
}
// directly assign fields that fallback to null // directly assign fields that fallback to null
case 'applicationName': case 'applicationName':
case 'description': case 'description':
......
...@@ -263,3 +263,17 @@ export const resolveFacebook: FieldResolver<'facebook'> = (facebook) => { ...@@ -263,3 +263,17 @@ export const resolveFacebook: FieldResolver<'facebook'> = (facebook) => {
admins: resolveAsArrayOrUndefined(facebook.admins), admins: resolveAsArrayOrUndefined(facebook.admins),
} }
} }
export const resolvePagination: FieldResolverExtraArgs<
'pagination',
[ResolvedMetadata['metadataBase'], MetadataContext]
> = (pagination, metadataBase, context) => {
return {
previous: pagination?.previous
? resolveAlternateUrl(pagination.previous, metadataBase, context)
: null,
next: pagination?.next
? resolveAlternateUrl(pagination.next, metadataBase, context)
: null,
}
}
...@@ -454,6 +454,22 @@ interface Metadata extends DeprecatedMetadataFields { ...@@ -454,6 +454,22 @@ interface Metadata extends DeprecatedMetadataFields {
*/ */
bookmarks?: null | string | Array<string> | undefined // This is technically against HTML spec but is used in wild bookmarks?: null | string | Array<string> | undefined // This is technically against HTML spec but is used in wild
/**
* The pagination link rel properties.
*
* @see https://developers.google.com/search/blog/2011/09/pagination-with-relnext-and-relprev
* @example
* ```tsx
* "https://example.com/items"
* <link rel="prev" href="https://example.com/items?page=1" />
* <link rel="next" href="https://example.com/items?page=3" />
* ```
*/
pagination?: {
previous?: null | string | URL | undefined
next?: null | string | URL | undefined
}
// meta name properties // meta name properties
/** /**
...@@ -566,6 +582,10 @@ interface ResolvedMetadata extends DeprecatedMetadataFields { ...@@ -566,6 +582,10 @@ interface ResolvedMetadata extends DeprecatedMetadataFields {
archives: null | Array<string> archives: null | Array<string>
assets: null | Array<string> assets: null | Array<string>
bookmarks: null | Array<string> // This is technically against HTML spec but is used in wild bookmarks: null | Array<string> // This is technically against HTML spec but is used in wild
pagination: {
previous: null | string
next: null | string
}
// meta name properties // meta name properties
category: null | string category: null | string
......
...@@ -28,6 +28,10 @@ export const metadata: Metadata = { ...@@ -28,6 +28,10 @@ export const metadata: Metadata = {
manifest: '/api/manifest', manifest: '/api/manifest',
robots: 'index, follow', robots: 'index, follow',
alternates: {}, alternates: {},
pagination: {
previous: '/basic?page=1',
next: '/basic?page=3',
},
formatDetection: { formatDetection: {
email: false, email: false,
address: false, address: false,
......
...@@ -29,6 +29,10 @@ export const metadata: Metadata = { ...@@ -29,6 +29,10 @@ export const metadata: Metadata = {
publisher: 'vercel', publisher: 'vercel',
robots: 'index, follow', robots: 'index, follow',
alternates: {}, alternates: {},
pagination: {
previous: '/basic?page=1',
next: '/basic?page=3',
},
formatDetection: { formatDetection: {
email: false, email: false,
address: false, address: false,
......
...@@ -82,6 +82,8 @@ describe('app dir - metadata', () => { ...@@ -82,6 +82,8 @@ describe('app dir - metadata', () => {
preconnect: '/preconnect-url', preconnect: '/preconnect-url',
preload: '/api/preload', preload: '/api/preload',
'dns-prefetch': '/dns-prefetch-url', 'dns-prefetch': '/dns-prefetch-url',
prev: '/basic?page=1',
next: '/basic?page=3',
}) })
// Manifest link should have crossOrigin attribute // Manifest link should have crossOrigin attribute
...@@ -117,6 +119,8 @@ describe('app dir - metadata', () => { ...@@ -117,6 +119,8 @@ describe('app dir - metadata', () => {
preconnect: '/preconnect-url', preconnect: '/preconnect-url',
preload: '/api/preload', preload: '/api/preload',
'dns-prefetch': '/dns-prefetch-url', 'dns-prefetch': '/dns-prefetch-url',
prev: '/basic?page=1',
next: '/basic?page=3',
}) })
// Manifest link should have crossOrigin attribute // Manifest link should have crossOrigin attribute
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment