Skip to content

Commit 157b1e1

Browse files
authored
fix(plugin-seo): now respects serverURL and api routes configuration (#8546)
1 parent a735f40 commit 157b1e1

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

packages/plugin-seo/src/fields/MetaDescription/MetaDescriptionComponent.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { TextareaFieldClientProps } from 'payload'
66
import {
77
FieldLabel,
88
TextareaInput,
9+
useConfig,
910
useDocumentInfo,
1011
useField,
1112
useFieldProps,
@@ -41,6 +42,13 @@ export const MetaDescriptionComponent: React.FC<MetaDescriptionProps> = (props)
4142
} = props
4243
const { path: pathFromContext } = useFieldProps()
4344

45+
const {
46+
config: {
47+
routes: { api },
48+
serverURL,
49+
},
50+
} = useConfig()
51+
4452
const { t } = useTranslation<PluginSEOTranslations, PluginSEOTranslationKeys>()
4553

4654
const locale = useLocale()
@@ -58,7 +66,9 @@ export const MetaDescriptionComponent: React.FC<MetaDescriptionProps> = (props)
5866
return
5967
}
6068

61-
const genDescriptionResponse = await fetch('/api/plugin-seo/generate-description', {
69+
const endpoint = `${serverURL}${api}/plugin-seo/generate-description`
70+
71+
const genDescriptionResponse = await fetch(endpoint, {
6272
body: JSON.stringify({
6373
id: docInfo.id,
6474
collectionSlug: docInfo.collectionSlug,
@@ -85,7 +95,23 @@ export const MetaDescriptionComponent: React.FC<MetaDescriptionProps> = (props)
8595
const { result: generatedDescription } = await genDescriptionResponse.json()
8696

8797
setValue(generatedDescription || '')
88-
}, [hasGenerateDescriptionFn, docInfo, getData, locale, setValue])
98+
}, [
99+
hasGenerateDescriptionFn,
100+
serverURL,
101+
api,
102+
docInfo.id,
103+
docInfo.collectionSlug,
104+
docInfo.docPermissions,
105+
docInfo.globalSlug,
106+
docInfo.hasPublishPermission,
107+
docInfo.hasSavePermission,
108+
docInfo.initialData,
109+
docInfo.initialState,
110+
docInfo.title,
111+
getData,
112+
locale,
113+
setValue,
114+
])
89115

90116
return (
91117
<div

packages/plugin-seo/src/fields/MetaImage/MetaImageComponent.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export const MetaImageComponent: React.FC<MetaImageProps> = (props) => {
4040
} = props || {}
4141
const { path: pathFromContext } = useFieldProps()
4242

43+
const {
44+
config: {
45+
collections,
46+
routes: { api },
47+
serverURL,
48+
},
49+
} = useConfig()
50+
4351
const field: FieldType<string> = useField({ ...props, path: pathFromContext } as Options)
4452

4553
const { t } = useTranslation<PluginSEOTranslations, PluginSEOTranslationKeys>()
@@ -55,7 +63,9 @@ export const MetaImageComponent: React.FC<MetaImageProps> = (props) => {
5563
return
5664
}
5765

58-
const genImageResponse = await fetch('/api/plugin-seo/generate-image', {
66+
const endpoint = `${serverURL}${api}/plugin-seo/generate-image`
67+
68+
const genImageResponse = await fetch(endpoint, {
5969
body: JSON.stringify({
6070
id: docInfo.id,
6171
collectionSlug: docInfo.collectionSlug,
@@ -79,14 +89,26 @@ export const MetaImageComponent: React.FC<MetaImageProps> = (props) => {
7989
const generatedImage = await genImageResponse.text()
8090

8191
setValue(generatedImage || '')
82-
}, [hasGenerateImageFn, docInfo, getData, locale, setValue])
92+
}, [
93+
hasGenerateImageFn,
94+
serverURL,
95+
api,
96+
docInfo.id,
97+
docInfo.collectionSlug,
98+
docInfo.docPermissions,
99+
docInfo.globalSlug,
100+
docInfo.hasPublishPermission,
101+
docInfo.hasSavePermission,
102+
docInfo.initialData,
103+
docInfo.initialState,
104+
docInfo.title,
105+
getData,
106+
locale,
107+
setValue,
108+
])
83109

84110
const hasImage = Boolean(value)
85111

86-
const { config } = useConfig()
87-
88-
const { collections, routes: { api } = {}, serverURL } = config
89-
90112
const collection = collections?.find((coll) => coll.slug === relationTo) || undefined
91113

92114
return (

packages/plugin-seo/src/fields/MetaTitle/MetaTitleComponent.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { TextFieldClientProps } from 'payload'
66
import {
77
FieldLabel,
88
TextInput,
9+
useConfig,
910
useDocumentInfo,
1011
useField,
1112
useFieldProps,
@@ -44,6 +45,13 @@ export const MetaTitleComponent: React.FC<MetaTitleProps> = (props) => {
4445
const { path: pathFromContext } = useFieldProps()
4546
const { t } = useTranslation<PluginSEOTranslations, PluginSEOTranslationKeys>()
4647

48+
const {
49+
config: {
50+
routes: { api },
51+
serverURL,
52+
},
53+
} = useConfig()
54+
4755
const field: FieldType<string> = useField({
4856
path: pathFromContext,
4957
} as Options)
@@ -59,7 +67,9 @@ export const MetaTitleComponent: React.FC<MetaTitleProps> = (props) => {
5967
return
6068
}
6169

62-
const genTitleResponse = await fetch('/api/plugin-seo/generate-title', {
70+
const endpoint = `${serverURL}${api}/plugin-seo/generate-title`
71+
72+
const genTitleResponse = await fetch(endpoint, {
6373
body: JSON.stringify({
6474
id: docInfo.id,
6575
collectionSlug: docInfo.collectionSlug,
@@ -83,7 +93,23 @@ export const MetaTitleComponent: React.FC<MetaTitleProps> = (props) => {
8393
const { result: generatedTitle } = await genTitleResponse.json()
8494

8595
setValue(generatedTitle || '')
86-
}, [hasGenerateTitleFn, docInfo, getData, locale, setValue])
96+
}, [
97+
hasGenerateTitleFn,
98+
serverURL,
99+
api,
100+
docInfo.id,
101+
docInfo.collectionSlug,
102+
docInfo.docPermissions,
103+
docInfo.globalSlug,
104+
docInfo.hasPublishPermission,
105+
docInfo.hasSavePermission,
106+
docInfo.initialData,
107+
docInfo.initialState,
108+
docInfo.title,
109+
getData,
110+
locale,
111+
setValue,
112+
])
87113

88114
return (
89115
<div

packages/plugin-seo/src/fields/Preview/PreviewComponent.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { FormField, UIField } from 'payload'
44

55
import {
66
useAllFormFields,
7+
useConfig,
78
useDocumentInfo,
89
useForm,
910
useLocale,
@@ -29,6 +30,13 @@ export const PreviewComponent: React.FC<PreviewProps> = (props) => {
2930

3031
const { t } = useTranslation<PluginSEOTranslations, PluginSEOTranslationKeys>()
3132

33+
const {
34+
config: {
35+
routes: { api },
36+
serverURL,
37+
},
38+
} = useConfig()
39+
3240
const locale = useLocale()
3341
const [fields] = useAllFormFields()
3442
const { getData } = useForm()
@@ -45,8 +53,10 @@ export const PreviewComponent: React.FC<PreviewProps> = (props) => {
4553
const [href, setHref] = useState<string>()
4654

4755
useEffect(() => {
56+
const endpoint = `${serverURL}${api}/plugin-seo/generate-url`
57+
4858
const getHref = async () => {
49-
const genURLResponse = await fetch('/api/plugin-seo/generate-url', {
59+
const genURLResponse = await fetch(endpoint, {
5060
body: JSON.stringify({
5161
id: docInfo.id,
5262
collectionSlug: docInfo.collectionSlug,
@@ -75,7 +85,7 @@ export const PreviewComponent: React.FC<PreviewProps> = (props) => {
7585
if (hasGenerateURLFn && !href) {
7686
void getHref()
7787
}
78-
}, [fields, href, locale, docInfo, hasGenerateURLFn, getData])
88+
}, [fields, href, locale, docInfo, hasGenerateURLFn, getData, serverURL, api])
7989

8090
return (
8191
<div

0 commit comments

Comments
 (0)