From f5194d35bf43786910c7aae0def05e21b1fbefc6 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 17 Sep 2017 12:36:31 -0400 Subject: [PATCH] Add automatic/fallback redirection for URLs with missing or incorrect trailing slash --- assets/javascripts/app/router.coffee | 13 +++++++++++-- assets/javascripts/lib/page.coffee | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/app/router.coffee b/assets/javascripts/app/router.coffee index 41870424..38617f12 100644 --- a/assets/javascripts/app/router.coffee +++ b/assets/javascripts/app/router.coffee @@ -66,14 +66,23 @@ class app.Router entry: (context, next) -> doc = app.docs.findBySlug(context.params.doc) + return next() unless doc + path = context.params.path + hash = context.hash - if entry = doc?.findEntryByPathAndHash(context.params.path, context.hash) + if entry = doc.findEntryByPathAndHash(path, hash) context.doc = doc context.entry = entry @triggerRoute 'entry' return + else if path.slice(-6) is '/index' + path = path.substr(0, path.length - 6) + return entry.fullPath() if entry = doc.findEntryByPathAndHash(path, hash) else - return next() + path = "#{path}/index" + return entry.fullPath() if entry = doc.findEntryByPathAndHash(path, hash) + + return next() root: -> return '/' if app.isSingleDoc() diff --git a/assets/javascripts/lib/page.coffee b/assets/javascripts/lib/page.coffee index d8159094..8df7422e 100644 --- a/assets/javascripts/lib/page.coffee +++ b/assets/javascripts/lib/page.coffee @@ -53,7 +53,12 @@ page.replace = (path, state, skipDispatch, init) -> context = new Context(path, state or currentState) context.init = init currentState = context.state - page.dispatch(context) unless skipDispatch + result = page.dispatch(context) unless skipDispatch + if result + context = new Context(result) + context.init = init + currentState = context.state + page.dispatch(context) context.replaceState() updateCanonicalLink() track() unless skipDispatch