Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Added: better support of react-router routes (IndexRoute and trailing…
Browse files Browse the repository at this point in the history
… slashes)
  • Loading branch information
MoOx committed Feb 23, 2017
1 parent 7fc3ad2 commit c6e3834
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/static/routes-to-urls/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ flow

import React from "react"
import { Route } from "react-router"
import { Route, IndexRoute } from "react-router"

import routesToUrls from "../index.js"

Expand Down Expand Up @@ -56,10 +56,13 @@ const collection: PhenomicCollection = [

const routes = (
<Route component={ Noop }>
<Route path="/author/:author" component={ Noop } />
<Route path="/author">
<IndexRoute component={ Noop } />
<Route path=":author" component={ Noop } />
</Route>
<Route path="blog" component={ Noop }>
<Route path="category/:category" component={ Noop } />
<Route path="/tag/:tag" component={ Noop } />
<Route path="/tag/:tag/" component={ Noop } />
</Route>
<Route path="key/:key" component={ Noop } />
<Route path="*" component={ Noop } />
Expand All @@ -75,6 +78,7 @@ it("generate a list of routes based on tags", () => {
urls
).toEqual(
[
"/author",
"/author/Jack",
"/author/James",
"/author/John",
Expand Down
19 changes: 14 additions & 5 deletions src/static/routes-to-urls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import arrayUnique from "../../_utils/array-unique"
const defaultConsole = console.log

const flattenRoute = (route) => {
const root = route.path ? route.path : ""
// @todo remove the default route.path, user should use IndexRoute instead?
let routesUrls = route.path ? [ route.path ] : []

if (route.indexRoute) {
routesUrls.push(route.path)
}
if (route.childRoutes) {
const root = route.path
route.childRoutes.forEach((route) => {
routesUrls = [
...routesUrls,
...flattenRoute(route).map((r) => urlJoin(root, r)),
...flattenRoute(route).map((r) => root ? urlJoin(root, r) : r),
]
})
}
Expand Down Expand Up @@ -153,8 +157,9 @@ export default (
// for testing
log: Function = defaultConsole,
): Array<string> => {
const flattenedRoutes = createRoutes(routes)
.reduce((acc, r) => [ ...acc, ...flattenRoute(r) ], [])
const flattenedRoutes = arrayUnique(
createRoutes(routes).reduce((acc, r) => [ ...acc, ...flattenRoute(r) ], [])
)

if (flattenedRoutes.filter((url) => url.indexOf("*") > -1).length > 1) {
throw new Error(
Expand All @@ -165,5 +170,9 @@ export default (
)
}

return hydrateRoutesUrls(flattenedRoutes, collection, log)
const normalizedRoutes = flattenedRoutes.map(
(route) => "/" + route.replace(/^\/+/, "").replace(/\/+$/, "")
)

return hydrateRoutesUrls(normalizedRoutes, collection, log)
}

0 comments on commit c6e3834

Please sign in to comment.