From 0bd1b94274b0bcb4d3109be3ca4c663a7007e6ca Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 22 Aug 2018 11:01:53 +0300 Subject: [PATCH 1/4] Store uncoerced query-params in frontend match This matches path-params and fragment. --- modules/reitit-frontend/src/reitit/frontend.cljs | 3 ++- test/cljs/reitit/frontend/core_test.cljs | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend.cljs b/modules/reitit-frontend/src/reitit/frontend.cljs index dd29697a..089f4eb6 100644 --- a/modules/reitit-frontend/src/reitit/frontend.cljs +++ b/modules/reitit-frontend/src/reitit/frontend.cljs @@ -21,9 +21,10 @@ (let [uri (.parse Uri path)] (if-let [match (reitit/match-by-path router (.getPath uri))] (let [q (query-params uri) + match (assoc match :query-params q) ;; Return uncoerced values if coercion is not enabled - so ;; that tha parameters are always accessible from same property. - parameters (or (coercion/coerce! (assoc match :query-params q)) + parameters (or (coercion/coerce! match) {:path (:path-params match) :query q})] (assoc match :parameters parameters))))) diff --git a/test/cljs/reitit/frontend/core_test.cljs b/test/cljs/reitit/frontend/core_test.cljs index 03586309..20009088 100644 --- a/test/cljs/reitit/frontend/core_test.cljs +++ b/test/cljs/reitit/frontend/core_test.cljs @@ -20,6 +20,7 @@ {:template "/" :data {:name ::frontpage} :path-params {} + :query-params {} :path "/" :parameters {:query {} :path {}}}) @@ -32,6 +33,7 @@ {:template "/foo" :data {:name ::foo} :path-params {} + :query-params {} :path "/foo" :parameters {:query {} :path {}}}) @@ -59,6 +61,7 @@ (is (= (r/map->Match {:template "/:id" :path-params {:id "5"} + :query-params {} :path "/5" :parameters {:query {} :path {:id 5}}}) @@ -71,7 +74,7 @@ (is (= (r/map->Match {:template "/:id" :path-params {:id "5"} - ;; Note: query not included in path + :query-params {:mode "foo"} :path "/5" :parameters {:path {:id 5} :query {:mode :foo}}}) @@ -84,6 +87,7 @@ (is (= (r/map->Match {:template "/:id" :path-params {:id "5"} + :query-params {:mode "foo"} :path "/5" :parameters {:path {:id 5} :query {:mode :foo}}}) From e42c309144f86d03e6887983d0462800638733dd Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Thu, 23 Aug 2018 09:54:36 +0300 Subject: [PATCH 2/4] Read query string in HTML5 router --- examples/frontend/project.clj | 3 ++- examples/frontend/resources/public/index.html | 2 +- examples/frontend/src/backend/server.clj | 11 +++++++++++ .../reitit-frontend/src/reitit/frontend/history.cljs | 3 ++- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 examples/frontend/src/backend/server.clj diff --git a/examples/frontend/project.clj b/examples/frontend/project.clj index 62d4cfc3..8416bc0a 100644 --- a/examples/frontend/project.clj +++ b/examples/frontend/project.clj @@ -51,4 +51,5 @@ :figwheel {:http-server-root "public" :server-port 3449 - :nrepl-port 7002}) + :nrepl-port 7002 + :ring-handler backend.server/handler}) diff --git a/examples/frontend/resources/public/index.html b/examples/frontend/resources/public/index.html index bbad514c..ce1dd45b 100644 --- a/examples/frontend/resources/public/index.html +++ b/examples/frontend/resources/public/index.html @@ -5,6 +5,6 @@
- + diff --git a/examples/frontend/src/backend/server.clj b/examples/frontend/src/backend/server.clj new file mode 100644 index 00000000..88171cfa --- /dev/null +++ b/examples/frontend/src/backend/server.clj @@ -0,0 +1,11 @@ +(ns backend.server + (:require [clojure.java.io :as io] + [ring.util.response :as resp] + [ring.middleware.content-type :as content-type])) + +(def handler + (-> (fn [request] + (or (resp/resource-response (:uri request) {:root "public"}) + (-> (resp/resource-response "index.html" {:root "public"}) + (resp/content-type "text/html")))) + content-type/wrap-content-type)) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 98d40b0a..96881140 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -96,7 +96,8 @@ (gevents/unlistenByKey listen-key) (gevents/unlistenByKey click-listen-key)) (-get-path [this] - (.. js/window -location -pathname)) + (str (.. js/window -location -pathname) + (.. js/window -location -search))) (-href [this path] path)) From 82ad1fa6cc414d46b71346d7bfd055044dbe1c69 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Thu, 23 Aug 2018 09:55:14 +0300 Subject: [PATCH 3/4] Clean duplicate require --- modules/reitit-frontend/src/reitit/frontend/history.cljs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 96881140..9b59fe0e 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -1,7 +1,6 @@ (ns reitit.frontend.history "" (:require [reitit.core :as reitit] - [goog.events :as e] [reitit.core :as r] [reitit.frontend :as rf] [reitit.impl :as impl] @@ -89,7 +88,7 @@ (-on-navigate this (-get-path this)) (assoc this :listen-key (gevents/listen js/window goog.events.EventType.POPSTATE handler false) - :click-listen-key (e/listen js/document e/EventType.CLICK ignore-anchor-click)))) + :click-listen-key (gevents/listen js/document goog.events.EventType.CLICK ignore-anchor-click)))) (-on-navigate [this path] (on-navigate (rf/match-by-path router path) this)) (-stop [this] From 567edbd07a22d0f6d3f45b11b40e703b2f1fed3e Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Thu, 23 Aug 2018 10:05:10 +0300 Subject: [PATCH 4/4] Serve index.html for all request in html5 routing mode --- examples/frontend-controllers/project.clj | 4 +++- .../frontend-controllers/resources/public/index.html | 2 +- examples/frontend-controllers/src/backend/server.clj | 11 +++++++++++ examples/frontend/project.clj | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 examples/frontend-controllers/src/backend/server.clj diff --git a/examples/frontend-controllers/project.clj b/examples/frontend-controllers/project.clj index feab3f83..e758882b 100644 --- a/examples/frontend-controllers/project.clj +++ b/examples/frontend-controllers/project.clj @@ -49,4 +49,6 @@ :figwheel {:http-server-root "public" :server-port 3449 - :nrepl-port 7002}) + :nrepl-port 7002 + ;; Server index.html for all routes for HTML5 routing + :ring-handler backend.server/handler}) diff --git a/examples/frontend-controllers/resources/public/index.html b/examples/frontend-controllers/resources/public/index.html index bbad514c..ce1dd45b 100644 --- a/examples/frontend-controllers/resources/public/index.html +++ b/examples/frontend-controllers/resources/public/index.html @@ -5,6 +5,6 @@
- + diff --git a/examples/frontend-controllers/src/backend/server.clj b/examples/frontend-controllers/src/backend/server.clj new file mode 100644 index 00000000..88171cfa --- /dev/null +++ b/examples/frontend-controllers/src/backend/server.clj @@ -0,0 +1,11 @@ +(ns backend.server + (:require [clojure.java.io :as io] + [ring.util.response :as resp] + [ring.middleware.content-type :as content-type])) + +(def handler + (-> (fn [request] + (or (resp/resource-response (:uri request) {:root "public"}) + (-> (resp/resource-response "index.html" {:root "public"}) + (resp/content-type "text/html")))) + content-type/wrap-content-type)) diff --git a/examples/frontend/project.clj b/examples/frontend/project.clj index 8416bc0a..4f651748 100644 --- a/examples/frontend/project.clj +++ b/examples/frontend/project.clj @@ -52,4 +52,5 @@ :figwheel {:http-server-root "public" :server-port 3449 :nrepl-port 7002 + ;; Server index.html for all routes for HTML5 routing :ring-handler backend.server/handler})