Routing and state management in the JSS Angular sample app
Version:
The sample app uses dynamic routing based on the Layout Service (or local route data files in disconnected mode) and uses route/navigation changes to trigger app state changes. Tracing the primary execution flow must begin with the route configuration.
Client-side routing
Client-side routing
Client-side routing occurs in the web browser. The following describes the client-side routing flow:
- A route change (or initial load) triggers Angular routing, configured inÂ
src/app/routing/routing.module.ts. - Most URLs fall through to the JSS catch-all route, which is needed because the local application is not aware of all the potential routes (items/pages) configured in Sitecore.
- The catch-all route is configured with a matcher rather than a path, specifically theÂ
jssRouteMatcher function. This function uses theÂJssRouteBuilderService to attempt to parse the URL as a Sitecore-formatted route,/[language]/then/the/item/path/. - TheÂ
jssRouteMatcher function returns the language andÂserverRoute parameters, which are then provided to the configured route resolver,ÂJssRouteResolver. JssRouteResolver first invokesÂchangeRoute on theÂJssContextService.JssContextService on the client-side retrieves the route from one of the following:- If running in Integrated or Headless server-side rendering (SSR) mode immediately following a server render, an attempt is made to obtain route data from theÂ
TransferState, which must contain the server-side rendering state. When this occurs, the HTTP call to layout service is skipped. - In all other cases, the route data is fetched through an HTTP call to the Sitecore Layout Service.
- If running in Integrated or Headless server-side rendering (SSR) mode immediately following a server render, an attempt is made to obtain route data from theÂ
- The
JssContextService retains the current route state, and returns it to theÂJssRouteResolver. The route resolver performs routines necessary for Experience Editor support. - Finally, the configured route component (defined inÂ
routing.module.ts, defaults toÂapp/routing/layout/layout.component.ts) is loaded into the router-outlet defined inÂsrc/app/app.component.ts and is provided with the data from the route resolver. This layout component is responsible for:- Handling the UI for data fetching errors, such as HTTP 404s and 500s.
- Updating route-level states, such as the page title or other meta fields.
- Updating route-level states, such as the page title or other meta fields.
Server-side routing and data transfer
Server-side routing and data transfer
When the Angular app is prerendered by a Node server, it returns HTML to the client in the initial response. This means that the route data flow is similar to the client-side routing but has a few key differences.
The following describes how the server-side route data flow differs from the client-side routing:
- The first step varies based on application mode:
- In integrated mode only, Sitecore receives the request, parses the route server-side, and determines whether the requested item is handled by a JSS application, and which bundle to run.
- In headless mode only, the Node SSR proxy receives a request and passes it on to a Sitecore layout service.
- The Node host invokes theÂ
renderView function in theÂserver.bundle.ts artifact. The function arguments include the route data/Layout Service output. - TheÂ
renderView function uses Angular SSR to render the application, with two key differences in its module initialization:- The app uses the
AppServerModule that provides server-specific implementations of some services. - The initial route state is injected through dependency injection using aÂ
JSS_SERVER_TO_SSRÂ injection token.
- The app uses the
- Routing executes server-side and invokes the server-side implementation ofÂ
JssService. - TheÂ
JssServerService returns the route data from theÂJSS_SERVER_TO_SSR injection token and also places the data inÂTransferState for reading on the client-side. - Route rendering continues as it does on the client.
If you have suggestions for improving this article, let us know!