Introduction

Dojo’s Routing package provides a first class declarative routing solution for web applications. Widgets are the fundamental concept in Dojo applications and with routing it is no different. Dojo Routing provides a collection of widgets that integrate directly into a Dojo application and enable application widgets to be associated with routes without compromising their functionality, reusability or property interface.

FeatureDescription
Multiple History ManagersRouting comes with a collection of history managers depending on the needs of your application
Out-of-the-box Routing WidgetsThere are a selection of out the box routing widgets, such as Link and ActiveLink
Automatic Code SplittingCombined with @dojo/cli-build-app, top level routes are automatically code split

Basic usage

Adding routing to an application

  • Add an initial route configuration that defines a single url path that maps to a route identifier and an outlet name.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home',
  5. outlet: 'main'
  6. },
  7. {
  8. id: 'about',
  9. path: 'about',
  10. outlet: 'main'
  11. },
  12. {
  13. id: 'profile',
  14. path: 'profile',
  15. outlet: 'main'
  16. }
  17. ];
  • Configure the application to be routing-aware by registering the router with an application registry.

src/main.tsx

  1. import renderer, { tsx } from '@dojo/framework/core/vdom';
  2. import Registry from '@dojo/framework/core/Registry';
  3. import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
  4. import routes from './routes';
  5. import App from './App';
  6. const registry = new Registry();
  7. // creates a router with the routes and registers the router with the registry
  8. registerRouterInjector(routes, registry);
  9. const r = renderer(() => <App />);
  10. r.mount({ registry });
  • Add a Route widget to show the text “Home” when the home route is visited. Route is a widget that displays something when the path for the route id is matched. The application’s src/routes.ts file associates a route to an id via the Route‘s id property.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Route from '@dojo/framework/routing/Route';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <Route id="home" renderer={() => <div>Home</div>} />
  8. <Route id="about" renderer={() => <div>About</div>} />
  9. <Route id="profile" renderer={() => <div>Profile</div>} />
  10. </div>
  11. );
  12. });

or using outlets and the Outlet widget, check out the Outlet documentation for more information:

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Outlet from '@dojo/framework/routing/Outlet';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <Outlet id="main">
  8. {{
  9. home: <div>Home</div>,
  10. about: <div>About</div>,
  11. profile: <div>Profile</div>
  12. }}
  13. </Outlet>
  14. </div>
  15. );
  16. });
  • The URL of the route is determined by the path element of the route configuration. In this case, home was specified so the route can be accessed via the URL path /#home.
    • By default the router uses the HashHistory history manager which requires the use of the # before the route path. Other history managers are available to support other history management mechanisms.

Path and query parameters

Path parameters are placeholders in the routing configuration that will match any value for the segment. The parameters are defined using curly braces, for example: {param}.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home/{page}',
  5. outlet: 'home'
  6. }
  7. ];

The parameter values are injected into the matching Route‘s renderer property.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Route from '@dojo/framework/routing/Route';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <Route id="home" renderer={(matchDetails) => <div>{`Home ${matchDetails.params.page}`}</div>} />
  8. </div>
  9. );
  10. });

Query parameters can also be added to route URLs. As with normal query parameters, the first must be prefixed with a ? with additional query parameters delimited by the & character. Note that the route configuration does not change when using query parameters.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home/{page}?{queryOne}&{queryTwo}',
  5. outlet: 'home'
  6. }
  7. ];

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Route from '@dojo/framework/routing/Route';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <Route
  8. id="home"
  9. renderer={(matchDetails) => {
  10. const { queryParams } = matchDetails;
  11. return <div>{`Home ${queryParams.queryOne}-${queryParams.queryTwo}`}</div>;
  12. }}
  13. />
  14. </div>
  15. );
  16. });

If the browser is pointed to the URL path /home/page?queryOne=modern&queryTwo=dojo, then the query parameters are injected into the matching Route‘s renderer method as an object of type MatchDetails and accessed via that object’s queryParams property. Using this URL, the page would show “Hello modern-dojo”. If a query parameter is not provided, then its value will be set to undefined.

Default route and parameters

  • Specify a default route by updating the routing configuration to include defaultRoute: true for the preferred route. The default route is used to redirect the application on initial load if no route has been provided or the requested route has not been registered.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home',
  5. outlet: 'home',
  6. defaultRoute: true
  7. }
  8. ];

If the default route has path or query parameters a map of defaults need to be specified.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home/{page}',
  5. outlet: 'home',
  6. defaultRoute: true,
  7. defaultParams: {
  8. page: 'about'
  9. }
  10. }
  11. ];

Using link widgets

The Link widget is a wrapper around an anchor tag that enables consumers to specify a route id to create a link to. If the generated link requires specific path or query parameters that are not in the route, they can be passed via the params property.

Link Properties:

  • to: string: The route id.
  • params: { [index: string]: string }: Params to generate the link with for the route.
  • onClick: (event: MouseEvent) => void (optional): Function that gets called when the Link is clicked.

In addition to the Link specific properties, all the standard VNodeProperties are available for the Link widget as they would be creating an anchor tag.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import { Link } from '@dojo/framework/routing/Link';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <Link to="home" params={{ foo: 'bar' }}>
  8. Link Text
  9. </Link>
  10. </div>
  11. );
  12. });

The ActiveLink widget is a wrapper around the Link widget that conditionally sets classes on the a node if the link is currently active:

ActiveLink Properties:

  • activeClasses: string[]: An array of classes to apply when the Link‘s route is matched.
  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import { ActiveLink } from '@dojo/framework/routing/ActiveLink';
  3. const factory = create();
  4. export default factory(function App() {
  5. return (
  6. <div>
  7. <ActiveLink to="home" params={{ foo: 'bar' }} activeClasses={['link-active']}>
  8. Link Text
  9. </ActiveLink>
  10. </div>
  11. );
  12. });

Code splitting by route

When using @dojo/cli-build-app, Dojo supports automatic code splitting by default for all top level routes. This means that all widgets referenced within the Routes renderer will include a specific bundle for the route that will be loaded lazily when a user accesses the route.

To take advantage of the code splitting there are 4 rules:

  1. The routing configuration needs to be the default export in the src/routes.ts module.
  2. The widgets must be the default export of their module.
  3. The renderer property must be defined inline.
  4. The id and outlet in the routing config must be static and defined inline.

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home',
  5. outlet: 'home'
  6. },
  7. {
  8. id: 'about',
  9. path: 'about',
  10. outlet: 'about',
  11. children: [
  12. {
  13. id: 'company',
  14. path: 'company',
  15. outlet: 'about-company'
  16. }
  17. ]
  18. },
  19. {
  20. id: 'profile',
  21. path: 'profile',
  22. outlet: 'profile'
  23. },
  24. {
  25. id: 'settings',
  26. path: 'settings',
  27. outlet: 'settings'
  28. }
  29. ];

With the routing configuration above the following example will generate 4 separate bundles for each of the widgets returned in the Route‘s renderer, Home, About, Profile and Settings.

src/App.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Route from '@dojo/framework/routing/Route';
  3. import Home from './Home';
  4. import About from './About';
  5. import Profile from './Profile';
  6. import Settings from './Settings';
  7. const factory = create();
  8. export default factory(function App() {
  9. return (
  10. <div>
  11. <Route id="home" renderer={() => <Home />} />
  12. <Route id="about" renderer={() => <About />} />
  13. <Route id="profile" renderer={() => <Profile />} />
  14. <Route id="settings" renderer={() => <Settings />} />
  15. </div>
  16. );
  17. });