Skip to content

Commit

Permalink
fix: adapt 'addGlobalGuard' function to work with functional guards (#…
Browse files Browse the repository at this point in the history
…1425)

* needed to fix Hybrid Approach to successfully register 'hybridRedirectGuard'
* add code documentation for Hybrid Approach route guard and migration notes
  • Loading branch information
shauke authored May 3, 2023
1 parent 1a1b6cf commit 2bf9828
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/guides/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ kb_sync_latest_only

The Intershop PWA now uses Node.js 18.16.0 LTS with the corresponding npm version 9.5.1 to resolve an issue with Azure docker deployments (see #1416).

As a leftover adaption regarding the switch from deprecated class-based route guards in favor of functional guards the `addGlobalGuard` function was adapted to actually work with functional guards.
If the `addGlobalGuard` function is used for customizations make sure it now works as expected.

## 3.3 to 4.0

The Intershop PWA now uses Node.js 18.15.0 LTS with the corresponding npm version 9.5.0 and the `"lockfileVersion": 3,`.
Expand Down
3 changes: 3 additions & 0 deletions src/app/core/guards/hybrid-redirect.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { getICMWebURL } from 'ish-core/store/hybrid';

import { HYBRID_MAPPING_TABLE } from '../../../hybrid/default-url-mapping-table';

/**
* guard that handles the Hybrid Approach functionality on the browser side using the HYBRID_MAPPING_TABLE configuration
*/
export function hybridRedirectGuard(_: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return checkRedirect(state.url);
}
Expand Down
1 change: 1 addition & 0 deletions src/app/core/store/hybrid/hybrid-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { HybridEffects, SSR_HYBRID_STATE } from './hybrid.effects';
})
export class HybridStoreModule {
constructor(router: Router, transferState: TransferState) {
// enable the Hybrid Approach handling for the browser side if Hybrid Approach is configured
if (!SSR && transferState.get(SSR_HYBRID_STATE, false)) {
addGlobalGuard(router, hybridRedirectGuard);
}
Expand Down
15 changes: 11 additions & 4 deletions src/app/core/utils/routing.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { CanActivateFn, Router } from '@angular/router';
import { CanActivateChildFn, CanActivateFn, Router } from '@angular/router';

/**
* function to add a functional guard to all routes
*
* @param router The router
* @param guard The functional guard
* @param config An optional configuration to control whether the guard should be added for 'canActivate' or 'canActivateChild' routes (default is for both)
*/
export function addGlobalGuard(
router: Router,
guard: CanActivateFn,
guard: CanActivateFn | CanActivateChildFn,
config: { canActivate: boolean; canActivateChild: boolean } = { canActivate: true, canActivateChild: true }
) {
router.config.forEach(route => {
if (config.canActivate && guard.prototype.canActivate) {
if (config.canActivate) {
if (route.canActivate) {
route.canActivate.push(guard);
} else {
route.canActivate = [guard];
}
}
if (config.canActivateChild && guard.prototype.canActivateChild) {
if (config.canActivateChild) {
if (route.canActivateChild) {
route.canActivateChild.push(guard);
} else {
Expand Down

0 comments on commit 2bf9828

Please sign in to comment.