feat(plugin-seo): pass req through to generate functions (#7711)

Closes https://github.com/payloadcms/payload/issues/7708
This commit is contained in:
Paul
2024-08-15 17:09:08 -06:00
committed by GitHub
parent b7d01dec70
commit 5eee49da9a
7 changed files with 33 additions and 23 deletions

View File

@@ -119,7 +119,7 @@ A function that allows you to return any meta title, including from document's c
{
// ...
seoPlugin({
generateTitle: ({ ...docInfo, doc, locale }) => `Website.com — ${doc?.title}`,
generateTitle: ({ ...docInfo, doc, locale, req }) => `Website.com — ${doc?.title}`,
})
}
```
@@ -133,7 +133,7 @@ A function that allows you to return any meta description, including from docume
{
// ...
seoPlugin({
generateDescription: ({ ...docInfo, doc, locale }) => doc?.excerpt,
generateDescription: ({ ...docInfo, doc, locale, req }) => doc?.excerpt,
})
}
```
@@ -147,7 +147,7 @@ A function that allows you to return any meta image, including from document's c
{
// ...
seoPlugin({
generateImage: ({ ...docInfo, doc, locale }) => doc?.featuredImage,
generateImage: ({ ...docInfo, doc, locale, req }) => doc?.featuredImage,
})
}
```
@@ -161,7 +161,7 @@ A function called by the search preview component to display the actual URL of y
{
// ...
seoPlugin({
generateURL: ({ ...docInfo, doc, locale }) =>
generateURL: ({ ...docInfo, doc, locale, req }) =>
`https://yoursite.com/${collection?.slug}/${doc?.slug}`,
})
}

View File

@@ -61,7 +61,7 @@ export const MetaDescriptionComponent: React.FC<MetaDescriptionProps> = (props)
...docInfo,
doc: { ...getData() },
locale: typeof locale === 'object' ? locale?.code : locale,
} satisfies Parameters<GenerateDescription>[0]),
} satisfies Omit<Parameters<GenerateDescription>[0], 'req'>),
credentials: 'include',
headers: {
'Content-Type': 'application/json',

View File

@@ -57,7 +57,7 @@ export const MetaImageComponent: React.FC<MetaImageProps> = (props) => {
...docInfo,
doc: { ...getData() },
locale: typeof locale === 'object' ? locale?.code : locale,
} satisfies Parameters<GenerateImage>[0]),
} satisfies Omit<Parameters<GenerateImage>[0], 'req'>),
credentials: 'include',
headers: {
'Content-Type': 'application/json',

View File

@@ -62,7 +62,7 @@ export const MetaTitleComponent: React.FC<MetaTitleProps> = (props) => {
...docInfo,
doc: { ...getData() },
locale: typeof locale === 'object' ? locale?.code : locale,
} satisfies Parameters<GenerateTitle>[0]),
} satisfies Omit<Parameters<GenerateTitle>[0], 'req'>),
credentials: 'include',
headers: {
'Content-Type': 'application/json',

View File

@@ -49,7 +49,7 @@ export const PreviewComponent: React.FC<PreviewProps> = ({
...docInfo,
doc: { ...getData() },
locale: typeof locale === 'object' ? locale?.code : locale,
} satisfies Parameters<GenerateURL>[0]),
} satisfies Omit<Parameters<GenerateURL>[0], 'req'>),
credentials: 'include',
headers: {
'Content-Type': 'application/json',

View File

@@ -134,11 +134,12 @@ export const seoPlugin =
{
handler: async (req) => {
await addDataAndFileToRequest(req)
req.t
const result = pluginConfig.generateTitle
? await pluginConfig.generateTitle(
req.data as unknown as Parameters<GenerateTitle>[0],
)
? await pluginConfig.generateTitle({
...req.data,
req,
} as unknown as Parameters<GenerateTitle>[0])
: ''
return new Response(JSON.stringify({ result }), { status: 200 })
},
@@ -148,10 +149,12 @@ export const seoPlugin =
{
handler: async (req) => {
await addDataAndFileToRequest(req)
const result = pluginConfig.generateDescription
? await pluginConfig.generateDescription(
req.data as unknown as Parameters<GenerateDescription>[0],
)
? await pluginConfig.generateDescription({
...req.data,
req,
} as unknown as Parameters<GenerateDescription>[0])
: ''
return new Response(JSON.stringify({ result }), { status: 200 })
},
@@ -161,8 +164,12 @@ export const seoPlugin =
{
handler: async (req) => {
await addDataAndFileToRequest(req)
const result = pluginConfig.generateURL
? await pluginConfig.generateURL(req.data as unknown as Parameters<GenerateURL>[0])
? await pluginConfig.generateURL({
...req.data,
req,
} as unknown as Parameters<GenerateURL>[0])
: ''
return new Response(JSON.stringify({ result }), { status: 200 })
},
@@ -172,10 +179,12 @@ export const seoPlugin =
{
handler: async (req) => {
await addDataAndFileToRequest(req)
const result = pluginConfig.generateImage
? await pluginConfig.generateImage(
req.data as unknown as Parameters<GenerateImage>[0],
)
? await pluginConfig.generateImage({
...req.data,
req,
} as unknown as Parameters<GenerateImage>[0])
: ''
return new Response(result, { status: 200 })
},

View File

@@ -1,23 +1,24 @@
import type { DocumentInfoContext } from '@payloadcms/ui'
import type { Field, TextField, TextareaField, UploadField } from 'payload'
import type { Field, PayloadRequest, TextField, TextareaField, UploadField } from 'payload'
export type GenerateTitle<T = any> = (
args: { doc: T; locale?: string } & DocumentInfoContext,
args: { doc: T; locale?: string; req: PayloadRequest } & DocumentInfoContext,
) => Promise<string> | string
export type GenerateDescription<T = any> = (
args: {
doc: T
locale?: string
req: PayloadRequest
} & DocumentInfoContext,
) => Promise<string> | string
export type GenerateImage<T = any> = (
args: { doc: T; locale?: string } & DocumentInfoContext,
args: { doc: T; locale?: string; req: PayloadRequest } & DocumentInfoContext,
) => Promise<string> | string
export type GenerateURL<T = any> = (
args: { doc: T; locale?: string } & DocumentInfoContext,
args: { doc: T; locale?: string; req: PayloadRequest } & DocumentInfoContext,
) => Promise<string> | string
export type SEOPluginConfig = {