colab-router
v0.0.2
Published
Shared page navigation for co:lab sites — moves between pages without a full reload (PJAX today), from the fragments d8e --pjax writes or the pages themselves.
Readme
colab-router
Shared page navigation for sites. A click on a link to another page swaps that page's content in, so the shell around it (nav, footer, a WebGL canvas, a loader that has already run) stays rendered.
How it ships
co:lab sites serve src/ directly with no build step, so node_modules is never reachable from a page.
colab-router therefore ships as a single classic script (colab-router.js, global: ColabRouter) that consumers
vendor into their own src/script/ with a copy step.
Setup
1. Install
npm install --save-dev colab-router2. Vendor the script
Add to the consumer's package.json scripts (postinstall keeps the vendored copy in sync with the installed
version):
"sync-router": "cp node_modules/colab-router/colab-router.js src/script/colab-router.js",
"postinstall": "npm run sync-router"Commit src/script/colab-router.js — local dev serves src/ as-is, so the file must exist in the tree, and d8e
minifies/hashes it on build like any other script.
3. Build with the fragments
"build": "colab-partial --check && d8e --pjax" (and the same for heroku-postbuild). d8e writes a <page>.json
beside every page: its <title> and its <main>, which is what the router swaps. In dev there is none, and the
router fetches the page itself and takes the same two things from it, so navigation works the same way without a
build.
4. Wire it up
Load it before the app's own scripts, then create the router once:
<script src="./script/colab-router.js"></script>
<script src="./script/app.js"></script>ColabRouter.create({
run: (view, hash) => transition(view, hash),
jump: (hash) => scrollTo(hash),
skip: ['/asset/']
});run(view, hash)— swapview(the next page's<main>, as HTML) into the document, then land onhashif there is one. Returns a promise if the swap animates; the router waits for it before taking the next click.document.titleand the URL are already set when it's called.jump(hash)— a link to an anchor on the page already showing, or to its top (''). A site with its own scroll (a translated#view) scrolls there itself; a plain site can uselocation.hash.skip— path prefixes the router leaves to the browser. A single link opts out withdata-no-router.
Every page's shared markup (nav, footer, the scripts) must be identical, since it is never reloaded — which the
partials already guarantee — and every page sits at the same depth under src/, as the partials require.
What the router owns
Catching clicks on same-origin links (not modified clicks, target="_blank", downloads, data-no-router or a
skip prefix); the URL rule (/a/b.html is /a/b, /index.html is /, as cleanUrls and the nginx config serve
them — ColabRouter.clean(path) is exported for a caller that needs the same rule); one fetch per page, however its
links are written; Back and Forward; a missing page showing the site's 404.html in place; and a full load of the
link if nothing can be fetched. Nothing assumes the site is served at the root.
Tests
npm test — c3b-lint, then test/colab-router.test.js with Node's built-in runner: the script is loaded against
fakes for window, document, history, location, fetch and DOMParser, so each test sees exactly what the
router fetched, pushed and handed to the page.
Deploying to npm
- FIRST TIME ONLY: run
npm login - Bump the version in
package.json - Run
npm test && npm publish
