From cdaebcc79c2608d95626652d044d04c065a0d29a Mon Sep 17 00:00:00 2001 From: Said Akhrarov <36972061+akhrarovsaid@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:08:58 -0500 Subject: [PATCH] fix(plugin-search): respect custom api route in reindexButton (#10258) ### 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! --- packages/plugin-search/src/Search/index.ts | 2 ++ .../src/Search/ui/ReindexButton/index.client.tsx | 7 +++++-- .../plugin-search/src/Search/ui/ReindexButton/index.tsx | 3 ++- .../plugin-search/src/Search/ui/ReindexButton/types.ts | 1 + packages/plugin-search/src/index.ts | 1 + packages/plugin-search/src/types.ts | 1 + 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/plugin-search/src/Search/index.ts b/packages/plugin-search/src/Search/index.ts index 2135f91364..47769c17d8 100644 --- a/packages/plugin-search/src/Search/index.ts +++ b/packages/plugin-search/src/Search/index.ts @@ -8,6 +8,7 @@ import { generateReindexHandler } from '../utilities/generateReindexHandler.js' export const generateSearchCollection = ( pluginConfig: SearchPluginConfigWithLocales, ): CollectionConfig => { + const apiBasePath = pluginConfig?.apiBasePath || '/api' const searchSlug = pluginConfig?.searchOverrides?.slug || 'search' const searchCollections = pluginConfig?.collections || [] const collectionLabels = pluginConfig?.labels @@ -70,6 +71,7 @@ export const generateSearchCollection = ( { path: '@payloadcms/plugin-search/client#ReindexButton', serverProps: { + apiBasePath, collectionLabels, searchCollections, searchSlug, diff --git a/packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx b/packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx index 27fa42ec16..5d6c22368a 100644 --- a/packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx +++ b/packages/plugin-search/src/Search/ui/ReindexButton/index.client.tsx @@ -20,6 +20,7 @@ import { ReindexConfirmModal } from './ReindexConfirmModal/index.js' const confirmReindexModalSlug = 'confirm-reindex-modal' export const ReindexButtonClient: React.FC = ({ + apiBasePath, collectionLabels, searchCollections, searchSlug, @@ -45,8 +46,10 @@ export const ReindexButtonClient: React.FC = ({ closeConfirmModal() setLoading(true) + const basePath = apiBasePath.endsWith('/') ? apiBasePath.slice(0, -1) : apiBasePath + try { - const endpointRes = await fetch(`/api/${searchSlug}/reindex?locale=${locale.code}`, { + const endpointRes = await fetch(`${basePath}/${searchSlug}/reindex?locale=${locale.code}`, { body: JSON.stringify({ collections: reindexCollections, }), @@ -67,7 +70,7 @@ export const ReindexButtonClient: React.FC = ({ setReindexCollections([]) setLoading(false) } - }, [closeConfirmModal, isLoading, reindexCollections, router, searchSlug, locale]) + }, [closeConfirmModal, isLoading, reindexCollections, router, searchSlug, locale, apiBasePath]) const handleShowConfirmModal = useCallback( (collections: string | string[] = searchCollections) => { diff --git a/packages/plugin-search/src/Search/ui/ReindexButton/index.tsx b/packages/plugin-search/src/Search/ui/ReindexButton/index.tsx index fbc341a75c..05d465a7b7 100644 --- a/packages/plugin-search/src/Search/ui/ReindexButton/index.tsx +++ b/packages/plugin-search/src/Search/ui/ReindexButton/index.tsx @@ -3,7 +3,7 @@ import type { SearchReindexButtonServerComponent } from './types.js' import { ReindexButtonClient } from './index.client.js' export const ReindexButton: SearchReindexButtonServerComponent = (props) => { - const { collectionLabels, i18n, searchCollections, searchSlug } = props + const { apiBasePath, collectionLabels, i18n, searchCollections, searchSlug } = props const getStaticLocalizedPluralLabels = () => { return Object.fromEntries( @@ -26,6 +26,7 @@ export const ReindexButton: SearchReindexButtonServerComponent = (props) => { return ( searchCollections: string[] searchSlug: string diff --git a/packages/plugin-search/src/index.ts b/packages/plugin-search/src/index.ts index a43939d1c6..6c7db8a282 100644 --- a/packages/plugin-search/src/index.ts +++ b/packages/plugin-search/src/index.ts @@ -37,6 +37,7 @@ export const searchPlugin = const pluginConfig: SearchPluginConfigWithLocales = { // write any config defaults here + apiBasePath: config.routes?.api, deleteDrafts: true, labels, locales, diff --git a/packages/plugin-search/src/types.ts b/packages/plugin-search/src/types.ts index 733145a2bc..0e1e6185f4 100644 --- a/packages/plugin-search/src/types.ts +++ b/packages/plugin-search/src/types.ts @@ -31,6 +31,7 @@ export type BeforeSync = (args: { export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[] export type SearchPluginConfig = { + apiBasePath?: string beforeSync?: BeforeSync collections?: string[] defaultPriorities?: {