I'm a junior full-stack developer from Alexandria, Egypt, and one of the bigger projects on my GitHub is an e-commerce storefront built with Angular 17 and server-side rendering. To be clear about scope up front: I did not build the backend. The app consumes RouteMisr's external e-commerce API for products, brands, categories, cart, wishlist, orders and auth. My job was everything on the client: the Angular app, the SSR setup, the routing, the guards, the UI.

That turned out to be more than enough work. This post is about the parts of Angular SSR that actually bit me, with the real code. The repo is public: github.com/ZiadtahaM/ecommerce-fullstack-app

Why SSR for a storefront

A storefront lives or dies on its product and category pages. With a pure client-rendered Angular app, the first response is an empty shell and a big JavaScript bundle. With SSR (Angular 17 ships @angular/ssr with an Express server), the server renders the page to HTML first, so the user sees real content immediately and the page exists as markup before any JavaScript runs.

What I didn't expect: SSR changes the rules for code you've written a hundred times. Anything that touches the browser, localStorage, window, document, now runs in two environments, and one of them doesn't have those things.

The localStorage trap, in my own guards

My app has two route guards. One protects the shop pages (authGuard), one protects the login/register pages (aynelaGuard, sends you to /home if you're already logged in). Both need to read the JWT from localStorage. Both had to learn that the server has no localStorage.

Here's the real authGuard from the repo:

import { isPlatformBrowser } from '@angular/common'; import { inject, PLATFORM_ID } from '@angular/core'; import { CanActivateFn, Router } from '@angular/router'; export const authGuard: CanActivateFn = (route, state) => { const _Router = inject(Router); const _PLATFORM_ID = inject(PLATFORM_ID); if (isPlatformBrowser(_PLATFORM_ID)) { if (localStorage.getItem('usertoken') !== null) { return true; } else { _Router.navigate(['/login']); return false; } } else { return false } }; 

Two things are going on here:

  1. isPlatformBrowser(PLATFORM_ID) is how you ask "am I in a browser right now?" Without that check, the guard crashes on the server with localStorage is not defined.
  2. Notice the else { return false }. On the server, this guard always refuses. That's a deliberate trade: the server-rendered HTML for a protected page is effectively "nothing yet", and the real check happens once the app hydrates in the browser where the token actually lives. Your auth model decides your rendering model. A token in localStorage is invisible to the server by definition.

The funny part: my other guard solves the same problem a different way, with typeof localStorage !== 'undefined' instead of isPlatformBrowser. Both work. Having both in one codebase is a small honest artifact of learning the pattern while building: I wrote one, hit the error, learned the idiomatic version, and never went back to unify them.

Two layouts, one app

The routing is split into two shells:

export const routes: Routes = [ { path: '', component: AuthLayoutComponent, canActivate: [aynelaGuard], children: [ { path: '', redirectTo: 'login', pathMatch: 'full' }, { path: 'login', component: LoginComponent }, { path: 'register', component: RegisterComponent }, ]}, { path: '', component: BlankLayoutComponent, canActivate: [authGuard], children: [ { path: '', redirectTo: 'home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'products', component: ProductsComponent }, { path: 'brands', component: BrandsComponent }, { path: 'categories', component: CategoriesComponent }, { path: 'cart', component: CartComponent }, { path: 'wish-list', component: WishListComponent }, { path: 'details/:id', component: DetailsComponent }, { path: 'orders/:id', component: OrdersComponent }, { path: 'allorders', component: AllordersComponent }, // ... ]}, { path: '**', component: NotfoundComponent } ]; 

Two empty paths, two layouts, each with its own guard. The auth pages get one header/footer, the shop gets another, and the guards decide which shell you're even allowed to see. Simple once you see it, but it took me a while to realize path: '' can appear twice as long as the components differ.

The rest of the stack, briefly

  • UI: Angular Material + PrimeNG + Flowbite, with ngx-owl-carousel-o for product carousels. In hindsight three component libraries is too many; that's what tutorials do to you.
  • Auth token: the API returns a JWT, stored in localStorage, read client-side with jwt-decode.
  • Forms: reactive forms with @rxweb/reactive-form-validators for the validation rules.

What I'd do differently now

  • One UI library, not three. The bundle and the CSS conflicts aren't worth it.
  • HttpOnly cookies or an interceptor-first design for the token, so SSR can participate in auth instead of being locked out of it.
  • Tests. app.component.spec.ts exists; that is nearly the whole test suite, and I know it.
  • Unify the two guard patterns into one isPlatformBrowser helper.

If you're hiring or need a frontend built

I'm available for freelance work: Angular frontends, NestJS APIs, or fixing the SSR bug that's been ruining your week. I'm junior and I price accordingly, but everything above is real code I wrote and can walk you through.

Thanks for reading. If you've fought the localStorage is not defined battle on the server, you have my sympathy.