Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 41 additions & 43 deletions imports/plugins/core/router/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import createMemoryHistory from "history/createMemoryHistory";
import pathToRegexp from "path-to-regexp";
import queryParse from "query-parse";
import Immutable from "immutable";
import { uniqBy } from "lodash";
import { Meteor } from "meteor/meteor";
import Blaze from "meteor/gadicc:blaze-react-component";
import { Tracker } from "meteor/tracker";
Expand Down Expand Up @@ -34,9 +35,16 @@ class Router {
static history = history
static Hooks = Hooks
static routes = []
static _routes = Router.routes // for legacy
static _initialized = false;

static set _routes(value) {
Router.routes = value;
}

static get _routes() {
return Router.routes;
}

static ready() {
routerReadyDependency.depend();
return Router._initialized;
Expand Down Expand Up @@ -382,7 +390,7 @@ Router.initPackageRoutes = (options) => {

const pkgs = Packages.find().fetch();
const prefix = Router.Reaction.getShopPrefix();
const reactRouterRoutes = [];
const routeDefinitions = [];

// prefixing isnt necessary if we only have one shop
// but we need to bypass the current
Expand All @@ -392,9 +400,12 @@ Router.initPackageRoutes = (options) => {
// using tmeasday:publish-counts
const shopCount = Counts.get("shops-count");

// Index layout
// Default layouts
const indexLayout = ReactionLayout(options.indexRoute);
const indexRoute = {
const notFoundLayout = ReactionLayout({ template: "notFound" });

// Index route
routeDefinitions.push({
route: "/",
name: "index",
options: {
Expand All @@ -403,19 +414,10 @@ Router.initPackageRoutes = (options) => {
component: indexLayout.component,
structure: indexLayout.structure
}
};

reactRouterRoutes.push(
<Route
exact={true}
key="index"
path="/"
render={indexLayout.component}
/>
);
});

const notFoundLayout = ReactionLayout({ template: "notFound" });
const notFoundRoute = {
// Not found route
routeDefinitions.push({
route: "/not-found",
name: "not-found",
options: {
Expand All @@ -424,18 +426,7 @@ Router.initPackageRoutes = (options) => {
component: notFoundLayout.component,
structure: notFoundLayout.structure
}
};

reactRouterRoutes.push(
<Route
key="not-found"
path="/not-found"
render={notFoundLayout.component}
/>
);

Router.routes.push(indexRoute);
Router.routes.push(notFoundRoute);
});

// get package registry route configurations
for (const pkg of pkgs) {
Expand Down Expand Up @@ -483,9 +474,7 @@ Router.initPackageRoutes = (options) => {
//
// add group and routes to routing table
//
const uniqRoutes = new Set(newRoutes);
let index = 0;
for (const route of uniqRoutes) {
for (const route of newRoutes) {
// allow overriding of prefix in route definitions
// define an "absolute" url by excluding "/"
route.group = {};
Expand All @@ -500,24 +489,33 @@ Router.initPackageRoutes = (options) => {
route.route = `${prefix}${route.route}`;
}

// Add the route to the routing table
reactRouterRoutes.push(
<Route
key={`${pkg.name}-${route.name}-${index++}`}
path={route.route}
exact={true}
render={route.options.component}
/>
);

Router.routes.push(route);
routeDefinitions.push(route);
}
}
} // end package loop

// Uniq-ify routes
// Take all route definitions in the order that were received, and reverse it.
// Routes defined later, like in the case of custom routes will then have a
// higher precedence. Any duplicates after the first instance will be removed.
//
// TODO: In the future, sort by priority
// TODO: Allow duplicated routes with a prefix / suffix / flag
const uniqRoutes = uniqBy(routeDefinitions.reverse(), "route");
const reactRouterRoutes = uniqRoutes.map((route, index) => {
return (
<Route
key={`${route.name}-${index}`}
path={route.route}
exact={true}
render={route.options.component}
/>
);
});

Router._initialized = true;
Router.reactComponents = reactRouterRoutes;
Router._routes = Router.routes;
Router._routes = uniqRoutes;

routerReadyDependency.changed();
}
Expand Down