Important: this is a fork of Karida's original FlowRouter SSR branch. It is a quick fix, it is NOT backward compatible, it will probably not be supported in the future. Use at your own risks. I'm releasing it to fix my own problems :)
To use this you will have to remove everything Kadira related you should have installed to get SSR working:
meteor remove kadira:inject-data
meteor remove kadira:fast-render
meteor remove kadira:flow-router-ssr
Then install this version, which comes bundled with abeck's new meteor 1.3.4 compatible packages:
meteor add bensventures:flow-router-ssr // This will install staringatlights' inject-data and fast-render packages
meteor add bensventures:dochead //for good mesure. You probably want good seo
Last but not least, you need to change the npm modules react-mounter
so it works with this package. Search in the modules dist folder for references to kadira:flow-router-ssr
and replace them with bensventures:flow-router-ssr
.
This version of FlowRouter does NOT support subscription management.
Carefully Designed Client Side Router for Meteor.
FlowRouter is a very simple router for Meteor. It does routing for client-side apps and does not handle rendering itself.
It exposes a great API for changing the URL and reactively getting data from the URL. However, inside the router, it's not reactive. Most importantly, FlowRouter is designed with performance in mind and it focuses on what it does best: routing.
We've released 2.0 and follow this migration guide if you are already using FlowRouter.
- Meteor Routing Guide
- Getting Started
- Routes Definition
- Group Routes
- Rendering and Layout Management
- Triggers
- Not Found Routes
- API
- Subscription Management
- IE9 Support
- Hashbang URLs
- Prefixed paths
- Add-ons
- Difference with Iron Router
- Migrating into 2.0
Meteor Routing Guide is a completed guide into routing and related topics in Meteor. It talks about how to use FlowRouter properly and use it with Blaze and React. It also shows how to manage subscriptions and implement auth logic in the view layer.
Add FlowRouter to your app:
meteor add kadira:flow-router
Let's write our first route (add this file to lib/router.js
):
FlowRouter.route('/blog/:postId', {
action: function(params, queryParams) {
console.log("Yeah! We are on the post:", params.postId);
}
});
Then visit /blog/my-post-id
from the browser or invoke the following command from the browser console:
FlowRouter.go('/blog/my-post-id');
Then you can see some messages printed in the console.
FlowRouter routes are very simple and based on the syntax of path-to-regexp which is used in both Express and iron:router
.
Here's the syntax for a simple route:
FlowRouter.route('/blog/:postId', {
// do some action for this route
action: function(params, queryParams) {
console.log("Params:", params);
console.log("Query Params:", queryParams);
},
name: "<name for the route>" // optional
});
So, this route will be activated when you visit a url like below:
FlowRouter.go('/blog/my-post?comments=on&color=dark');
After you've visit the route, this will be printed in the console:
Params: {postId: "my-post"}
Query Params: {comments: "on", color: "dark"}
For a single interaction, the router only runs once. That means, after you've visit a route, first it will call triggers
, then subscriptions
and finally action
. After that happens, none of those methods will be called again for that route visit.
You can define routes anywhere in the client
directory. But, we recommend to add them in the lib
directory. Then fast-render
can detect subscriptions and send them for you (we'll talk about this is a moment).
You can group routes for better route organization. Here's an example:
var adminRoutes = FlowRouter.group({
prefix: '/admin',
name: 'admin',
triggersEnter: [function(context, redirect) {
console.log('running group triggers');
}]
});
// handling /admin route
adminRoutes.route('/', {
action: function() {
BlazeLayout.render('componentLayout', {content: 'admin'});
},
triggersEnter: [function(context, redirect) {
console.log('running /admin trigger');
}]
});
// handling /admin/posts
adminRoutes.route('/posts', {
action: function() {
BlazeLayout.render('componentLayout', {content: 'posts'});
}
});
All of the options for the FlowRouter.group()
are optional.
You can even have nested group routes as shown below:
var adminRoutes = FlowRouter.group({
prefix: "/admin",
name: "admin"
});
var superAdminRoutes = adminRoutes.group({
prefix: "/super",
name: "superadmin"
});
// handling /admin/super/post
superAdminRoutes.route('/post', {
action: function() {
}
});
You can determine which group the current route is in using:
FlowRouter.current().route.group.name
This can be useful for determining if the current route is in a specific group (e.g. admin, public, loggedIn) without needing to use prefixes if you don't want to. If it's a nested group, you can get the parent group's name with:
FlowRouter.current().route.group.parent.name
As with all current route properties, these are not reactive, but can be combined with FlowRouter.watchPathChange()
to get group names reactively.
FlowRouter does not handle rendering or layout management. For that, you can use:
Then you can invoke the layout manager inside the action
method in the router.
FlowRouter.route('/blog/:postId', {
action: function(params) {
BlazeLayout.render("mainLayout", {area: "blog"});
}
});
Triggers are the way FlowRouter allows you to perform tasks before you enter into a route and after you exit from a route.
Here's how you can define triggers for a route:
FlowRouter.route('/home', {
// calls just before the action
triggersEnter: [trackRouteEntry],
action: function() {
// do something you like
},
// calls when when we decide to move to another route
// but calls before the next route started
triggersExit: [trackRouteClose]
});
function trackRouteEntry(context) {
// context is the output of `FlowRouter.current()`
Mixpanel.track("visit-to-home", context.queryParams);
}
function trackRouteClose(context) {
Mixpanel.track("move-from-home", context.queryParams);
}
This is how you can define triggers on a group definition.
var adminRoutes = FlowRouter.group({
prefix: '/admin',
triggersEnter: [trackRouteEntry],
triggersExit: [trackRouteEntry]
});
You can add triggers to individual routes in the group too.
You can also define triggers globally. Here's how to do it:
FlowRouter.triggers.enter([cb1, cb2]);
FlowRouter.triggers.exit([cb1, cb2]);
// filtering
FlowRouter.triggers.enter([trackRouteEntry], {only: ["home"]});
FlowRouter.triggers.exit([trackRouteExit], {except: ["home"]});
As you can see from the last two examples, you can filter routes using the only
or except
keywords. But, you can't use both only
and except
at once.
If you'd like to learn more about triggers and design decisions, visit here.
You can redirect to a different route using triggers. You can do it from both enter and exit triggers. See how to do it:
FlowRouter.route('/', {
triggersEnter: [function(context, redirect) {
redirect('/some-other-path');
}],
action: function(_params) {
throw new Error("this should not get called");
}
});
Every trigger callback comes with a second argument: a function you can use to redirect to a different route. Redirect also has few properties to make sure it's not blocking the router.
- redirect must be called with an URL
- redirect must be called within the same event loop cycle (no async or called inside a Tracker)
- redirect cannot be called multiple times
Check this PR to learn more about our redirect API.
In some cases, you may need to stop the route callback from firing using triggers. You can do this in before triggers, using the third argument: the stop
function. For example, you can check the prefix and if it fails, show the notFound layout and stop before the action fires.
var localeGroup = FlowRouter.group({
prefix: '/:locale?',
triggersEnter: [localeCheck]
});
localeGroup.route('/login', {
action: function (params, queryParams) {
BlazeLayout.render('componentLayout', {content: 'login'});
}
});
function localeCheck(context, redirect, stop) {
var locale = context.params.locale;
if (locale !== undefined && locale !== 'fr') {
BlazeLayout.render('notFound');
stop();
}
}
Note: When using the stop function, you should always pass the second redirect argument, even if you won't use it.
You can configure Not Found routes like this:
FlowRouter.notFound = {
// Subscriptions registered here don't have Fast Render support.
subscriptions: function() {
},
action: function() {
}
};
FlowRouter has a rich API to help you to navigate the router and reactively get information from the router.
Reactive function which you can use to get a parameter from the URL.
// route def: /apps/:appId
// url: /apps/this-is-my-app
var appId = FlowRouter.getParam("appId");
console.log(appId); // prints "this-is-my-app"
Reactive function which you can use to get a value from the queryString.
// route def: /apps/:appId
// url: /apps/this-is-my-app?show=yes&color=red
var color = FlowRouter.getQueryParam("color");
console.log(color); // prints "red"
Generate a path from a path definition. Both params
and queryParams
are optional.
Special characters in params
and queryParams
will be URL encoded.
var pathDef = "/blog/:cat/:id";
var params = {cat: "met eor", id: "abc"};
var queryParams = {show: "y+e=s", color: "black"};
var path = FlowRouter.path(pathDef, params, queryParams);
console.log(path); // prints "/blog/met%20eor/abc?show=y%2Be%3Ds&color=black"
If there are no params or queryParams, this will simply return the pathDef as it is.
You can also use the route's name instead of the pathDef. Then, FlowRouter will pick the pathDef from the given route. See the following example:
FlowRouter.route("/blog/:cat/:id", {
name: "blogPostRoute",
action: function(params) {
//...
}
})
var params = {cat: "meteor", id: "abc"};
var queryParams = {show: "yes", color: "black"};
var path = FlowRouter.path("blogPostRoute", params, queryParams);
console.log(path); // prints "/blog/meteor/abc?show=yes&color=black"
This will get the path via FlowRouter.path
based on the arguments and re-route to that path.
You can call FlowRouter.go
like this as well:
FlowRouter.go("/blog");
Just like FlowRouter.path
, but gives the absolute url. (Uses Meteor.absoluteUrl
behind the scenes.)
This will change the current params with the newParams and re-route to the new path.
// route def: /apps/:appId
// url: /apps/this-is-my-app?show=yes&color=red
FlowRouter.setParams({appId: "new-id"});
// Then the user will be redirected to the following path
// /apps/new-id?show=yes&color=red
Just like FlowRouter.setParams
, but for queryString params.
To remove a query param set it to null
like below:
FlowRouter.setQueryParams({paramToRemove: null});
To get the name of the route reactively.
Tracker.autorun(function() {
var routeName = FlowRouter.getRouteName();
console.log("Current route name is: ", routeName);
});
Get the current state of the router. This API is not reactive.
If you need to watch the changes in the path simply use FlowRouter.watchPathChange()
.
This gives an object like this:
// route def: /apps/:appId
// url: /apps/this-is-my-app?show=yes&color=red
var current = FlowRouter.current();
console.log(current);
// prints following object
// {
// path: "/apps/this-is-my-app?show=yes&color=red",
// params: {appId: "this-is-my-app"},
// queryParams: {show: "yes", color: "red"}
// route: {pathDef: "/apps/:appId", name: "name-of-the-route"}
// }
Reactively watch the changes in the path. If you need to simply get the params or queryParams use dedicated APIs like FlowRouter.getQueryParam()
.
Tracker.autorun(function() {
FlowRouter.watchPathChange();
var currentContext = FlowRouter.current();
// do anything with the current context
// or anything you wish
});
Normally, all the route changes made via APIs like FlowRouter.go
and FlowRouter.setParams()
add a URL item to the browser history. For example, run the following code:
FlowRouter.setParams({id: "the-id-1"});
FlowRouter.setParams({id: "the-id-2"});
FlowRouter.setParams({id: "the-id-3"});
Now you can hit the back button of your browser two times. This is normal behavior since users may click the back button and expect to see the previous state of the app.
But sometimes, this is not something you want. You don't need to pollute the browser history. Then, you can use the following syntax.
FlowRouter.withReplaceState(function() {
FlowRouter.setParams({id: "the-id-1"});
FlowRouter.setParams({id: "the-id-2"});
FlowRouter.setParams({id: "the-id-3"});
});
Now, there is no item in the browser history. Just like FlowRouter.setParams
, you can use any FlowRouter API inside FlowRouter.withReplaceState
.
We named this function as
withReplaceState
because, replaceState is the underline API used for this functionality. Read more about replace state & the history API.
FlowRouter routes are idempotent. That means, even if you call FlowRouter.go()
to the same URL multiple times, it only activates in the first run. This is also true for directly clicking on paths.
So, if you really need to reload the route, this is the API you want.
By default, FlowRouter initializes the routing process in a Meteor.startup()
callback. This works for most of the apps. But, some apps have custom initializations and FlowRouter needs to initialize after that.
So, that's where FlowRouter.wait()
comes to save you. You need to call it directly inside your JavaScript file. After that, whenever your app is ready call FlowRouter.initialize()
.
eg:-
// file: app.js
FlowRouter.wait();
WhenEverYourAppIsReady(function() {
FlowRouter.initialize();
});
For more information visit issue #180.
This API is specially designed for add-on developers. They can listen for any registered route and add custom functionality to FlowRouter. This works on both server and client alike.
FlowRouter.onRouteRegister(function(route) {
// do anything with the route object
console.log(route);
});
Let's say a user defined a route like this:
FlowRouter.route('/blog/:post', {
name: 'postList',
triggersEnter: [function() {}],
subscriptions: function() {},
action: function() {},
triggersExit: [function() {}],
customField: 'customName'
});
Then the route object will be something like this:
{
pathDef: '/blog/:post',
name: 'postList',
options: {customField: 'customName'}
}
So, it's not the internal route object we are using.
FlowRouter has built in support for Fast Render.
meteor add meteorhacks:fast-render
- Put
router.js
in a shared location. We suggestlib/router.js
.
You can exclude Fast Render support by wrapping the subscription registration in an isClient
block:
FlowRouter.route('/blog/:postId', {
subscriptions: function(params, queryParams) {
// using Fast Render
this.register('myPost', Meteor.subscribe('blogPost', params.postId));
// not using Fast Render
if(Meteor.isClient) {
this.register('data', Meteor.subscribe('bootstrap-data');
}
}
});
FlowRouter has IE9 support. But it does not ship the HTML5 history polyfill out of the box. That's because most apps do not require it.
If you need to support IE9, add the HTML5 history polyfill with the following package.
meteor add tomwasd:history-polyfill
To enable hashbang urls like mydomain.com/#!/mypath
simple set the hashbang
option to true
in the initialize function:
// file: app.js
FlowRouter.wait();
WhenEverYourAppIsReady(function() {
FlowRouter.initialize({hashbang: true});
});
In cases you wish to run multiple web application on the same domain name, you’ll probably want to serve your particular meteor application under a sub-path (eg example.com/myapp
). In this case simply include the path prefix in the meteor ROOT_URL
environment variable and FlowRouter will handle it transparently without any additional configuration.
Router is a base package for an app. Other projects like useraccounts should have support for FlowRouter. Otherwise, it's hard to use FlowRouter in a real project. Now a lot of packages have started to support FlowRouter.
So, you can use your your favorite package with FlowRouter as well. If not, there is an easy process to convert them to FlowRouter.
Add-on API
We have also released a new API to support add-on developers. With that add-on packages can get a notification, when the user created a route in their app.
If you've more ideas for the add-on API, let us know.
FlowRouter and Iron Router are two different routers. Iron Router tries to be a full featured solution. It tries to do everything including routing, subscriptions, rendering and layout management.
FlowRouter is a minimalistic solution focused on routing with UI performance in mind. It exposes APIs for related functionality.