diff --git a/examples/frontend/src/frontend/core.cljs b/examples/frontend/src/frontend/core.cljs index 367ff97d..82731258 100644 --- a/examples/frontend/src/frontend/core.cljs +++ b/examples/frontend/src/frontend/core.cljs @@ -17,7 +17,10 @@ (defn about-page [] [:div [:h2 "About frontend"] - [:a {:href "http://google.com"} "external link"]]) + [:ul + [:li [:a {:href "http://google.com"} "external link"]] + [:li [:a {:href (rfh/href @router ::foobar)} "Missing route"]] + [:li [:a {:href (rfh/href @router ::item)} "Missing route params"]]]]) (defn item-page [match] (let [{:keys [path query]} (:parameters match) diff --git a/modules/reitit-frontend/src/reitit/frontend.cljs b/modules/reitit-frontend/src/reitit/frontend.cljs index b4eb094e..fba8fa3d 100644 --- a/modules/reitit-frontend/src/reitit/frontend.cljs +++ b/modules/reitit-frontend/src/reitit/frontend.cljs @@ -2,6 +2,7 @@ "" (:require [reitit.core :as reitit] [clojure.string :as str] + [clojure.set :as set] [reitit.coercion :as coercion] [goog.events :as e] [goog.dom :as dom]) @@ -38,3 +39,25 @@ (match-by-name router name {})) ([router name path-params] (reitit/match-by-name router name path-params))) + +(defn match-by-name! + "Logs problems using console.warn" + ([router name] + (match-by-name! router name {})) + ([router name path-params] + (if-let [match (reitit/match-by-name router name path-params)] + (if (reitit/partial-match? match) + (if (every? #(contains? path-params %) (:required match)) + match + (let [defined (-> path-params keys set) + missing (set/difference (:required match) defined)] + (js/console.warn + "missing path-params for route" name + {:template (:template match) + :missing missing + :path-params path-params + :required (:required match)}) + nil)) + match) + (do (js/console.warn "missing route" name) + nil)))) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index a95aa617..97c135b1 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -110,7 +110,7 @@ ([state k params] (href state k params nil)) ([{:keys [router history]} k params query] - (let [match (rf/match-by-name router k params) + (let [match (rf/match-by-name! router k params) token (match->token history match k params query)] (token->href history token)))) @@ -118,6 +118,6 @@ ([state k params] (replace-token state k params nil)) ([{:keys [router history]} k params query] - (let [match (rf/match-by-name router k params) + (let [match (rf/match-by-name! router k params) token (match->token history match k params query)] (.replaceToken history token))))