Skip to content

Commit cdaebcc

Browse files
authored
fix(plugin-search): respect custom api route in reindexButton (#10258)
<!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> ### What? This PR fixes an issue with `plugin-search` where the ReindexButton was not directing the reindex call to the correct endpoint location if the user defined a custom config api route. ### Why? To allow collection reindexing even when the default base api path gets overriden with a user-provided one. ### How? By threading the custom route to the ReindexButton component that calls the reindex endpoint. Fixes #10245 Notes: - I think the `basePath` check/manipulation might be better in the RSC instead of the client Edit: @JessChowdhury Didn't see you were assigned until after! Felt bad about this since it's my bad, wanted to take some ownership over the bug here, my mistake!
1 parent 5b4730d commit cdaebcc

File tree

6 files changed

+12
-3
lines changed

6 files changed

+12
-3
lines changed

packages/plugin-search/src/Search/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { generateReindexHandler } from '../utilities/generateReindexHandler.js'
88
export const generateSearchCollection = (
99
pluginConfig: SearchPluginConfigWithLocales,
1010
): CollectionConfig => {
11+
const apiBasePath = pluginConfig?.apiBasePath || '/api'
1112
const searchSlug = pluginConfig?.searchOverrides?.slug || 'search'
1213
const searchCollections = pluginConfig?.collections || []
1314
const collectionLabels = pluginConfig?.labels
@@ -70,6 +71,7 @@ export const generateSearchCollection = (
7071
{
7172
path: '@payloadcms/plugin-search/client#ReindexButton',
7273
serverProps: {
74+
apiBasePath,
7375
collectionLabels,
7476
searchCollections,
7577
searchSlug,

packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ReindexConfirmModal } from './ReindexConfirmModal/index.js'
2020
const confirmReindexModalSlug = 'confirm-reindex-modal'
2121

2222
export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
23+
apiBasePath,
2324
collectionLabels,
2425
searchCollections,
2526
searchSlug,
@@ -45,8 +46,10 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
4546
closeConfirmModal()
4647
setLoading(true)
4748

49+
const basePath = apiBasePath.endsWith('/') ? apiBasePath.slice(0, -1) : apiBasePath
50+
4851
try {
49-
const endpointRes = await fetch(`/api/${searchSlug}/reindex?locale=${locale.code}`, {
52+
const endpointRes = await fetch(`${basePath}/${searchSlug}/reindex?locale=${locale.code}`, {
5053
body: JSON.stringify({
5154
collections: reindexCollections,
5255
}),
@@ -67,7 +70,7 @@ export const ReindexButtonClient: React.FC<ReindexButtonProps> = ({
6770
setReindexCollections([])
6871
setLoading(false)
6972
}
70-
}, [closeConfirmModal, isLoading, reindexCollections, router, searchSlug, locale])
73+
}, [closeConfirmModal, isLoading, reindexCollections, router, searchSlug, locale, apiBasePath])
7174

7275
const handleShowConfirmModal = useCallback(
7376
(collections: string | string[] = searchCollections) => {

packages/plugin-search/src/Search/ui/ReindexButton/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { SearchReindexButtonServerComponent } from './types.js'
33
import { ReindexButtonClient } from './index.client.js'
44

55
export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
6-
const { collectionLabels, i18n, searchCollections, searchSlug } = props
6+
const { apiBasePath, collectionLabels, i18n, searchCollections, searchSlug } = props
77

88
const getStaticLocalizedPluralLabels = () => {
99
return Object.fromEntries(
@@ -26,6 +26,7 @@ export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
2626

2727
return (
2828
<ReindexButtonClient
29+
apiBasePath={apiBasePath}
2930
collectionLabels={getStaticLocalizedPluralLabels()}
3031
searchCollections={searchCollections}
3132
searchSlug={searchSlug}

packages/plugin-search/src/Search/ui/ReindexButton/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CustomComponent, PayloadServerReactComponent, StaticLabel } from '
33
import type { CollectionLabels } from '../../../types.js'
44

55
export type ReindexButtonProps = {
6+
apiBasePath: string
67
collectionLabels: Record<string, StaticLabel>
78
searchCollections: string[]
89
searchSlug: string

packages/plugin-search/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const searchPlugin =
3737

3838
const pluginConfig: SearchPluginConfigWithLocales = {
3939
// write any config defaults here
40+
apiBasePath: config.routes?.api,
4041
deleteDrafts: true,
4142
labels,
4243
locales,

packages/plugin-search/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type BeforeSync = (args: {
3131
export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[]
3232

3333
export type SearchPluginConfig = {
34+
apiBasePath?: string
3435
beforeSync?: BeforeSync
3536
collections?: string[]
3637
defaultPriorities?: {

0 commit comments

Comments
 (0)