Skip to content

Commit

Permalink
Fallback routes (#685)
Browse files Browse the repository at this point in the history
* Adds a new type of route "fallback" that is a combination of a regular route that gets normal pages and a dynamic route that fetches one dynamic page. This allows the creation of static pages to replace dynamic pages, and also when those static pages are removed they are replaced by a dynamic page again.

Co-authored-by: Jeff Pope <[email protected]>
  • Loading branch information
james-owen and jpope19 authored Sep 14, 2020
1 parent bd3192b commit a9f1b6b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/services/attachRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function attachDynamicRoute(router, { path, dynamicPage }) {
return router.get(path, render.renderDynamicRoute(dynamicPage));
}

/**
* Attaches a route that attempts to do a plain get and then if no uri is found
* gets a fallback dynamic page instead
* @param {Router} router
* @param {Object} route
* @param {String} route.path
* @param {String} route.fallback
* @return {Router}
*/
function attachFallbackRoute(router, { path, fallback }) {
return router.get(path, render, render.renderDynamicRoute(fallback));
}

/**
* Parses site route config object.
* @param {Router} router
Expand All @@ -89,7 +102,7 @@ function attachDynamicRoute(router, { path, dynamicPage }) {
* @return {Router}
*/
function parseHandler(router, routeObj, site) {
const { redirect, dynamicPage, path, middleware } = routeObj;
const { redirect, dynamicPage, fallback, path, middleware } = routeObj;

if (!validPath(path, site)) {
return;
Expand All @@ -103,6 +116,8 @@ function parseHandler(router, routeObj, site) {
return attachRedirect(router, routeObj, site);
} else if (dynamicPage) {
return attachDynamicRoute(router, routeObj); // Pass in the page ID
} else if (fallback) {
return attachFallbackRoute(router, routeObj); // Does normal route then dynamic as a fallback
} else {
return attachPlainRoute(router, routeObj);
}
Expand Down
1 change: 1 addition & 0 deletions lib/services/attachRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe(_.startCase(filename), function () {
{ path: '/section/' },
{ path: '/section', redirect: '/section/' },
{ path: '/news/:tag', dynamicPage: 'somePageId' },
{ path: '/tags/:tag', fallback: 'tags'},
{ path: '/example', middleware: [() => {}] }
],
siteConfigMock = {};
Expand Down

0 comments on commit a9f1b6b

Please sign in to comment.