History managers

Dojo Routing comes with three history managers for managing an application’s navigation state, HashHistory, StateHistory and MemoryHistory. By default the HashHistory is used, however, this can be overridden by passing a different HistoryManager when creating the Router or using registerRouterInjector.

src/main.ts

  1. import Router from '@dojo/framework/routing/Router';
  2. import StateHistory from '@dojo/framework/routing/history/StateHistory';
  3. import routes from './routes';
  4. // creates a router using the default history manager, `HashHistory`
  5. const router = new Router(routes);
  6. // creates a router using the `StateHistory`
  7. const routerWithHistoryOverride = new Router(routes, { HistoryManager: StateHistory });

Or using the registerRouterInjector helper function:

src/main.ts

  1. import Registry from '@dojo/framework/core/Registry';
  2. import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
  3. import StateHistory from '@dojo/framework/routing/history/StateHistory';
  4. import routes from './routes';
  5. const registry = new Registry();
  6. // creates and registers a router using the default history manager, `HashHistory`
  7. registerRouterInjector(routes, registry);
  8. // creates and registers a router using the `StateHistory`
  9. registerRouterInjector(routes, registry, { HistoryManager: StateHistory });

HashHistory

HashHistory uses the fragment identifier to process route changes, for example https://foo.com/#home would process the home as the route path. As HashHistory is the default manager, you do not need to import the module.

  1. import { Router } from '@dojo/framework/routing/Router';
  2. const router = new Router(config);

StateHistory

StateHistory uses the browser’s history API, to manage application route changes.

The StateHistory manager will require server-side machinery to enable an application to support refreshing on a route, for example:

  1. Re-writing the index.html request to load from the application root.
  2. Re-writing requests to load static resources (.js, .css etc) from the application root.

Note: This machinery is included with @dojo/cli-build-app using the --serve option (intended for development only).

  1. import { Router } from '@dojo/framework/routing/Router';
  2. import { StateHistory } from '@dojo/framework/routing/history/StateHistory';
  3. const router = new Router(config, { HistoryManager: StateHistory });

MemoryHistory

The MemoryHistory does not rely on any browser API but keeps its own internal path state. It should not be used in production applications but is useful for testing application routing.

  1. import { Router } from '@dojo/framework/routing/Router';
  2. import { MemoryHistory } from '@dojo/framework/routing/history/MemoryHistory';
  3. const router = new Router(config, { HistoryManager: MemoryHistory });

src/main.tsx

  1. import renderer from '@dojo/framework/core/vdom';
  2. import { tsx } from '@dojo/framework/core/vdom';
  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 });

These history managers work like adapters, meaning that custom history managers can be implemented by fulfilling the history manager interface.