-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support running ICM and PWA in hybrid mode
- Loading branch information
Showing
12 changed files
with
321 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, CanActivateChild, RouterStateSnapshot } from '@angular/router'; | ||
import { Store, select } from '@ngrx/store'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { getICMWebURL } from 'ish-core/store/hybrid'; | ||
|
||
import { HYBRID_MAPPING_TABLE } from '../../../hybrid/default-url-mapping-table'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class HybridRedirectGuard implements CanActivate, CanActivateChild { | ||
constructor(private store$: Store<{}>) {} | ||
|
||
private checkRedirect(url: string): boolean | Observable<boolean> { | ||
return this.store$.pipe( | ||
select(getICMWebURL), | ||
map(icmWebUrl => { | ||
for (const entry of HYBRID_MAPPING_TABLE) { | ||
if (entry.handledBy === 'pwa') { | ||
continue; | ||
} | ||
const regex = new RegExp(entry.pwa); | ||
if (regex.exec(url)) { | ||
const newUrl = url.replace(regex, `${icmWebUrl}/${entry.icmBuild}`); | ||
location.assign(newUrl); | ||
return false; | ||
} | ||
} | ||
return true; | ||
}) | ||
); | ||
} | ||
|
||
canActivate(_, state: RouterStateSnapshot) { | ||
return this.checkRedirect(state.url); | ||
} | ||
|
||
canActivateChild(_, state: RouterStateSnapshot) { | ||
return this.checkRedirect(state.url); | ||
} | ||
} |
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,21 @@ | ||
import { isPlatformBrowser } from '@angular/common'; | ||
import { Inject, NgModule, PLATFORM_ID } from '@angular/core'; | ||
import { TransferState } from '@angular/platform-browser'; | ||
import { Router } from '@angular/router'; | ||
import { EffectsModule } from '@ngrx/effects'; | ||
|
||
import { HybridRedirectGuard } from 'ish-core/guards/hybrid-redirect.guard'; | ||
import { addGlobalGuard } from 'ish-core/utils/routing'; | ||
|
||
import { HybridEffects, SSR_HYBRID_STATE } from './hybrid.effects'; | ||
|
||
@NgModule({ | ||
imports: [EffectsModule.forFeature([HybridEffects])], | ||
}) | ||
export class HybridStoreModule { | ||
constructor(router: Router, transferState: TransferState, @Inject(PLATFORM_ID) platformId: string) { | ||
if (isPlatformBrowser(platformId) && transferState.get(SSR_HYBRID_STATE, false)) { | ||
addGlobalGuard(router, HybridRedirectGuard); | ||
} | ||
} | ||
} |
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,24 @@ | ||
import { isPlatformServer } from '@angular/common'; | ||
import { Inject, Optional, PLATFORM_ID } from '@angular/core'; | ||
import { TransferState, makeStateKey } from '@angular/platform-browser'; | ||
import { Actions, Effect } from '@ngrx/effects'; | ||
import { filter, take, tap } from 'rxjs/operators'; | ||
|
||
export const SSR_HYBRID_STATE = makeStateKey<boolean>('ssrHybrid'); | ||
|
||
export class HybridEffects { | ||
constructor( | ||
private actions: Actions, | ||
@Inject(PLATFORM_ID) private platformId: string, | ||
private transferState: TransferState, | ||
@Optional() @Inject('SSR_HYBRID') private ssrHybridState: boolean | ||
) {} | ||
|
||
@Effect({ dispatch: false }) | ||
propagateSSRHybridPropToTransferState$ = this.actions.pipe( | ||
take(1), | ||
filter(() => isPlatformServer(this.platformId)), | ||
filter(() => !!this.ssrHybridState), | ||
tap(() => this.transferState.set(SSR_HYBRID_STATE, true)) | ||
); | ||
} |
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,17 @@ | ||
import { createSelector } from '@ngrx/store'; | ||
|
||
import { getConfigurationState, getICMApplication } from 'ish-core/store/configuration'; | ||
import { getCurrentLocale } from 'ish-core/store/locale'; | ||
|
||
import { ICM_WEB_URL } from '../../../../hybrid/default-url-mapping-table'; | ||
|
||
export const getICMWebURL = createSelector( | ||
getConfigurationState, | ||
getCurrentLocale, | ||
getICMApplication, | ||
(state, locale, application) => | ||
ICM_WEB_URL.replace('$<channel>', state.channel) | ||
.replace('$<lang>', locale.lang) | ||
.replace('$<application>', application) | ||
.replace('$<currency>', locale.currency) | ||
); |
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,3 @@ | ||
// tslint:disable no-barrel-files | ||
// API to access ngrx hybrid state | ||
export * from './hybrid.selectors'; |
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,35 @@ | ||
import { HYBRID_MAPPING_TABLE, ICM_WEB_URL } from './default-url-mapping-table'; | ||
|
||
describe('Default Url Mapping Table', () => { | ||
describe('ICM_WEB_URL', () => { | ||
it('should only contain placeholders for supported properties', () => { | ||
const supported = ['channel', 'lang', 'application', 'currency']; | ||
const allReplaced = supported.reduce((acc, val) => acc.replace(`\$<${val}>`, 'something'), ICM_WEB_URL); | ||
expect(allReplaced).not.toMatch(/\$<.*?>/); | ||
}); | ||
}); | ||
|
||
describe('HYBRID_MAPPING_TABLE', () => { | ||
it.each(HYBRID_MAPPING_TABLE.map(e => e.icm))(`{icm: '%p'} should be a valid regex`, entry => { | ||
expect(() => new RegExp(entry)).not.toThrow(); | ||
}); | ||
|
||
it.each(HYBRID_MAPPING_TABLE.map(e => e.pwa))(`{pwa: '%p'} should be a valid regex`, entry => { | ||
expect(() => new RegExp(entry)).not.toThrow(); | ||
}); | ||
|
||
it.each(HYBRID_MAPPING_TABLE.map(e => e.pwa))( | ||
`{pwa: '%p'} should not use named capture groups due to browser compatibility`, | ||
entry => { | ||
expect(entry).not.toMatch(/\(\?<.*?>.*?\)/); | ||
} | ||
); | ||
|
||
it.each(HYBRID_MAPPING_TABLE.map(e => e.icmBuild))( | ||
`{icmBuild: '%p'} should not use named capture group replacements due to browser compatibility`, | ||
entry => { | ||
expect(entry).not.toMatch(/\$<.*?>/); | ||
} | ||
); | ||
}); | ||
}); |
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,98 @@ | ||
const ICM_CONFIG_MATCH = `^/INTERSHOP/web/WFS/(?<channel>[\\w-]+)/(?<lang>[\\w-]+)/(?<application>[\\w-]+)/[\\w-]+`; | ||
const PWA_CONFIG_BUILD = ';channel=$<channel>;lang=$<lang>;application=$<application>;redirect=1'; | ||
|
||
export interface HybridMappingEntry { | ||
/** ID for grouping */ | ||
id: string; | ||
/** regex for detecting ICM URL */ | ||
icm: string; | ||
/** regex for building PWA URL */ | ||
pwaBuild: string; | ||
/** regex for detecting PWA URL */ | ||
pwa: string; | ||
/** regex for building ICM URL (w/o web url) */ | ||
icmBuild: string; | ||
/** handler */ | ||
handledBy: 'icm' | 'pwa'; | ||
} | ||
|
||
/** | ||
* base for generating ICM URLs. | ||
* | ||
* usable variables: | ||
* - channel | ||
* - lang | ||
* - application | ||
* - currency | ||
*/ | ||
export const ICM_WEB_URL = '/INTERSHOP/web/WFS/$<channel>/$<lang>/$<application>/$<currency>'; | ||
|
||
/** | ||
* Mapping table for running PWA and ICM in parallel | ||
*/ | ||
export const HYBRID_MAPPING_TABLE: HybridMappingEntry[] = [ | ||
{ | ||
id: 'Home', | ||
icm: `${ICM_CONFIG_MATCH}/(Default-Start|ViewHomepage-Start).*$`, | ||
pwaBuild: `home${PWA_CONFIG_BUILD}`, | ||
pwa: `^/home.*$`, | ||
icmBuild: `ViewHomepage-Start`, | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Product Detail Page', | ||
icm: `${ICM_CONFIG_MATCH}/ViewProduct-Start.*(\\?|&)SKU=(?<sku>[\\w-]+).*$`, | ||
pwaBuild: `product/$<sku>${PWA_CONFIG_BUILD}`, | ||
pwa: `^.*/product/([\\w-]+).*$`, | ||
icmBuild: `ViewProduct-Start?SKU=$1`, | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Category Page', | ||
icm: `${ICM_CONFIG_MATCH}/ViewStandardCatalog-Browse.*(\\?|&)CatalogID=(?<catalog>[\\w-]+).*$`, | ||
pwaBuild: `category/$<catalog>${PWA_CONFIG_BUILD}`, | ||
pwa: `^.*/category/([\\w-]+).*$`, | ||
icmBuild: `ViewStandardCatalog?CatalogID=$1&CategoryName=$1`, | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Shopping Basket', | ||
icm: `${ICM_CONFIG_MATCH}/.*ViewCart-View$`, | ||
pwaBuild: `basket${PWA_CONFIG_BUILD}`, | ||
pwa: '^/basket.*$', | ||
icmBuild: 'ViewCart-View', | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Login', | ||
icm: `${ICM_CONFIG_MATCH}/ViewUserAccount-ShowLogin.*$`, | ||
pwaBuild: `login${PWA_CONFIG_BUILD}`, | ||
pwa: '^/login.*$', | ||
icmBuild: 'ViewUserAccount-ShowLogin', | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Password Reset', | ||
icm: `${ICM_CONFIG_MATCH}/ViewForgotLoginData-NewPassword\\?uid=(?<uid>[^&]+)&Hash=(?<hash>[0-9a-f-]+).*$`, | ||
pwaBuild: `forgotPassword/updatePassword?uid=$<uid>&Hash=$<hash>${PWA_CONFIG_BUILD}`, | ||
pwa: `^/forgotPassword/updatePassword?uid=([^&]+)&Hash=([0-9a-f-]+).*$`, | ||
icmBuild: 'ViewForgotLoginData-NewPassword\\?uid=$1&Hash=$2', | ||
handledBy: 'pwa', | ||
}, | ||
{ | ||
id: 'Content Pages', | ||
icm: `${ICM_CONFIG_MATCH}/ViewContent-Start\\?PageletEntryPointID=($<id>.*?)(&.*|)$`, | ||
pwaBuild: `page/$<id>${PWA_CONFIG_BUILD}`, | ||
pwa: '^/page/(.*)$', | ||
icmBuild: 'ViewContent-Start?PageletEntryPointID=$1', | ||
handledBy: 'icm', | ||
}, | ||
{ | ||
id: 'My Account', | ||
icm: `${ICM_CONFIG_MATCH}/ViewUserAccount-Start.*$`, | ||
pwaBuild: `account${PWA_CONFIG_BUILD}`, | ||
pwa: '^/account.*$', | ||
icmBuild: 'ViewUserAccount-Start', | ||
handledBy: 'icm', | ||
}, | ||
]; |
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
Oops, something went wrong.