Skip to content

Commit 1231251

Browse files
authored
fix!: plugin-search with localization enabled (#8938)
The search plugin was incorrectly retrieving all locales, when it should just be retrieving the locale of the parent document that was actively being updated. ## BREAKING CHANGES: If you have a localized Payload config, and you are using the `plugin-search`, we will now automatically localize the `title` field that is injected by the search plugin and this may lead to data loss. To opt out of this new behavior, you can pass `localize: false` to the plugin options.
1 parent 04bd502 commit 1231251

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

docs/plugins/search.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export default config
8181

8282
The `collections` property is an array of collection slugs to enable syncing to search. Enabled collections receive a `beforeChange` and `afterDelete` hook that creates, updates, and deletes its respective search record as it changes over time.
8383

84+
### `localize`
85+
86+
By default, the search plugin will add `localization: true` to the `title` field of the newly added `search` collection if you have localization enabled. If you would like to disable this behavior, you can set this to `false`.
87+
8488
#### `defaultPriorities`
8589

8690
This plugin automatically adds a `priority` field to the `search` collection that can be used as the `?sort=` parameter in your queries. For example, you may want to list blog posts before pages. Or you may want one specific post to always take appear first.

packages/plugin-search/src/Search/hooks/syncWithSearch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
3030
docToSyncWith = await payload.findByID({
3131
id,
3232
collection,
33-
locale: 'all',
33+
locale: req.locale,
3434
req,
3535
})
3636
}
@@ -71,6 +71,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
7171
...dataToSave,
7272
priority: defaultPriority,
7373
},
74+
locale: req.locale,
7475
req,
7576
})
7677
}
@@ -82,6 +83,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
8283
const searchDocQuery = await payload.find({
8384
collection: searchSlug,
8485
depth: 0,
86+
locale: req.locale,
8587
req,
8688
where: {
8789
'doc.relationTo': {
@@ -128,6 +130,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
128130
...dataToSave,
129131
priority: foundDoc.priority || defaultPriority,
130132
},
133+
locale: req.locale,
131134
req,
132135
})
133136
} catch (err: unknown) {
@@ -154,6 +157,7 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
154157
...dataToSave,
155158
priority: defaultPriority,
156159
},
160+
locale: req.locale,
157161
req,
158162
})
159163
} catch (err: unknown) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const generateSearchCollection = (pluginConfig: SearchPluginConfig): Coll
1111
admin: {
1212
readOnly: true,
1313
},
14+
localized: pluginConfig.localize,
1415
},
1516
{
1617
name: 'priority',

packages/plugin-search/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ export const searchPlugin =
1414
(config: Config): Config => {
1515
const { collections } = config
1616

17+
// If the user defines `localize` to either true or false, use that
18+
// Otherwise, set it based on if their config has localization enabled or disabled
19+
const shouldLocalize =
20+
typeof incomingPluginConfig.localize === 'boolean'
21+
? incomingPluginConfig.localize
22+
: Boolean(config.localization)
23+
incomingPluginConfig.localize = shouldLocalize
24+
1725
if (collections) {
1826
const pluginConfig: SearchPluginConfig = {
1927
// write any config defaults here

packages/plugin-search/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type SearchPluginConfig = {
3434
[collection: string]: ((doc: any) => number | Promise<number>) | number
3535
}
3636
deleteDrafts?: boolean
37+
localize?: boolean
3738
searchOverrides?: { fields?: FieldsOverride } & Partial<Omit<CollectionConfig, 'fields'>>
3839
syncDrafts?: boolean
3940
}

test/plugin-search/int.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('@payloadcms/plugin-search', () => {
193193
const createdDoc = await payload.create({
194194
collection: 'posts',
195195
data: {
196-
_status: 'draft',
196+
_status: 'published',
197197
title: 'test title',
198198
slug: 'es',
199199
},

0 commit comments

Comments
 (0)