@faststore/core 4.11.4-dev.3 → 4.11.4-dev.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-generate.log +3 -3
- package/.turbo/turbo-test.log +181 -167
- package/CHANGELOG.md +12 -0
- package/cms/faststore/components/cms_component__navbar.jsonc +11 -1
- package/cms/faststore/schema.json +11 -1
- package/cms/faststore/sections.json +11 -1
- package/package.json +6 -6
- package/src/components/navigation/Navbar/Navbar.tsx +4 -0
- package/src/components/search/SearchDropdown/SearchDropdown.tsx +11 -2
- package/src/components/search/SearchHistory/SearchHistory.tsx +12 -2
- package/src/components/search/SearchInput/SearchInput.tsx +12 -0
- package/src/components/sections/Navbar/Navbar.tsx +2 -0
- package/src/server/cms/ResilientClientCMS.ts +168 -0
- package/src/server/cms/index.ts +3 -2
- package/test/components/search/SearchDropdown.test.tsx +80 -0
- package/test/components/search/SearchInputComponent.test.tsx +71 -4
- package/test/server/cms/ResilientClientCMS.test.ts +482 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.11.4-dev.5](https://github.com/vtex/faststore/compare/v4.11.4-dev.4...v4.11.4-dev.5) (2026-11-19)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **core:** retry CMS fetch bypassing cache on invalid JSON ([#3590](https://github.com/vtex/faststore/issues/3590)) ([12f9062](https://github.com/vtex/faststore/commit/12f9062184eef948f29d2aeac0308c97300d842f))
|
|
11
|
+
|
|
12
|
+
## [4.11.4-dev.4](https://github.com/vtex/faststore/compare/v4.11.4-dev.3...v4.11.4-dev.4) (2026-11-19)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
- **core:** forward search dropdown labels from the CMS ([#3600](https://github.com/vtex/faststore/issues/3600)) ([0f18b72](https://github.com/vtex/faststore/commit/0f18b729eb90e7053b823ceb90207967d9d383f8)), closes [#3334](https://github.com/vtex/faststore/issues/3334) [#3334](https://github.com/vtex/faststore/issues/3334) [#3240](https://github.com/vtex/faststore/issues/3240) [#3240](https://github.com/vtex/faststore/issues/3240)
|
|
17
|
+
|
|
6
18
|
## [4.11.4-dev.3](https://github.com/vtex/faststore/compare/v4.11.4-dev.2...v4.11.4-dev.3) (2026-11-18)
|
|
7
19
|
|
|
8
20
|
### Bug Fixes
|
|
@@ -93,7 +93,17 @@
|
|
|
93
93
|
"searchTopTitle": {
|
|
94
94
|
"title": "Top Search Title",
|
|
95
95
|
"type": "string",
|
|
96
|
-
"default": "Top
|
|
96
|
+
"default": "Top Search"
|
|
97
|
+
},
|
|
98
|
+
"searchHistoryClearLabel": {
|
|
99
|
+
"title": "Clear Search History Button Label",
|
|
100
|
+
"type": "string",
|
|
101
|
+
"default": "Clear History"
|
|
102
|
+
},
|
|
103
|
+
"searchProductsTitle": {
|
|
104
|
+
"title": "Suggested Products Title",
|
|
105
|
+
"type": "string",
|
|
106
|
+
"default": "Suggested Products"
|
|
97
107
|
},
|
|
98
108
|
"quickOrderSettings": {
|
|
99
109
|
"title": "Quick Order settings",
|
|
@@ -2064,7 +2064,17 @@
|
|
|
2064
2064
|
"searchTopTitle": {
|
|
2065
2065
|
"title": "Top Search Title",
|
|
2066
2066
|
"type": "string",
|
|
2067
|
-
"default": "Top
|
|
2067
|
+
"default": "Top Search"
|
|
2068
|
+
},
|
|
2069
|
+
"searchHistoryClearLabel": {
|
|
2070
|
+
"title": "Clear Search History Button Label",
|
|
2071
|
+
"type": "string",
|
|
2072
|
+
"default": "Clear History"
|
|
2073
|
+
},
|
|
2074
|
+
"searchProductsTitle": {
|
|
2075
|
+
"title": "Suggested Products Title",
|
|
2076
|
+
"type": "string",
|
|
2077
|
+
"default": "Suggested Products"
|
|
2068
2078
|
},
|
|
2069
2079
|
"quickOrderSettings": {
|
|
2070
2080
|
"title": "Quick Order settings",
|
|
@@ -235,7 +235,17 @@
|
|
|
235
235
|
"searchTopTitle": {
|
|
236
236
|
"title": "Top Search Title",
|
|
237
237
|
"type": "string",
|
|
238
|
-
"default": "Top
|
|
238
|
+
"default": "Top Search"
|
|
239
|
+
},
|
|
240
|
+
"searchHistoryClearLabel": {
|
|
241
|
+
"title": "Clear Search History Button Label",
|
|
242
|
+
"type": "string",
|
|
243
|
+
"default": "Clear History"
|
|
244
|
+
},
|
|
245
|
+
"searchProductsTitle": {
|
|
246
|
+
"title": "Suggested Products Title",
|
|
247
|
+
"type": "string",
|
|
248
|
+
"default": "Suggested Products"
|
|
239
249
|
},
|
|
240
250
|
"quickOrderSettings": {
|
|
241
251
|
"title": "Quick Order settings",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/core",
|
|
3
|
-
"version": "4.11.4-dev.
|
|
3
|
+
"version": "4.11.4-dev.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -70,11 +70,11 @@
|
|
|
70
70
|
"style-loader": "^3.3.1",
|
|
71
71
|
"swr": "^2.2.5",
|
|
72
72
|
"use-sync-external-store": "^1.6.0",
|
|
73
|
-
"@faststore/api": "4.11.4-dev.
|
|
74
|
-
"@faststore/
|
|
75
|
-
"@faststore/
|
|
76
|
-
"@faststore/
|
|
77
|
-
"@faststore/
|
|
73
|
+
"@faststore/api": "4.11.4-dev.5",
|
|
74
|
+
"@faststore/diagnostics": "4.11.4-dev.5",
|
|
75
|
+
"@faststore/sdk": "4.11.4-dev.5",
|
|
76
|
+
"@faststore/lighthouse": "4.11.4-dev.5",
|
|
77
|
+
"@faststore/ui": "4.11.4-dev.5"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@cypress/code-coverage": "^3.12.1",
|
|
@@ -185,6 +185,8 @@ function Navbar({
|
|
|
185
185
|
loadingLabel={searchInput?.loadingLabel}
|
|
186
186
|
searchHistoryTitle={searchInput?.searchHistoryTitle}
|
|
187
187
|
searchTopTitle={searchInput?.searchTopTitle}
|
|
188
|
+
searchHistoryClearLabel={searchInput?.searchHistoryClearLabel}
|
|
189
|
+
searchProductsTitle={searchInput?.searchProductsTitle}
|
|
188
190
|
/>
|
|
189
191
|
)}
|
|
190
192
|
|
|
@@ -222,6 +224,8 @@ function Navbar({
|
|
|
222
224
|
loadingLabel={searchInput?.loadingLabel}
|
|
223
225
|
searchHistoryTitle={searchInput?.searchHistoryTitle}
|
|
224
226
|
searchTopTitle={searchInput?.searchTopTitle}
|
|
227
|
+
searchHistoryClearLabel={searchInput?.searchHistoryClearLabel}
|
|
228
|
+
searchProductsTitle={searchInput?.searchProductsTitle}
|
|
225
229
|
/>
|
|
226
230
|
)}
|
|
227
231
|
|
|
@@ -24,7 +24,9 @@ interface SearchDropdownProps {
|
|
|
24
24
|
sort: SearchState['sort']
|
|
25
25
|
quickOrderSettings?: NavbarProps['searchInput']['quickOrderSettings']
|
|
26
26
|
searchHistoryTitle?: string
|
|
27
|
+
searchHistoryClearLabel?: string
|
|
27
28
|
searchTopTitle?: string
|
|
29
|
+
searchProductsTitle?: string
|
|
28
30
|
[key: string]: any
|
|
29
31
|
onChangeCustomSearchDropdownVisible?: Dispatch<SetStateAction<boolean>>
|
|
30
32
|
}
|
|
@@ -47,10 +49,12 @@ function SearchDropdown({
|
|
|
47
49
|
sort,
|
|
48
50
|
quickOrderSettings,
|
|
49
51
|
searchHistoryTitle,
|
|
52
|
+
searchHistoryClearLabel,
|
|
50
53
|
searchTopTitle,
|
|
54
|
+
searchProductsTitle,
|
|
51
55
|
onChangeCustomSearchDropdownVisible,
|
|
52
56
|
...otherProps
|
|
53
|
-
}: SearchDropdownProps) {
|
|
57
|
+
}: Readonly<SearchDropdownProps>) {
|
|
54
58
|
const {
|
|
55
59
|
values: { onSearchSelection, products, term, terms, searchId },
|
|
56
60
|
} = useSearch()
|
|
@@ -58,7 +62,11 @@ function SearchDropdown({
|
|
|
58
62
|
|
|
59
63
|
return (
|
|
60
64
|
<UISearchDropdown {...otherProps}>
|
|
61
|
-
<SearchHistory
|
|
65
|
+
<SearchHistory
|
|
66
|
+
title={searchHistoryTitle}
|
|
67
|
+
clearLabel={searchHistoryClearLabel}
|
|
68
|
+
sort={sort}
|
|
69
|
+
/>
|
|
62
70
|
<SearchTop title={searchTopTitle} sort={sort} />
|
|
63
71
|
<UISearchAutoComplete>
|
|
64
72
|
{terms?.map(({ value: suggestion }) => (
|
|
@@ -95,6 +103,7 @@ function SearchDropdown({
|
|
|
95
103
|
))}
|
|
96
104
|
</UISearchAutoComplete>
|
|
97
105
|
<SearchProducts
|
|
106
|
+
title={searchProductsTitle}
|
|
98
107
|
data-af-element={searchId && 'search-autocomplete'}
|
|
99
108
|
data-af-onimpression={!!searchId}
|
|
100
109
|
data-af-search-id={searchId}
|
|
@@ -7,10 +7,15 @@ import useSearchHistory from 'src/sdk/search/useSearchHistory'
|
|
|
7
7
|
|
|
8
8
|
interface SearchHistoryProps {
|
|
9
9
|
title?: string
|
|
10
|
+
clearLabel?: string
|
|
10
11
|
[key: string]: unknown
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
const SearchHistory = ({
|
|
14
|
+
const SearchHistory = ({
|
|
15
|
+
title,
|
|
16
|
+
clearLabel,
|
|
17
|
+
...props
|
|
18
|
+
}: Readonly<SearchHistoryProps>) => {
|
|
14
19
|
const {
|
|
15
20
|
values: { onSearchSelection },
|
|
16
21
|
} = useSearch()
|
|
@@ -21,7 +26,12 @@ const SearchHistory = ({ title, ...props }: SearchHistoryProps) => {
|
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
return (
|
|
24
|
-
<UISearchHistory
|
|
29
|
+
<UISearchHistory
|
|
30
|
+
title={title}
|
|
31
|
+
clearLabel={clearLabel}
|
|
32
|
+
onClear={clearSearchHistory}
|
|
33
|
+
{...props}
|
|
34
|
+
>
|
|
25
35
|
{searchHistory.map((item) => (
|
|
26
36
|
<UISearchHistoryTerm
|
|
27
37
|
key={item.term}
|
|
@@ -73,7 +73,9 @@ export type SearchInputProps = {
|
|
|
73
73
|
submitButtonAriaLabel?: string
|
|
74
74
|
loadingLabel?: string
|
|
75
75
|
searchHistoryTitle?: string
|
|
76
|
+
searchHistoryClearLabel?: string
|
|
76
77
|
searchTopTitle?: string
|
|
78
|
+
searchProductsTitle?: string
|
|
77
79
|
} & Omit<UISearchInputFieldProps, 'onSubmit' | 'attachmentButtonIcon'>
|
|
78
80
|
|
|
79
81
|
export type SearchInputRef = UISearchInputFieldRef & {
|
|
@@ -114,6 +116,11 @@ const SearchInput = forwardRef<SearchInputRef, SearchInputProps>(
|
|
|
114
116
|
sort,
|
|
115
117
|
placeholder,
|
|
116
118
|
quickOrderSettings,
|
|
119
|
+
loadingLabel,
|
|
120
|
+
searchHistoryTitle,
|
|
121
|
+
searchHistoryClearLabel,
|
|
122
|
+
searchTopTitle,
|
|
123
|
+
searchProductsTitle,
|
|
117
124
|
...otherProps
|
|
118
125
|
},
|
|
119
126
|
ref
|
|
@@ -390,6 +397,11 @@ const SearchInput = forwardRef<SearchInputRef, SearchInputProps>(
|
|
|
390
397
|
onChangeCustomSearchDropdownVisible={
|
|
391
398
|
setCustomSearchDropdownVisibleCondition
|
|
392
399
|
}
|
|
400
|
+
loadingLabel={loadingLabel}
|
|
401
|
+
searchHistoryTitle={searchHistoryTitle}
|
|
402
|
+
searchHistoryClearLabel={searchHistoryClearLabel}
|
|
403
|
+
searchTopTitle={searchTopTitle}
|
|
404
|
+
searchProductsTitle={searchProductsTitle}
|
|
393
405
|
/>
|
|
394
406
|
</Suspense>
|
|
395
407
|
)}
|
|
@@ -25,7 +25,9 @@ export interface NavbarProps {
|
|
|
25
25
|
sort: string
|
|
26
26
|
submitButtonAriaLabel?: string
|
|
27
27
|
searchHistoryTitle?: string
|
|
28
|
+
searchHistoryClearLabel?: string
|
|
28
29
|
searchTopTitle?: string
|
|
30
|
+
searchProductsTitle?: string
|
|
29
31
|
collapseSearchAriaLabel?: string
|
|
30
32
|
quickOrderSettings?: {
|
|
31
33
|
quickOrder: boolean
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import ClientCMS from '@vtex/client-cms'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Query param added to the retry request. The CDN in front of the CMS API
|
|
5
|
+
* includes the query string in its cache key, so a new value is a cache miss
|
|
6
|
+
* that reaches the origin. A `Cache-Control: no-cache` request header is not
|
|
7
|
+
* enough, since CDNs commonly ignore it.
|
|
8
|
+
*/
|
|
9
|
+
export const CACHE_BUST_PARAM = 'cb'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The cache-busting value changes once per window instead of per request, so
|
|
13
|
+
* retries for the same URL within a window share one CDN cache entry: at most
|
|
14
|
+
* one of them reaches the origin, instead of every page generation causing a
|
|
15
|
+
* new cache miss while the bad response stays cached under the original URL.
|
|
16
|
+
*/
|
|
17
|
+
export const CACHE_BUST_WINDOW_MS = 60 * 1000
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How long a URL whose cache-busting retry also failed skips further retries.
|
|
21
|
+
*
|
|
22
|
+
* Must be at least `CACHE_BUST_WINDOW_MS`: the next retry after the cooldown
|
|
23
|
+
* has to fall in a later window, so it uses a new cache-busting value instead
|
|
24
|
+
* of the one whose cached response just failed. Derived from it to keep that
|
|
25
|
+
* invariant when either value changes.
|
|
26
|
+
*/
|
|
27
|
+
export const RETRY_COOLDOWN_MS = CACHE_BUST_WINDOW_MS
|
|
28
|
+
|
|
29
|
+
type ErrorShape = {
|
|
30
|
+
name?: unknown
|
|
31
|
+
type?: unknown
|
|
32
|
+
cause?: { code?: unknown }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Error codes Node raises while decompressing a response body: zlib (gzip,
|
|
37
|
+
* deflate) uses `Z_*` (e.g. `Z_DATA_ERROR`, `Z_BUF_ERROR`), and brotli uses
|
|
38
|
+
* `ERR_` + the decoder error name, which itself starts with an underscore (e.g.
|
|
39
|
+
* `ERR__ERROR_FORMAT_PADDING_1`). The double underscore is not a typo.
|
|
40
|
+
*/
|
|
41
|
+
const BODY_DECODING_ERROR_CODE = /^(Z_|ERR__ERROR_)/
|
|
42
|
+
|
|
43
|
+
const decodingErrorCode = (error: unknown) => {
|
|
44
|
+
const code = (error as ErrorShape | null)?.cause?.code
|
|
45
|
+
|
|
46
|
+
return typeof code === 'string' && BODY_DECODING_ERROR_CODE.test(code)
|
|
47
|
+
? code
|
|
48
|
+
: undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `error` means the response arrived but its body could not be parsed
|
|
53
|
+
* or decoded:
|
|
54
|
+
* - `SyntaxError`: `response.json()` on a body that is not valid JSON. Checked
|
|
55
|
+
* by name so it also matches errors created in another realm (e.g. a test vm
|
|
56
|
+
* context).
|
|
57
|
+
* - A decoding error while reading the body, when `Content-Encoding` does not
|
|
58
|
+
* match the bytes: undici rejects with `TypeError: terminated`, caused by a
|
|
59
|
+
* zlib or brotli error.
|
|
60
|
+
* - `invalid-json`: the parse failure as reported by `node-fetch`.
|
|
61
|
+
* `@vtex/client-cms` only falls back to it when there is no global fetch,
|
|
62
|
+
* which does not happen on the Node versions core supports (>= 20.9), so this
|
|
63
|
+
* case is only a safeguard.
|
|
64
|
+
*
|
|
65
|
+
* Bodies that parse to an unexpected value (e.g. `null`) are not retried: they
|
|
66
|
+
* keep reaching callers as before.
|
|
67
|
+
*/
|
|
68
|
+
const isInvalidBodyError = (error: unknown) => {
|
|
69
|
+
const { name, type } = (error ?? {}) as ErrorShape
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
name === 'SyntaxError' ||
|
|
73
|
+
type === 'invalid-json' ||
|
|
74
|
+
decodingErrorCode(error) !== undefined
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Error name plus decoding code, without the message (it may embed body). */
|
|
79
|
+
const describeError = (error: unknown) =>
|
|
80
|
+
[(error as ErrorShape | null)?.name, decodingErrorCode(error)]
|
|
81
|
+
.filter(Boolean)
|
|
82
|
+
.join(': ')
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* `ClientCMS` that retries a request once, bypassing CDN caches, when the
|
|
86
|
+
* response body cannot be parsed or decoded. A single bad cached response
|
|
87
|
+
* (e.g. a compressed body stored without its `Content-Encoding`) would
|
|
88
|
+
* otherwise fail every page that depends on it until the cache entry expires.
|
|
89
|
+
*
|
|
90
|
+
* Only that case is retried: network errors are already retried by
|
|
91
|
+
* `@vtex/client-cms`, and non-2xx responses keep failing as before.
|
|
92
|
+
*
|
|
93
|
+
* The retry reaches the origin. When the retry fails too, for any reason (an
|
|
94
|
+
* invalid body, a 5xx, a network error), the origin itself is likely unhealthy,
|
|
95
|
+
* so that URL fails fast without retrying for `RETRY_COOLDOWN_MS` instead of
|
|
96
|
+
* adding an uncached origin hit to every page generation. Skipping the retry
|
|
97
|
+
* only falls back to the behavior without this class.
|
|
98
|
+
*/
|
|
99
|
+
export class ResilientClientCMS extends ClientCMS {
|
|
100
|
+
/** URL -> time until which its cache-busting retry is skipped. */
|
|
101
|
+
private readonly retryCooldownUntil = new Map<string, number>()
|
|
102
|
+
|
|
103
|
+
async fetch<T>(path = '', params?: URLSearchParams): Promise<T> {
|
|
104
|
+
try {
|
|
105
|
+
return await super.fetch<T>(path, params)
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (!isInvalidBodyError(error)) {
|
|
108
|
+
throw error
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const url = [path, params?.toString()].filter(Boolean).join('?')
|
|
112
|
+
if (this.isCoolingDown(url)) {
|
|
113
|
+
throw error
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Only the error name (and decoding error code) is logged: a JSON parse
|
|
117
|
+
// error message embeds the start of the body, which may be binary.
|
|
118
|
+
console.warn(
|
|
119
|
+
`[CMS] Invalid response body for "${url}" (${describeError(error)}). Retrying once bypassing the cache.`
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
const cacheBustingParams = new URLSearchParams(params)
|
|
123
|
+
cacheBustingParams.set(
|
|
124
|
+
CACHE_BUST_PARAM,
|
|
125
|
+
Math.floor(Date.now() / CACHE_BUST_WINDOW_MS).toString()
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
return await super.fetch<T>(path, cacheBustingParams)
|
|
130
|
+
} catch (retryError) {
|
|
131
|
+
// Any retry failure backs off, not only another invalid body: a 5xx or
|
|
132
|
+
// network error from the origin is also a reason not to add load.
|
|
133
|
+
this.startCooldown(url)
|
|
134
|
+
console.warn(
|
|
135
|
+
`[CMS] Cache-busting retry failed for "${url}" (${describeError(retryError)}). Skipping retries for this URL for ${RETRY_COOLDOWN_MS / 1000}s.`
|
|
136
|
+
)
|
|
137
|
+
throw retryError
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private isCoolingDown(url: string) {
|
|
143
|
+
const until = this.retryCooldownUntil.get(url)
|
|
144
|
+
|
|
145
|
+
if (until === undefined) {
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (until > Date.now()) {
|
|
150
|
+
return true
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.retryCooldownUntil.delete(url)
|
|
154
|
+
return false
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private startCooldown(url: string) {
|
|
158
|
+
const now = Date.now()
|
|
159
|
+
|
|
160
|
+
// Drop expired entries so URLs that are never requested again do not pile
|
|
161
|
+
// up for the lifetime of the process.
|
|
162
|
+
this.retryCooldownUntil.forEach((until, key) => {
|
|
163
|
+
if (until <= now) this.retryCooldownUntil.delete(key)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
this.retryCooldownUntil.set(url, now + RETRY_COOLDOWN_MS)
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/server/cms/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { ContentData, Locator } from '@vtex/client-cms'
|
|
2
|
-
import ClientCMS from '@vtex/client-cms'
|
|
2
|
+
import type ClientCMS from '@vtex/client-cms'
|
|
3
3
|
|
|
4
4
|
import MultipleContentError from 'src/sdk/error/MultipleContentError'
|
|
5
5
|
import { getStoreURL } from 'src/sdk/localization/useLocalizationConfig'
|
|
6
6
|
import { createPromiseCache } from 'src/server/content/promiseCache'
|
|
7
7
|
import { sanitizeHost } from 'src/utils/utilities'
|
|
8
8
|
import config from '../../../discovery.config'
|
|
9
|
+
import { ResilientClientCMS } from './ResilientClientCMS'
|
|
9
10
|
|
|
10
11
|
export type Options =
|
|
11
12
|
| Locator
|
|
@@ -72,7 +73,7 @@ export const isLocator = (x: any): x is Locator =>
|
|
|
72
73
|
typeof x.documentId === 'string' ||
|
|
73
74
|
typeof x.versionId === 'string')
|
|
74
75
|
|
|
75
|
-
export const clientCMS = new
|
|
76
|
+
export const clientCMS = new ResilientClientCMS({
|
|
76
77
|
workspace: config.api.workspace,
|
|
77
78
|
tenant: config.api.storeId,
|
|
78
79
|
builder:
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import React from 'react'
|
|
6
|
+
import { render } from '@testing-library/react'
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
8
|
+
|
|
9
|
+
const { received, capture } = vi.hoisted(() => {
|
|
10
|
+
const received: Record<string, Record<string, unknown>> = {}
|
|
11
|
+
const capture =
|
|
12
|
+
(name: string) =>
|
|
13
|
+
({
|
|
14
|
+
children,
|
|
15
|
+
...props
|
|
16
|
+
}: Readonly<{ children?: unknown } & Record<string, unknown>>) => {
|
|
17
|
+
received[name] = props
|
|
18
|
+
return (children ?? null) as never
|
|
19
|
+
}
|
|
20
|
+
return { received, capture }
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
vi.mock('@faststore/ui', () => ({
|
|
24
|
+
SearchDropdown: capture('SearchDropdown'),
|
|
25
|
+
SearchAutoComplete: capture('SearchAutoComplete'),
|
|
26
|
+
SearchAutoCompleteTerm: capture('SearchAutoCompleteTerm'),
|
|
27
|
+
SearchProducts: capture('SearchProducts'),
|
|
28
|
+
useSearch: () => ({
|
|
29
|
+
values: { products: [], term: '', terms: [], searchId: undefined },
|
|
30
|
+
}),
|
|
31
|
+
}))
|
|
32
|
+
|
|
33
|
+
vi.mock('src/components/search/SearchHistory', () => ({
|
|
34
|
+
SearchHistory: capture('SearchHistory'),
|
|
35
|
+
}))
|
|
36
|
+
|
|
37
|
+
vi.mock('src/components/search/SearchTop', () => ({
|
|
38
|
+
SearchTop: capture('SearchTop'),
|
|
39
|
+
}))
|
|
40
|
+
|
|
41
|
+
vi.mock('src/components/search/SearchProductItem', () => ({
|
|
42
|
+
default: () => null,
|
|
43
|
+
}))
|
|
44
|
+
|
|
45
|
+
vi.mock('src/sdk/search/formatSearchPath', () => ({
|
|
46
|
+
useFormatSearchPath: () => (args: { term: string }) => `/s?q=${args.term}`,
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
import SearchDropdown from '../../../src/components/search/SearchDropdown/SearchDropdown'
|
|
50
|
+
|
|
51
|
+
afterEach(() => {
|
|
52
|
+
for (const key of Object.keys(received)) delete received[key]
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('SearchDropdown labels', () => {
|
|
56
|
+
it('forwards the CMS labels to each dropdown block', () => {
|
|
57
|
+
render(
|
|
58
|
+
<SearchDropdown
|
|
59
|
+
sort="score_desc"
|
|
60
|
+
loadingLabel="Carregando..."
|
|
61
|
+
searchHistoryTitle="Histórico"
|
|
62
|
+
searchHistoryClearLabel="Limpar histórico"
|
|
63
|
+
searchTopTitle="Mais buscados"
|
|
64
|
+
searchProductsTitle="Produtos sugeridos"
|
|
65
|
+
/>
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
expect(received.SearchDropdown).toMatchObject({
|
|
69
|
+
loadingLabel: 'Carregando...',
|
|
70
|
+
})
|
|
71
|
+
expect(received.SearchHistory).toMatchObject({
|
|
72
|
+
title: 'Histórico',
|
|
73
|
+
clearLabel: 'Limpar histórico',
|
|
74
|
+
})
|
|
75
|
+
expect(received.SearchTop).toMatchObject({ title: 'Mais buscados' })
|
|
76
|
+
expect(received.SearchProducts).toMatchObject({
|
|
77
|
+
title: 'Produtos sugeridos',
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
})
|
|
@@ -3,12 +3,30 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import React from 'react'
|
|
6
|
-
import { act, render, waitFor } from '@testing-library/react'
|
|
6
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react'
|
|
7
7
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
8
8
|
|
|
9
|
-
// ─── next/dynamic:
|
|
9
|
+
// ─── next/dynamic: render the input field as a plain <input> ─────────────────
|
|
10
|
+
// `onFocus` opens the dropdown, and props leaked from SearchInput show up as
|
|
11
|
+
// DOM attributes.
|
|
10
12
|
vi.mock('next/dynamic', () => ({
|
|
11
|
-
default: () =>
|
|
13
|
+
default: () =>
|
|
14
|
+
function InputField({
|
|
15
|
+
onFocus,
|
|
16
|
+
...props
|
|
17
|
+
}: Readonly<React.InputHTMLAttributes<HTMLInputElement>>) {
|
|
18
|
+
const domProps = Object.fromEntries(
|
|
19
|
+
Object.entries(props).filter(([, value]) => typeof value === 'string')
|
|
20
|
+
)
|
|
21
|
+
return (
|
|
22
|
+
<input
|
|
23
|
+
data-testid="search-field"
|
|
24
|
+
readOnly
|
|
25
|
+
onFocus={onFocus}
|
|
26
|
+
{...domProps}
|
|
27
|
+
/>
|
|
28
|
+
)
|
|
29
|
+
},
|
|
12
30
|
}))
|
|
13
31
|
|
|
14
32
|
// ─── next/router ─────────────────────────────────────────────────────────────
|
|
@@ -17,7 +35,16 @@ vi.mock('next/router', () => ({
|
|
|
17
35
|
}))
|
|
18
36
|
|
|
19
37
|
// ─── lazy-loaded chunk ───────────────────────────────────────────────────────
|
|
20
|
-
vi.
|
|
38
|
+
const dropdownProps = vi.hoisted(() => ({
|
|
39
|
+
current: null as Record<string, unknown> | null,
|
|
40
|
+
}))
|
|
41
|
+
|
|
42
|
+
vi.mock('src/components/search/SearchDropdown', () => ({
|
|
43
|
+
default: (props: Record<string, unknown>) => {
|
|
44
|
+
dropdownProps.current = props
|
|
45
|
+
return null
|
|
46
|
+
},
|
|
47
|
+
}))
|
|
21
48
|
|
|
22
49
|
// ─── discovery.config ────────────────────────────────────────────────────────
|
|
23
50
|
vi.mock('discovery.config', () => ({
|
|
@@ -125,6 +152,7 @@ import SearchInput from '../../../src/components/search/SearchInput/SearchInput'
|
|
|
125
152
|
|
|
126
153
|
afterEach(() => {
|
|
127
154
|
vi.clearAllMocks()
|
|
155
|
+
dropdownProps.current = null
|
|
128
156
|
oesState.status = null
|
|
129
157
|
oesState.isUploading = false
|
|
130
158
|
oesState.isProcessing = false
|
|
@@ -274,3 +302,42 @@ describe('SearchInput (OES integration)', () => {
|
|
|
274
302
|
expect(true).toBe(true)
|
|
275
303
|
})
|
|
276
304
|
})
|
|
305
|
+
|
|
306
|
+
describe('SearchInput dropdown labels', () => {
|
|
307
|
+
const labels = {
|
|
308
|
+
loadingLabel: 'Carregando...',
|
|
309
|
+
searchHistoryTitle: 'Histórico',
|
|
310
|
+
searchHistoryClearLabel: 'Limpar histórico',
|
|
311
|
+
searchTopTitle: 'Mais buscados',
|
|
312
|
+
searchProductsTitle: 'Produtos sugeridos',
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
it('forwards the CMS labels to the SearchDropdown', async () => {
|
|
316
|
+
const { getByTestId } = render(
|
|
317
|
+
<Wrapper>
|
|
318
|
+
<SearchInput {...labels} />
|
|
319
|
+
</Wrapper>
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
await act(async () => {
|
|
323
|
+
fireEvent.focus(getByTestId('search-field'))
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
await waitFor(() => {
|
|
327
|
+
expect(dropdownProps.current).toMatchObject(labels)
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
it('does not leak the CMS labels to the input element', () => {
|
|
332
|
+
const { getByTestId } = render(
|
|
333
|
+
<Wrapper>
|
|
334
|
+
<SearchInput {...labels} />
|
|
335
|
+
</Wrapper>
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
const field = getByTestId('search-field')
|
|
339
|
+
for (const name of Object.keys(labels)) {
|
|
340
|
+
expect(field.hasAttribute(name)).toBe(false)
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
})
|