-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3593 from andrzejewsky/bugfix/3576
Refactoring storeCodeFromRoute, resolve store code on SSR
- Loading branch information
Showing
11 changed files
with
108 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { RawLocation } from 'vue-router' | ||
import config from 'config' | ||
import { LocalizedRoute } from './types' | ||
|
||
const getNormalizedPath = (matchedRouteOrUrl) => { | ||
const matchingPath = matchedRouteOrUrl && (matchedRouteOrUrl.path || matchedRouteOrUrl) | ||
|
||
return matchingPath && (matchingPath.length > 0 && matchingPath[0] !== '/') ? `/${matchingPath}` : matchingPath | ||
} | ||
|
||
const getUrl = (matchedRouteOrUrl) => { | ||
const normalizedPath = getNormalizedPath(matchedRouteOrUrl) | ||
|
||
if (matchedRouteOrUrl && typeof matchedRouteOrUrl === 'object') { | ||
if (matchedRouteOrUrl['host']) { | ||
return matchedRouteOrUrl['host'] + normalizedPath | ||
} | ||
|
||
return '' | ||
} | ||
|
||
return matchedRouteOrUrl | ||
} | ||
|
||
const isMatchingByPath = (matchedRouteOrUrl, store) => { | ||
const normalizedPath = getNormalizedPath(matchedRouteOrUrl) | ||
return normalizedPath.startsWith(`${store.url}/`) || normalizedPath === store.url | ||
} | ||
|
||
const isMatchingByDomainAndPath = (matchedRouteOrUrl, store) => { | ||
const url = getUrl(matchedRouteOrUrl) | ||
return url.startsWith(`${store.url}/`) || url === store.url | ||
} | ||
|
||
const storeCodeFromRoute = (matchedRouteOrUrl: LocalizedRoute | RawLocation | string): string => { | ||
if (!matchedRouteOrUrl) return '' | ||
|
||
for (let storeCode of config.storeViews.mapStoreUrlsFor) { | ||
const store = config.storeViews[storeCode] | ||
|
||
if (isMatchingByPath(matchedRouteOrUrl, store) || isMatchingByDomainAndPath(matchedRouteOrUrl, store)) { | ||
return storeCode | ||
} | ||
} | ||
|
||
return '' | ||
} | ||
|
||
export default storeCodeFromRoute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export interface LocalizedRoute { | ||
path?: string, | ||
name?: string, | ||
hash?: string, | ||
params?: { [key: string]: unknown }, | ||
fullPath?: string, | ||
host?: string | ||
} | ||
|
||
export interface StoreView { | ||
storeCode: string, | ||
extend?: string, | ||
disabled?: boolean, | ||
storeId: any, | ||
name?: string, | ||
url?: string, | ||
appendStoreCode?: boolean, | ||
elasticsearch: { | ||
host: string, | ||
index: string | ||
}, | ||
tax: { | ||
sourcePriceIncludesTax: boolean, | ||
defaultCountry: string, | ||
defaultRegion: null | string, | ||
calculateServerSide: boolean, | ||
userGroupId?: number, | ||
useOnlyDefaultUserGroupId: boolean | ||
}, | ||
i18n: { | ||
fullCountryName: string, | ||
fullLanguageName: string, | ||
defaultLanguage: string, | ||
defaultCountry: string, | ||
defaultLocale: string, | ||
currencyCode: string, | ||
currencySign: string, | ||
dateFormat: string | ||
}, | ||
seo: { | ||
defaultTitle: string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters