From 232e20251decde975560b220a5a796d10e68ad7c Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Mon, 22 May 2017 12:08:31 -0700 Subject: [PATCH 1/3] Better handling for unique routes for React Router routes --- imports/plugins/core/router/lib/router.js | 81 +++++++++++------------ 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/imports/plugins/core/router/lib/router.js b/imports/plugins/core/router/lib/router.js index 17dc70b88e2..a3f9b0bf225 100644 --- a/imports/plugins/core/router/lib/router.js +++ b/imports/plugins/core/router/lib/router.js @@ -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"; @@ -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; @@ -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 @@ -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: { @@ -403,19 +414,10 @@ Router.initPackageRoutes = (options) => { component: indexLayout.component, structure: indexLayout.structure } - }; - - reactRouterRoutes.push( - - ); + }); - const notFoundLayout = ReactionLayout({ template: "notFound" }); - const notFoundRoute = { + // Not found route + routeDefinitions.push({ route: "/not-found", name: "not-found", options: { @@ -424,18 +426,7 @@ Router.initPackageRoutes = (options) => { component: notFoundLayout.component, structure: notFoundLayout.structure } - }; - - reactRouterRoutes.push( - - ); - - Router.routes.push(indexRoute); - Router.routes.push(notFoundRoute); + }); // get package registry route configurations for (const pkg of pkgs) { @@ -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 = {}; @@ -500,24 +489,30 @@ Router.initPackageRoutes = (options) => { route.route = `${prefix}${route.route}`; } - // Add the route to the routing table - reactRouterRoutes.push( - - ); - - 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 be then have a + // higher precidence. Any duplicates after the first instance will be removed. + const uniqRoutes = uniqBy(routeDefinitions.reverse(), "route"); + const reactRouterRoutes = uniqRoutes.map((route, index) => { + return ( + + ); + }); + Router._initialized = true; Router.reactComponents = reactRouterRoutes; - Router._routes = Router.routes; + Router._routes = uniqRoutes; routerReadyDependency.changed(); } From f49dd4da3f3770f169c7654296aa86ade2b41e08 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Tue, 23 May 2017 11:16:37 -0700 Subject: [PATCH 2/3] Added TODO comments --- imports/plugins/core/router/lib/router.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imports/plugins/core/router/lib/router.js b/imports/plugins/core/router/lib/router.js index a3f9b0bf225..77af7264960 100644 --- a/imports/plugins/core/router/lib/router.js +++ b/imports/plugins/core/router/lib/router.js @@ -498,6 +498,9 @@ Router.initPackageRoutes = (options) => { // Take all route definitions in the order that were received, and reverse it. // Routes defined later, like in the case of custom routes will be then have a // higher precidence. 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 ( From 1f8d3d39096781e53f7ee9df951eb2b8bbeaa95a Mon Sep 17 00:00:00 2001 From: Aaron Judd Date: Thu, 25 May 2017 21:58:26 -0700 Subject: [PATCH 3/3] Update router.js --- imports/plugins/core/router/lib/router.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imports/plugins/core/router/lib/router.js b/imports/plugins/core/router/lib/router.js index 77af7264960..e692573417b 100644 --- a/imports/plugins/core/router/lib/router.js +++ b/imports/plugins/core/router/lib/router.js @@ -496,8 +496,8 @@ Router.initPackageRoutes = (options) => { // 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 be then have a - // higher precidence. Any duplicates after the first instance will be removed. + // 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