Skip to content

Commit

Permalink
feat: add "no server side rendering" guard to protect routes that sho…
Browse files Browse the repository at this point in the history
…uld not be rendered in SSR (#1277)

* used for all checkout pages that should only be rendered in the users browser
  • Loading branch information
shauke authored Sep 21, 2022
1 parent a6783cc commit e0cbc91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app/core/guards/no-server-side-rendering.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';

/**
* guards a route against server side rendering (e.g. if the logic requires information only available in browser rendering)
*/
@Injectable({ providedIn: 'root' })
export class NoServerSideRenderingGuard implements CanActivate {
constructor(private router: Router) {}

canActivate() {
// prevent any handling in the server side rendering (SSR) and instead show loading
if (SSR) {
return this.router.parseUrl('/loading');
}

// if not in SSR just return true and continue
return true;
}
}
2 changes: 2 additions & 0 deletions src/app/pages/checkout/checkout-page.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { NoServerSideRenderingGuard } from 'ish-core/guards/no-server-side-rendering.guard';
import { SharedModule } from 'ish-shared/shared.module';

import { CheckoutAddressPageModule } from '../checkout-address/checkout-address-page.module';
Expand All @@ -16,6 +17,7 @@ import { CheckoutProgressBarComponent } from './checkout-progress-bar/checkout-p
const checkoutPageRoutes: Routes = [
{
path: '',
canActivate: [NoServerSideRenderingGuard],
component: CheckoutPageComponent,
children: [
{
Expand Down

0 comments on commit e0cbc91

Please sign in to comment.