Sub topic 14
Path vs Hash Location Strategies
This chapter assumes you know how to register routes with provideRouter and how the router maps a URL to a component. Here you choose how those URLs look in the browser's address bar, and what that choice requires from your web server.
What a Location Strategy Does
A location strategy is the part of the Angular Router that reads and writes the browser URL. Angular provides two:
| Strategy | Example URL | How it updates the URL |
|---|---|---|
| `PathLocationStrategy` (default) | `https://shop.example.com/products/42` | HTML5 History API (`pushState`) |
| `HashLocationStrategy` | `https://shop.example.com/#/products/42` | Changes the part after `#` |
Your route definitions, routerLink values, and router.navigate calls are the same under both strategies. Only the URL shown in the address bar changes.
Path Strategy and the Deep-Link Problem
Path URLs are clean and look like normal server pages. The catch shows up when a user reloads or opens a deep link such as /products/42. The browser sends that full path to the server. If the server only knows about index.html, it returns a 404 before Angular can load.
The fix is a server rewrite rule: serve index.html for any path that does not match a real file. In nginx it looks like this:
location / {
try_files $uri $uri/ /index.html;
}The path strategy also depends on the <base href="/"> tag in index.html, which the Angular CLI generates for you. If the app is deployed under a subfolder, set the base href to that folder, for example <base href="/store/">, or build with ng build --base-href /store/.
Switching to the Hash Strategy
Browsers never send the part after # to the server. Every request goes to /, so the server always returns index.html and you need no rewrite rule. That makes the hash strategy useful on static hosts where you can't configure rewrites.
Turn it on with the withHashLocation() feature:
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, withHashLocation } from '@angular/router';
import { AppComponent } from './app.component';
import { routes } from './app.routes';
bootstrapApplication(AppComponent, {
providers: [provideRouter(routes, withHashLocation())]
});Trade-offs and Common Mistakes
- Server-side rendering and SEO. The server never sees hash routes, so it cannot pre-render
/#/products/42. If you use Angular SSR or care about search indexing, use the path strategy. - Fragments. The hash strategy already uses
#for the route, so in-page anchors andfragmentnavigation get awkward and some third-party tools read the URL wrongly. - Forgetting the rewrite rule. The most common production bug with the path strategy is an app that works when you click through it but returns 404 on reload. Check that the fallback is configured before you blame the router.
- Switching later breaks links. Bookmarks and shared links stop working when you change strategy. Pick one early.
Exercise
Your app uses the default strategy and is deployed to an nginx server with no extra configuration. Clicking from Home to /products/7 works, but reloading /products/7 returns a 404. Give two different fixes and name one drawback of each.
Show answer
Fix 1: add a fallback rule such as try_files $uri $uri/ /index.html; so nginx serves index.html for unknown paths. Drawback: you need access to the server configuration, and real missing files can be masked unless the rule is scoped carefully. Fix 2: switch to the hash strategy with provideRouter(routes, withHashLocation()). Drawback: URLs become /#/products/7, which rules out server-side rendering of those routes, hurts SEO, and breaks existing bookmarks.