tree: 2ff9c630b7e2452e3dff93c06559c15f4dbc5e27 [path history] [tgz]
  1. src/
  2. .bower.json
  3. app-router.csp.html
  4. app-router.html
  5. app-router.js
  6. bower.json
  9. preview.png

Router for Web Components

Works with Polymer, X-Tag, and natively.

Manage page state. Lazy-load content. Data-bind path variables and query parameters. Use multiple layouts. Navigate with hashchange and HTML5 pushState. Animate transitions using core-animated-pages.

Download or run bower install app-router --save.


Define how URLs map to pages.

<!doctype html>
    <title>App Router</title>
    <link rel="import" href="/bower_components/app-router/app-router.html">
      <!-- matches an exact path -->
      <app-route path="/home" import="/pages/home-page.html"></app-route>

      <!-- matches using a wildcard -->
      <app-route path="/customer/*" import="/pages/customer-page.html"></app-route>

      <!-- matches using a path variable -->
      <app-route path="/order/:id" import="/pages/order-page.html"></app-route>

      <!-- matches a pattern like '/word/number' -->
      <app-route path="/^\/\w+\/\d+$/i" regex import="/pages/regex-page.html"></app-route>

      <!-- matches everything else -->
      <app-route path="*" import="/pages/not-found-page.html"></app-route>


Click links or call router.go().

<!-- hashchange -->
<a href="#/home">Home</a>

<!-- pushState() -->
<a is="pushstate-anchor" href="/home">Home</a>

<!-- router.go(path, options) -->

The router listens to popstate and hashchange events. Changing the URL will find the first app-route that matches, load the element or template, and replace the current view.


Clicking <a href="#/home">Home</a> will fire a hashchange event and tell the router to load the first route that matches /home. You don't need to handle the event in your Javascript. Hash paths #/home match routes without the hash <app-route path="/home">.


You can use the pushstate-anchor or html5-history-anchor to extend <a> tags with the HTML5 history API.

<a is="pushstate-anchor" href="/home">Home</a>

This will call pushState() and dispatch a popstate event.

go(path, options)

You can call the router from Javascript to navigate imperatively.

// or
document.querySelector('app-router').go('/home', {replace: true});

If you use go(path, options) you should also set the mode to hash or pushstate on the router.

<app-router mode="auto|pushstate|hash">
  <!-- app-routes -->

Data Binding

Path variables and query parameters automatically attach to the element's attributes.

<!-- url -->
<a is="pushstate-anchor" href="/order/123?sort=ascending">Order 123</a>

<!-- route -->
<app-route path="/order/:orderId" import="/pages/order-page.html"></app-route>

<!-- will bind 123 to the page's `orderId` attribute and "ascending" to the `sort` attribute -->
<order-page orderId="123" sort="ascending"></order-page>

If you're using Polymer, you can also bind path variables and query parameters to templates.

<app-route path="/order/:orderId">
    <p>Your order number is {{orderId}}</p>

See it in action here.

<app-route> options

import a custom element

Lazy-load a custom element.

<app-route path="/customer/:customerId" import="/pages/customer-page.html" [element="customer-page"]></app-route>

You only need to set the element attribute if the element's name is different than the file name.

pre-loaded custom element

Include the element="element-name" attribute on the route to use a pre-loaded custom element. This is how you use bundled (vulcanized) custom elements.

  <link rel="import" href="/pages/page-bundle.html">
  <app-route path="/customer/:customerId" element="customer-page"></app-route>

import template

You can import a <template> instead of a custom element. Just include the template attribute.

<app-route path="/example" import="/pages/template-page.html" template></app-route>

inline template

Finally, you can in-line a <template> like this.

<app-route path="/example">
    <p>Inline template FTW!</p>

regular expressions

Include the regex attribute to match on a regular expression. The format is the same as a JavaScript regular expression.

<!-- matches a pattern like '/word/number' -->
<app-route path="/^\/\w+\/\d+$/i" regex import="/pages/regex-page.html"></app-route>


A route can redirect to another path.

<app-router mode="pushstate">
  <app-route path="/home" import="/pages/home-page.html"></app-route>
  <app-route path="*" redirect="/home"></app-route>

When you use redirect you should also set mode="hash|pushstate" on the app-router.

<app-router> options


One set of routes will match regular paths / and hash paths #/. You can force a specific mode with mode="auto|hash|pushstate".

<app-router mode="pushstate">
  <!-- always ignore the hash and match on the path -->

When left in auto, redirects and go(path, options) will use hash paths.

trailing slashes

By default /home and /home/ are treated as separate routes. You can configure the router to ignore trailing slashes with trailingSlash="ignore".

<app-router trailingSlash="ignore">
  <!-- matches '/home' and '/home/' -->
  <app-route path="/home" import="/pages/home-page.html"></app-route>

Demo Site & Example Setup

Check out the app-router in action at

You can download an example setup here to get running locally.

Examples showing app-router and flatiron-director versus no router

Breaking Changes

Check the change log for breaking changes in major versions.

Build, Test, and Debug Build Status

Source files are under the src folder. The build process writes to the root directory. The easiest way to debug is to include the source script rather than the minified HTML import.

<script src="/bower_components/app-router/src/app-router.js"></script>

To build:

  • Run bower install and npm install to install dev dependencies
  • Lint, test, build, and minify code with gulp
  • Manually run functional tests in the browser by starting a static content server (node http-server or python -m SimpleHTTPServer) and open http://localhost:8080/tests/functional-tests/