@faststore/core 3.0.59 → 3.0.60
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/.next/BUILD_ID +1 -1
- package/.next/build-manifest.json +6 -6
- package/.next/cache/.tsbuildinfo +1 -1
- package/.next/cache/config.json +3 -3
- package/.next/cache/eslint/.cache_1gneedd +1 -1
- package/.next/cache/fetch-cache/50912854cb7c781522a6ff8792d714e549515fcbbbfd660761961b06afe01c07 +1 -1
- package/.next/cache/webpack/client-production/0.pack +0 -0
- package/.next/cache/webpack/client-production/index.pack +0 -0
- package/.next/cache/webpack/server-production/0.pack +0 -0
- package/.next/cache/webpack/server-production/index.pack +0 -0
- package/.next/next-minimal-server.js.nft.json +1 -1
- package/.next/next-server.js.nft.json +1 -1
- package/.next/prerender-manifest.js +1 -1
- package/.next/prerender-manifest.json +1 -1
- package/.next/routes-manifest.json +1 -1
- package/.next/server/app/_not-found.html +1 -1
- package/.next/server/app/_not-found.js.nft.json +1 -1
- package/.next/server/app/_not-found.rsc +1 -1
- package/.next/server/app/fs-next-update/page.js.nft.json +1 -1
- package/.next/server/app/fs-next-update.html +1 -1
- package/.next/server/app/fs-next-update.rsc +1 -1
- package/.next/server/chunks/2133.js +5 -5
- package/.next/server/chunks/{6112.js → 2381.js} +1 -1
- package/.next/server/middleware-build-manifest.js +1 -1
- package/.next/server/pages/404.html +1 -1
- package/.next/server/pages/404.js.nft.json +1 -1
- package/.next/server/pages/500.js.nft.json +1 -1
- package/.next/server/pages/[...slug].js +2 -2
- package/.next/server/pages/[...slug].js.nft.json +1 -1
- package/.next/server/pages/[slug]/p.js +1 -1
- package/.next/server/pages/[slug]/p.js.nft.json +1 -1
- package/.next/server/pages/_app.js.nft.json +1 -1
- package/.next/server/pages/_document.js.nft.json +1 -1
- package/.next/server/pages/_error.js.nft.json +1 -1
- package/.next/server/pages/account.js.nft.json +1 -1
- package/.next/server/pages/api/graphql.js.nft.json +1 -1
- package/.next/server/pages/api/health/live.js.nft.json +1 -1
- package/.next/server/pages/api/health/ready.js.nft.json +1 -1
- package/.next/server/pages/api/preview.js.nft.json +1 -1
- package/.next/server/pages/checkout.js.nft.json +1 -1
- package/.next/server/pages/en-US/404.html +2 -2
- package/.next/server/pages/en-US/500.html +2 -2
- package/.next/server/pages/en-US/account.html +2 -2
- package/.next/server/pages/en-US/checkout.html +2 -2
- package/.next/server/pages/en-US/login.html +2 -2
- package/.next/server/pages/en-US/s.html +2 -2
- package/.next/server/pages/en-US.html +2 -2
- package/.next/server/pages/index.js.nft.json +1 -1
- package/.next/server/pages/login.js.nft.json +1 -1
- package/.next/server/pages/s.js.nft.json +1 -1
- package/.next/server/pages-manifest.json +1 -1
- package/.next/static/{lIYvWbgkX76Tpj8gVu1kU → 6RpWP2aATNq_ljgOww_7v}/_buildManifest.js +1 -1
- package/.next/trace +62 -62
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +6 -5
- package/package.json +2 -2
- package/src/pages/[...slug].tsx +3 -1
- package/src/server/cms/pdp.ts +1 -1
- package/src/server/cms/plp.ts +33 -14
- package/src/utils/multipleTemplates.ts +178 -0
- package/src/utils/utilities.ts +0 -118
- package/test/utils/multipleTemplates.test.ts +66 -0
- /package/.next/static/{lIYvWbgkX76Tpj8gVu1kU → 6RpWP2aATNq_ljgOww_7v}/_ssgManifest.js +0 -0
- /package/.next/static/chunks/{590-a0b6f16148203bf2.js → 590-da547057f2ae283b.js} +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
$ yarn partytown & yarn generate && next build
|
|
2
2
|
$ partytown copylib ./public/~partytown
|
|
3
|
-
$ faststore generate-graphql -c
|
|
4
3
|
Partytown lib copied to: /home/runner/work/faststore/faststore/packages/core/public/~partytown
|
|
4
|
+
$ faststore generate-graphql -c
|
|
5
5
|
success - GraphQL schema, types, and optimizations successfully generated 🎉
|
|
6
6
|
⚠ No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
|
|
7
7
|
Attention: Next.js now collects completely anonymous telemetry regarding usage.
|
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
$ jest
|
|
2
|
-
PASS test/
|
|
3
|
-
PASS test/server/index.test.ts (
|
|
2
|
+
PASS test/utils/multipleTemplates.test.ts (32.235 s)
|
|
3
|
+
PASS test/server/cms/index.test.ts (32.591 s)
|
|
4
|
+
PASS test/server/index.test.ts (34.429 s)
|
|
4
5
|
|
|
5
|
-
Test Suites:
|
|
6
|
-
Tests:
|
|
6
|
+
Test Suites: 3 passed, 3 total
|
|
7
|
+
Tests: 16 passed, 16 total
|
|
7
8
|
Snapshots: 0 total
|
|
8
|
-
Time:
|
|
9
|
+
Time: 35.349 s
|
|
9
10
|
Ran all test suites.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.60",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": "vtex/faststore",
|
|
6
6
|
"browserslist": "supports es6-module and not dead",
|
|
@@ -125,5 +125,5 @@
|
|
|
125
125
|
"node": "18.19.0",
|
|
126
126
|
"yarn": "1.19.1"
|
|
127
127
|
},
|
|
128
|
-
"gitHead": "
|
|
128
|
+
"gitHead": "19bd97e7ce1d842d390dbdd1e7e1859c1624b5fb"
|
|
129
129
|
}
|
package/src/pages/[...slug].tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isNotFoundError } from '@faststore/api'
|
|
2
|
+
import storeConfig from 'faststore.config'
|
|
2
3
|
import type { GetStaticPaths, GetStaticProps } from 'next'
|
|
3
4
|
|
|
4
5
|
import { gql } from '@generated'
|
|
@@ -86,6 +87,7 @@ export const getStaticProps: GetStaticProps<
|
|
|
86
87
|
Locator
|
|
87
88
|
> = async ({ params, previewData }) => {
|
|
88
89
|
const slug = params?.slug.join('/') ?? ''
|
|
90
|
+
const rewrites = (await storeConfig.rewrites?.()) ?? []
|
|
89
91
|
|
|
90
92
|
const [landingPagePromise, globalSectionsPromise] = [
|
|
91
93
|
getLandingPageBySlug(slug, previewData),
|
|
@@ -114,7 +116,7 @@ export const getStaticProps: GetStaticProps<
|
|
|
114
116
|
variables: { slug },
|
|
115
117
|
operation: query,
|
|
116
118
|
}),
|
|
117
|
-
getPLP(slug, previewData),
|
|
119
|
+
getPLP(slug, previewData, rewrites),
|
|
118
120
|
])
|
|
119
121
|
|
|
120
122
|
const notFound = errors.find(isNotFoundError)
|
package/src/server/cms/pdp.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ServerProductQueryQuery } from '@generated/graphql'
|
|
2
2
|
import type { ContentData, Locator } from '@vtex/client-cms'
|
|
3
3
|
import MissingContentError from 'src/sdk/error/MissingContentError'
|
|
4
|
-
import { findBestPDPTemplate } from 'src/utils/
|
|
4
|
+
import { findBestPDPTemplate } from 'src/utils/multipleTemplates'
|
|
5
5
|
import { Options, getCMSPage, getPage } from '.'
|
|
6
6
|
import config from '../../../faststore.config'
|
|
7
7
|
|
package/src/server/cms/plp.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { ContentData, Locator } from '@vtex/client-cms'
|
|
2
2
|
import MissingContentError from 'src/sdk/error/MissingContentError'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
Rewrite,
|
|
5
|
+
RewritesConfig,
|
|
6
|
+
findBestPLPTemplate,
|
|
7
|
+
} from 'src/utils/multipleTemplates'
|
|
4
8
|
import config from '../../../faststore.config'
|
|
5
9
|
import { Options, getCMSPage, getPage } from '../cms'
|
|
6
10
|
|
|
@@ -23,27 +27,41 @@ type PLPfromCmsEnvData = {
|
|
|
23
27
|
|
|
24
28
|
export type PLPContentType = ContentData & PLPSettings
|
|
25
29
|
|
|
26
|
-
export const getPLP = async (
|
|
30
|
+
export const getPLP = async (
|
|
31
|
+
slug: string,
|
|
32
|
+
previewData: Locator,
|
|
33
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
34
|
+
) => {
|
|
27
35
|
if (config.cms.data) {
|
|
28
36
|
const cmsData = JSON.parse(config.cms.data)
|
|
29
37
|
const allPLPsFromCmsEnvData: PLPfromCmsEnvData[] = cmsData['plp']
|
|
30
38
|
|
|
31
|
-
return await getPLPFromCmsEnvData(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
return await getPLPFromCmsEnvData(
|
|
40
|
+
`/${slug}/`,
|
|
41
|
+
allPLPsFromCmsEnvData,
|
|
42
|
+
{
|
|
43
|
+
...(previewData?.contentType === 'plp' ? previewData : null),
|
|
44
|
+
contentType: 'plp',
|
|
45
|
+
},
|
|
46
|
+
rewrites
|
|
47
|
+
)
|
|
35
48
|
}
|
|
36
49
|
|
|
37
|
-
return (await getPLPFromCms(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
return (await getPLPFromCms(
|
|
51
|
+
`/${slug}/`,
|
|
52
|
+
{
|
|
53
|
+
...(previewData?.contentType === 'plp' ? previewData : null),
|
|
54
|
+
contentType: 'plp',
|
|
55
|
+
},
|
|
56
|
+
rewrites
|
|
57
|
+
)) as PLPContentType
|
|
41
58
|
}
|
|
42
59
|
|
|
43
60
|
const getPLPFromCmsEnvData = async (
|
|
44
61
|
slug: string,
|
|
45
62
|
allPLPsFromCMSData: PLPfromCmsEnvData[],
|
|
46
|
-
options: Options
|
|
63
|
+
options: Options,
|
|
64
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
47
65
|
): Promise<PLPContentType> => {
|
|
48
66
|
const pages: PLPfromCmsEnvData[] = allPLPsFromCMSData ?? []
|
|
49
67
|
|
|
@@ -51,7 +69,7 @@ const getPLPFromCmsEnvData = async (
|
|
|
51
69
|
throw new MissingContentError(options)
|
|
52
70
|
}
|
|
53
71
|
|
|
54
|
-
const template = findBestPLPTemplate(pages, slug)
|
|
72
|
+
const template = findBestPLPTemplate(pages, slug, rewrites)
|
|
55
73
|
|
|
56
74
|
return getPage<PLPContentType>({
|
|
57
75
|
contentType: 'plp',
|
|
@@ -62,7 +80,8 @@ const getPLPFromCmsEnvData = async (
|
|
|
62
80
|
|
|
63
81
|
export const getPLPFromCms = async (
|
|
64
82
|
slug: string,
|
|
65
|
-
options: Options
|
|
83
|
+
options: Options,
|
|
84
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
66
85
|
): Promise<Partial<PLPContentType>> => {
|
|
67
86
|
const pages = (await getCMSPage(options)).data
|
|
68
87
|
|
|
@@ -70,5 +89,5 @@ export const getPLPFromCms = async (
|
|
|
70
89
|
throw new MissingContentError(options)
|
|
71
90
|
}
|
|
72
91
|
|
|
73
|
-
return findBestPLPTemplate(pages, slug)
|
|
92
|
+
return findBestPLPTemplate(pages, slug, rewrites)
|
|
74
93
|
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { ServerProductQueryQuery } from '@generated/graphql'
|
|
2
|
+
import { PDPContentType } from 'src/server/cms/pdp'
|
|
3
|
+
import { PLPContentType } from 'src/server/cms/plp'
|
|
4
|
+
|
|
5
|
+
export type Rewrite = {
|
|
6
|
+
source: string
|
|
7
|
+
destination: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type RewritesConfig = {
|
|
11
|
+
beforeFiles?: Rewrite[]
|
|
12
|
+
afterFiles?: Rewrite[]
|
|
13
|
+
fallback?: Rewrite[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function normalizePLPSlug(slug: string) {
|
|
17
|
+
// Remove extra slashes at the beginning and end
|
|
18
|
+
let normalizedSlug = slug.replace(/^\/+|\/+$/g, '')
|
|
19
|
+
|
|
20
|
+
// Remove duplicate slashes and white spaces throughout the string
|
|
21
|
+
normalizedSlug = normalizedSlug.replace(/\/+/g, '/').replace(/\s+/g, '')
|
|
22
|
+
|
|
23
|
+
return '/' + normalizedSlug.toLowerCase()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function findPLPTemplateBySlug(
|
|
27
|
+
pages: Partial<PLPContentType>[],
|
|
28
|
+
slug: string,
|
|
29
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
30
|
+
) {
|
|
31
|
+
return pages.find((page) => {
|
|
32
|
+
// generic PLP template
|
|
33
|
+
if (!page.settings?.template?.value) return false
|
|
34
|
+
|
|
35
|
+
const templateValue = normalizePLPSlug(page.settings?.template?.value)
|
|
36
|
+
return (
|
|
37
|
+
templateValue === slug ||
|
|
38
|
+
hasRewritesConfigForSlug({ rewrites, templateValue, slug })
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Normalizes the rewrites object, and returns:
|
|
45
|
+
* true if the templateValue from hCMS passed as param match the source value from the rewrites object AND the slug match the destination.
|
|
46
|
+
* see https://nextjs.org/docs/app/api-reference/next-config-js/rewrites
|
|
47
|
+
* @param rewrites
|
|
48
|
+
* @param templateValue
|
|
49
|
+
* @param slug
|
|
50
|
+
* @returns boolean indicating if the slug exist as a rewrite destination
|
|
51
|
+
*/
|
|
52
|
+
export function hasRewritesConfigForSlug({
|
|
53
|
+
rewrites,
|
|
54
|
+
templateValue,
|
|
55
|
+
slug,
|
|
56
|
+
}: {
|
|
57
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
58
|
+
templateValue: string
|
|
59
|
+
slug: string
|
|
60
|
+
}): boolean {
|
|
61
|
+
if (!rewrites) {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let allRewrites: Rewrite[] = []
|
|
66
|
+
|
|
67
|
+
if (Array.isArray(rewrites)) {
|
|
68
|
+
allRewrites = rewrites
|
|
69
|
+
} else {
|
|
70
|
+
allRewrites = [
|
|
71
|
+
...(rewrites?.beforeFiles || []),
|
|
72
|
+
...(rewrites?.afterFiles || []),
|
|
73
|
+
...(rewrites?.fallback || []),
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const foundRewrite = allRewrites.find(
|
|
78
|
+
(rewriteConfig) =>
|
|
79
|
+
rewriteConfig.source === templateValue &&
|
|
80
|
+
rewriteConfig.destination === slug
|
|
81
|
+
)
|
|
82
|
+
return Boolean(foundRewrite)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Find the best PLP template from the CMS based on the slug passed as param.
|
|
87
|
+
*
|
|
88
|
+
* This function iterates the slug until there is no slashes (/), prioritizing the following order:
|
|
89
|
+
* 1. A PLP template that matches the subcategory (e.g. slug = /department/category/subcategory).
|
|
90
|
+
* 2. A PLP template that matches the category (e.g. slug = /department/category).
|
|
91
|
+
* 3. A PLP template that matches the department (e.g. slug = /department).
|
|
92
|
+
* 4. If no matches are found, use the generic PLP template.
|
|
93
|
+
*
|
|
94
|
+
* @param pages
|
|
95
|
+
* @param originalSlug
|
|
96
|
+
* @returns The best PLP template page for the slug
|
|
97
|
+
*/
|
|
98
|
+
export function findBestPLPTemplate(
|
|
99
|
+
pages: Partial<PLPContentType>[],
|
|
100
|
+
originalSlug: string,
|
|
101
|
+
rewrites: Rewrite[] | RewritesConfig
|
|
102
|
+
) {
|
|
103
|
+
let slug = normalizePLPSlug(originalSlug)
|
|
104
|
+
let foundPageTemplate = findPLPTemplateBySlug(pages, slug, rewrites)
|
|
105
|
+
|
|
106
|
+
while (!foundPageTemplate && slug.lastIndexOf('/') !== -1) {
|
|
107
|
+
slug = slug.substring(0, slug.lastIndexOf('/'))
|
|
108
|
+
foundPageTemplate = findPLPTemplateBySlug(pages, slug, rewrites)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
foundPageTemplate ||
|
|
113
|
+
pages.find((page) => !page.settings?.template?.value) ||
|
|
114
|
+
pages[0]
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function normalizePDPTemplate(templateValue: string) {
|
|
119
|
+
// Remove extra slashes, white spaces at the beginning and end
|
|
120
|
+
let formattedValue = templateValue.trim().replace(/^\/+|\/+$/g, '')
|
|
121
|
+
|
|
122
|
+
// Add a slash at the beginning if not present
|
|
123
|
+
if (!formattedValue.startsWith('/')) {
|
|
124
|
+
formattedValue = '/' + formattedValue
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Add a slash at the end if not ending with '/p'
|
|
128
|
+
if (!formattedValue.endsWith('/p')) {
|
|
129
|
+
formattedValue += '/'
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Remove duplicate slashes and white spaces throughout the string
|
|
133
|
+
formattedValue = formattedValue.replace(/\/+/g, '/').replace(/\s+/g, '')
|
|
134
|
+
|
|
135
|
+
return formattedValue.toLowerCase()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Find the best PDP template from the CMS based on the slug or in the product category tree.
|
|
140
|
+
* Prioritizing the following order:
|
|
141
|
+
*
|
|
142
|
+
* 1. A PDP template that matches the page slug (e.g. slug = /apple-magic-mouse/p).
|
|
143
|
+
* 2. A PDP template that matches the product subcategory (e.g. /department/category/subcategory).
|
|
144
|
+
* 3. A PDP template that matches the product category (e.g. /department/category).
|
|
145
|
+
* 4. A PDP template that matches the product department (e.g. /department).
|
|
146
|
+
* 5. If no matches are found, use the generic PDP template.
|
|
147
|
+
*
|
|
148
|
+
* @param pages
|
|
149
|
+
* @param originalSlug
|
|
150
|
+
* @param product
|
|
151
|
+
* @returns The best PDP template page for the slug
|
|
152
|
+
*/
|
|
153
|
+
export function findBestPDPTemplate(
|
|
154
|
+
pages: Partial<PDPContentType>[],
|
|
155
|
+
slug: string,
|
|
156
|
+
product: ServerProductQueryQuery['product']
|
|
157
|
+
) {
|
|
158
|
+
// productSlugAndCategoryTree with the prioritized order. [slug, subcategory tree, category tree, department]
|
|
159
|
+
const productSlugAndCategoryTree = product?.breadcrumbList?.itemListElement
|
|
160
|
+
? [...product?.breadcrumbList?.itemListElement]
|
|
161
|
+
.reverse()
|
|
162
|
+
.map(({ item }) => item)
|
|
163
|
+
: []
|
|
164
|
+
productSlugAndCategoryTree.unshift(slug)
|
|
165
|
+
|
|
166
|
+
for (const item of productSlugAndCategoryTree) {
|
|
167
|
+
for (const page of pages) {
|
|
168
|
+
if (!page.settings?.template?.value) continue
|
|
169
|
+
|
|
170
|
+
const templateValue = normalizePDPTemplate(page.settings.template.value)
|
|
171
|
+
if (templateValue === item) {
|
|
172
|
+
return page
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return pages.find((page) => !page.settings?.template?.value) || pages[0]
|
|
178
|
+
}
|
package/src/utils/utilities.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { ServerProductQueryQuery } from '@generated/graphql'
|
|
2
|
-
import { PDPContentType } from 'src/server/cms/pdp'
|
|
3
|
-
import { PLPContentType } from 'src/server/cms/plp'
|
|
4
|
-
|
|
5
1
|
//Input "Example Text!". Output: example-text
|
|
6
2
|
export function textToKebabCase(text: string): string {
|
|
7
3
|
// Replace spaces and special characters with hyphens
|
|
@@ -15,117 +11,3 @@ export function textToKebabCase(text: string): string {
|
|
|
15
11
|
|
|
16
12
|
return kebabCase ?? ''
|
|
17
13
|
}
|
|
18
|
-
|
|
19
|
-
export function normalizePLPSlug(slug: string) {
|
|
20
|
-
// Remove extra slashes at the beginning and end
|
|
21
|
-
let normalizedSlug = slug.replace(/^\/+|\/+$/g, '')
|
|
22
|
-
|
|
23
|
-
// Remove duplicate slashes and white spaces throughout the string
|
|
24
|
-
normalizedSlug = normalizedSlug.replace(/\/+/g, '/').replace(/\s+/g, '')
|
|
25
|
-
|
|
26
|
-
return '/' + normalizedSlug.toLowerCase()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function findPLPTemplateBySlug(pages: Partial<PLPContentType>[], slug: string) {
|
|
30
|
-
return pages.find((page) => {
|
|
31
|
-
// generic PLP template
|
|
32
|
-
if (!page.settings?.template?.value) return false
|
|
33
|
-
|
|
34
|
-
const templateValue = normalizePLPSlug(page.settings?.template?.value)
|
|
35
|
-
return templateValue === slug
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Find the best PLP template from the CMS based on the slug passed as param.
|
|
41
|
-
*
|
|
42
|
-
* This function iterates the slug until there is no slashes (/), prioritizing the following order:
|
|
43
|
-
* 1. A PLP template that matches the subcategory (e.g. slug = /department/category/subcategory).
|
|
44
|
-
* 2. A PLP template that matches the category (e.g. slug = /department/category).
|
|
45
|
-
* 3. A PLP template that matches the department (e.g. slug = /department).
|
|
46
|
-
* 4. If no matches are found, use the generic PLP template.
|
|
47
|
-
*
|
|
48
|
-
* @param pages
|
|
49
|
-
* @param originalSlug
|
|
50
|
-
* @returns The best PLP template page for the slug
|
|
51
|
-
*/
|
|
52
|
-
export function findBestPLPTemplate(
|
|
53
|
-
pages: Partial<PLPContentType>[],
|
|
54
|
-
originalSlug: string
|
|
55
|
-
) {
|
|
56
|
-
let slug = normalizePLPSlug(originalSlug)
|
|
57
|
-
let foundPageTemplate = findPLPTemplateBySlug(pages, slug)
|
|
58
|
-
|
|
59
|
-
while (!foundPageTemplate && slug.lastIndexOf('/') !== -1) {
|
|
60
|
-
slug = slug.substring(0, slug.lastIndexOf('/'))
|
|
61
|
-
foundPageTemplate = findPLPTemplateBySlug(pages, slug)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return (
|
|
65
|
-
foundPageTemplate ||
|
|
66
|
-
pages.find((page) => !page.settings?.template?.value) ||
|
|
67
|
-
pages[0]
|
|
68
|
-
)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function normalizePDPTemplate(templateValue: string) {
|
|
72
|
-
// Remove extra slashes, white spaces at the beginning and end
|
|
73
|
-
let formattedValue = templateValue.trim().replace(/^\/+|\/+$/g, '')
|
|
74
|
-
|
|
75
|
-
// Add a slash at the beginning if not present
|
|
76
|
-
if (!formattedValue.startsWith('/')) {
|
|
77
|
-
formattedValue = '/' + formattedValue
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Add a slash at the end if not ending with '/p'
|
|
81
|
-
if (!formattedValue.endsWith('/p')) {
|
|
82
|
-
formattedValue += '/'
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Remove duplicate slashes and white spaces throughout the string
|
|
86
|
-
formattedValue = formattedValue.replace(/\/+/g, '/').replace(/\s+/g, '')
|
|
87
|
-
|
|
88
|
-
return formattedValue.toLowerCase()
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Find the best PDP template from the CMS based on the slug or in the product category tree.
|
|
93
|
-
* Prioritizing the following order:
|
|
94
|
-
*
|
|
95
|
-
* 1. A PDP template that matches the page slug (e.g. slug = /apple-magic-mouse/p).
|
|
96
|
-
* 2. A PDP template that matches the product subcategory (e.g. /department/category/subcategory).
|
|
97
|
-
* 3. A PDP template that matches the product category (e.g. /department/category).
|
|
98
|
-
* 4. A PDP template that matches the product department (e.g. /department).
|
|
99
|
-
* 5. If no matches are found, use the generic PDP template.
|
|
100
|
-
*
|
|
101
|
-
* @param pages
|
|
102
|
-
* @param originalSlug
|
|
103
|
-
* @param product
|
|
104
|
-
* @returns The best PDP template page for the slug
|
|
105
|
-
*/
|
|
106
|
-
export function findBestPDPTemplate(
|
|
107
|
-
pages: Partial<PDPContentType>[],
|
|
108
|
-
slug: string,
|
|
109
|
-
product: ServerProductQueryQuery['product']
|
|
110
|
-
) {
|
|
111
|
-
// productSlugAndCategoryTree with the prioritized order. [slug, subcategory tree, category tree, department]
|
|
112
|
-
const productSlugAndCategoryTree = product?.breadcrumbList?.itemListElement
|
|
113
|
-
? [...product?.breadcrumbList?.itemListElement]
|
|
114
|
-
.reverse()
|
|
115
|
-
.map(({ item }) => item)
|
|
116
|
-
: []
|
|
117
|
-
productSlugAndCategoryTree.unshift(slug)
|
|
118
|
-
|
|
119
|
-
for (const item of productSlugAndCategoryTree) {
|
|
120
|
-
for (const page of pages) {
|
|
121
|
-
if (!page.settings?.template?.value) continue
|
|
122
|
-
|
|
123
|
-
const templateValue = normalizePDPTemplate(page.settings.template.value)
|
|
124
|
-
if (templateValue === item) {
|
|
125
|
-
return page
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return pages.find((page) => !page.settings?.template?.value) || pages[0]
|
|
131
|
-
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Rewrite, RewritesConfig } from '../../src/utils/multipleTemplates'
|
|
2
|
+
import { hasRewritesConfigForSlug } from '../../src/utils/multipleTemplates'
|
|
3
|
+
|
|
4
|
+
describe('Multiple page templates', () => {
|
|
5
|
+
describe('hasRewritesConfigForSlug', () => {
|
|
6
|
+
it('should return true when rewrites is an array of objects containing the desired destination and source', () => {
|
|
7
|
+
const rewritesArr: Rewrite[] = [
|
|
8
|
+
{ source: '/test/my-office', destination: '/office' },
|
|
9
|
+
]
|
|
10
|
+
const result = hasRewritesConfigForSlug({
|
|
11
|
+
rewrites: rewritesArr,
|
|
12
|
+
templateValue: '/test/my-office',
|
|
13
|
+
slug: '/office',
|
|
14
|
+
})
|
|
15
|
+
expect(result).toBe(true)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should return true when rewrites is an object with three arrays containing the desired destination and source', () => {
|
|
19
|
+
const rewritesObj: RewritesConfig = {
|
|
20
|
+
beforeFiles: [{ source: '/test/my-office', destination: '/office' }],
|
|
21
|
+
afterFiles: [],
|
|
22
|
+
fallback: [],
|
|
23
|
+
}
|
|
24
|
+
const result = hasRewritesConfigForSlug({
|
|
25
|
+
rewrites: rewritesObj,
|
|
26
|
+
templateValue: '/test/my-office',
|
|
27
|
+
slug: '/office',
|
|
28
|
+
})
|
|
29
|
+
expect(result).toBe(true)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should return false when the desired source is found but the destination is different', () => {
|
|
33
|
+
const rewritesArr: Rewrite[] = [
|
|
34
|
+
{ source: '/test/my-office', destination: '/home' },
|
|
35
|
+
]
|
|
36
|
+
const result = hasRewritesConfigForSlug({
|
|
37
|
+
rewrites: rewritesArr,
|
|
38
|
+
templateValue: '/test/my-office',
|
|
39
|
+
slug: '/office',
|
|
40
|
+
})
|
|
41
|
+
expect(result).toBe(false)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should return false when the rewrites is empty array', () => {
|
|
45
|
+
const rewritesArr = []
|
|
46
|
+
const result = hasRewritesConfigForSlug({
|
|
47
|
+
rewrites: rewritesArr,
|
|
48
|
+
templateValue: '/test/my-office',
|
|
49
|
+
slug: '/office',
|
|
50
|
+
})
|
|
51
|
+
expect(result).toBe(false)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should return false when the desired destination is found but the source is different', () => {
|
|
55
|
+
const rewritesArr: Rewrite[] = [
|
|
56
|
+
{ source: '/test/my-home', destination: '/office' },
|
|
57
|
+
]
|
|
58
|
+
const result = hasRewritesConfigForSlug({
|
|
59
|
+
rewrites: rewritesArr,
|
|
60
|
+
templateValue: '/test/my-office',
|
|
61
|
+
slug: '/office',
|
|
62
|
+
})
|
|
63
|
+
expect(result).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
})
|
|
File without changes
|
|
File without changes
|