Skip to content

Commit c29e1f0

Browse files
authored
fix(plugin-search): add locale to key in syncedDocsSet (#14289)
<!-- 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 adjusts the `docKey` used in `syncDocAsSearchIndex` to consider configured locales. The existing behavior prevents reindexing from saving locales outside of the default locale. With this PR, the plugin successfully reindexes all configured locales. ### Why? To allow all configured locales to be reindexed as expected. ### How? - Checking if the pluginConfig includes locales and, if so, sets the `docKey` to include the `syncLocale` being used - Correcting type for `pluginConfig` in `SyncWithSearchArgs` to be `SanitizedSearchPluginConfig` - Adding an int test to demonstrate reindexing all configured locales as default behavior Fixes #14276 Before: [before.mp4](https://private-user-images.githubusercontent.com/20878653/503591671-d7d9ccbf-7e9d-4d98-bbc4-0e5729bb2505.mp4?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjEwNTk4NjMsIm5iZiI6MTc2MTA1OTU2MywicGF0aCI6Ii8yMDg3ODY1My81MDM1OTE2NzEtZDdkOWNjYmYtN2U5ZC00ZDk4LWJiYzQtMGU1NzI5YmIyNTA1Lm1wND9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTEwMjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUxMDIxVDE1MTI0M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTlmZGYxYTZmNDBlMTJhMDlkZGQ4ZTg3ZGU4Njk1ZThiZDk0ODc3MzllNTE1YTA3NDgyNGE5YWJmZmVmYWI4ZTcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.93C6OWecb34l8JeAtAc2AdOFh6aVoIDfcSxJ247FO1k) After: [Search-Results---Payload--after.webm](https://github.com/user-attachments/assets/57bb689d-4cfb-40f0-be8d-08c6787c7bec)
1 parent ad0e7b2 commit c29e1f0

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

packages/plugin-search/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type SanitizedSearchPluginConfig = {
8484

8585
export type SyncWithSearchArgs = {
8686
collection: string
87-
pluginConfig: SearchPluginConfig
87+
pluginConfig: SanitizedSearchPluginConfig
8888
} & Omit<Parameters<CollectionAfterChangeHook>[0], 'collection'>
8989

9090
export type SyncDocArgs = {

packages/plugin-search/src/utilities/syncDocAsSearchIndex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const syncDocAsSearchIndex = async ({
2424
},
2525
title,
2626
}
27-
const docKey = `${collection}:${id}`
27+
const docKeyPrefix = `${collection}:${id}`
28+
const docKey = pluginConfig.locales?.length ? `${docKeyPrefix}:${syncLocale}` : docKeyPrefix
2829
const syncedDocsSet = (req.context?.syncedDocsSet as Set<string>) || new Set<string>()
2930

3031
if (syncedDocsSet.has(docKey)) {

test/plugin-search/int.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,88 @@ describe('@payloadcms/plugin-search', () => {
589589
)
590590
})
591591

592+
it('should reindex all configured locales', async () => {
593+
const post = await payload.create({
594+
collection: postsSlug,
595+
locale: 'en',
596+
data: {
597+
title: 'Test page published',
598+
_status: 'published',
599+
slug: 'test-en',
600+
},
601+
})
602+
await payload.update({
603+
collection: postsSlug,
604+
id: post.id,
605+
locale: 'es',
606+
data: {
607+
_status: 'published',
608+
slug: 'test-es',
609+
},
610+
})
611+
await payload.update({
612+
collection: postsSlug,
613+
id: post.id,
614+
locale: 'de',
615+
data: {
616+
_status: 'published',
617+
slug: 'test-de',
618+
},
619+
})
620+
621+
const {
622+
docs: [postBeforeReindex],
623+
} = await payload.find({
624+
collection: 'search',
625+
locale: 'all',
626+
where: {
627+
doc: {
628+
equals: {
629+
value: post.id,
630+
relationTo: postsSlug,
631+
},
632+
},
633+
},
634+
pagination: false,
635+
limit: 1,
636+
depth: 0,
637+
})
638+
639+
expect(postBeforeReindex?.slug).not.toBeFalsy()
640+
641+
const endpointRes = await restClient.POST(`/search/reindex`, {
642+
body: JSON.stringify({
643+
collections: [postsSlug],
644+
}),
645+
headers: {
646+
Authorization: `JWT ${token}`,
647+
},
648+
})
649+
650+
expect(endpointRes.status).toBe(200)
651+
652+
const {
653+
docs: [postAfterReindex],
654+
} = await payload.find({
655+
collection: 'search',
656+
locale: 'all',
657+
where: {
658+
doc: {
659+
equals: {
660+
value: post.id,
661+
relationTo: postsSlug,
662+
},
663+
},
664+
},
665+
pagination: false,
666+
limit: 1,
667+
depth: 0,
668+
})
669+
670+
expect(postAfterReindex?.slug).not.toBeFalsy()
671+
expect(postAfterReindex?.slug).toStrictEqual(postBeforeReindex?.slug)
672+
})
673+
592674
it('should sync trashed documents correctly with search plugin', async () => {
593675
// Create a published post
594676
const publishedPost = await payload.create({

0 commit comments

Comments
 (0)