gabrielmoreira / riot-router

Riot Router - A simple routing solution for Riot
MIT License
77 stars 13 forks source link

Routing on server #21

Closed anaibol closed 7 years ago

anaibol commented 8 years ago

Hi! Thanks for making this router. Do you plan to support routing on the server like in isomorphic/universal apps?

Thanks!

gabrielmoreira commented 8 years ago

Well, I did not planned it. But i think it's not too dificult.

Process current route is working, but rendering "route" tag isn't.

Apply this patch on riot-router/lib/router.js for test rendering route tag:

change from:
            this.root.replaceChild(document.createElement(tag), this.root.children[0]);
            this.instance = riot.mount(this.root.children[0], tag, api)[0];
to:
            if (this.root.firstChild) this.root.removeChild(this.root.firstChild);
            this.root.appendChild(document.createElement(tag));
            this.instance = riot.mount(this.root.firstChild, tag, api)[0];

change from:
            riot.router.on('route:updated', this.updateRoute);

to:
            riot.router.on('route:updated', this.updateRoute);
            this.updateRoute();

Test code:

var riot = require('riot'); 
riot.tag('home', '<div>home</div>'); 

var router = require("riot-router/lib/router.js"); 
router.routes([new router.DefaultRoute({tag: 'home'})]); 
router.process("/");  // test any uri

console.log("uri:", router.current.uri);
console.log("match:", router.current.matches[1]);
console.log("html:", riot.render('route'));

Output:

uri: /
match: { route: DefaultRoute { tag: 'home', api: undefined },
  tag: 'home',
  api: undefined,
  found: '' }
html: <route><home riot-tag="home"><div>home</div></home><router-content></router-content></route>

Rendering <router-content></router-content> is wrong, and need some investigation...