Skip to content

Commit

Permalink
Build store URL using subDomainPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
lariciamota committed Feb 21, 2025
1 parent 09a30aa commit 3749d01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/pages/[slug]/p.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { getOfferUrl, useOffer } from 'src/sdk/offer'
import PageProvider, { type PDPContext } from 'src/sdk/overrides/PageProvider'
import { useProductQuery } from 'src/sdk/product/useProductQuery'
import { getPDP, type PDPContentType } from 'src/server/cms/pdp'
import { buildStoreUrl } from 'src/utils/buildStoreUrl'

type StoreConfig = typeof storeConfig & {
experimental: {
Expand Down Expand Up @@ -87,6 +88,7 @@ function Page({
globalSections,
offers,
meta,
storeUrl,
}: Props) {
const { product } = server
const { currency } = useSession()
Expand All @@ -96,7 +98,7 @@ function Page({
if (itemListElements.length !== 0) {
itemListElements = itemListElements.map(
({ item: pathname, name, position }) => {
const pageUrl = storeConfig.storeUrl + pathname
const pageUrl = storeUrl + pathname

return { name, position, item: pageUrl }
}
Expand Down Expand Up @@ -297,7 +299,11 @@ export const getStaticProps: GetStaticProps<
const { seo } = data.product
const title = seo.title || storeConfig.seo.title
const description = seo.description || storeConfig.seo.description
const canonical = `${storeConfig.storeUrl}${seo.canonical}`
const storeUrl = buildStoreUrl(
storeConfig.storeUrl,
(storeConfig.api as Record<string, any>).subDomainPrefix
)
const canonical = `${storeUrl}${seo.canonical}`

const meta = { title, description, canonical }

Expand All @@ -323,6 +329,7 @@ export const getStaticProps: GetStaticProps<
offers,
globalSections,
key: seo.canonical,
storeUrl,
},
revalidate: (storeConfig as StoreConfig).experimental.revalidate ?? false,
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/server/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export const apiOptions: APIOptions = {
platform: storeConfig.platform as APIOptions['platform'],
account: storeConfig.api.storeId,
environment: storeConfig.api.environment as APIOptions['environment'],
subDomainPrefix: storeConfig.api.subDomainPrefix ?? ['www'],
subDomainPrefix: (storeConfig.api as Record<string, any>).subDomainPrefix ?? [
'www',
],
hideUnavailableItems: storeConfig.api.hideUnavailableItems,
showSponsored: storeConfig.api.showSponsored,
simulationBehavior: (storeConfig.api as Record<string, any>)
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/utils/buildStoreUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function buildStoreUrl(
storeUrl: string,
subDomainPrefix: string[]
): string {
return storeUrl.replace(
/^(.*:\/\/)?(.*)/,
(_, protocol, restOfUrl) =>
`${protocol || ''}${subDomainPrefix && subDomainPrefix.length > 0 ? `${subDomainPrefix[0]}.` : ''}${restOfUrl}`
)
}

0 comments on commit 3749d01

Please sign in to comment.