url-routingclojurescriptreitit

Why is a pound symbol required in my Reitit routes?


I have been making a personal site using ClojureScript and decided to go with Reitit as my routing library instead of my usual Secretary. I have read really good things about Reitit and have it sort of working, but it only will react when I navigate to a path with '#' in it.

Example: I want to be able to go to "website.com/posts", but it only recognizes when I go to "website.com/#/posts"

My routes.cljs is pretty much a copy of their re-frame example:

(defn href
  ([k]
   (href k nil nil))
  ([k params]
   (href k params nil))
  ([k params query]
   (rfe/href k params query)))


(def routes
  ["/"
   [""
    {:name :home
     :view v/main-panel
     :controllers [{:start (fn [& params] (js/console.log "Home"))
                    :stop (fn [& params] (js/console.log "Leaving Home"))}]}]
   ["posts"
    {:name :posts
     :view v/post-panel
     :controllers [{:start (fn [& params] (js/console.log "Posts"))
                    :stop (fn [& params] (js/console.log "Leaving Posts"))}]}]])

(defn on-navigate [new-match]
  (when new-match
    (re-frame/dispatch [::e/navigated new-match])))

(def router
  (rf/router
   routes
   {:data {:coercion rss/coercion}}))

(defn init-routes! []
  (rfe/start!
   router
   on-navigate
   {:use-fragment true}))

and my events.cljs also follows their example:

(re-frame/reg-event-fx
 ::navigate
 (fn [db [_ & route]]
   {::navigate! route}))

(re-frame/reg-event-fx
 ::navigate!
 (fn [route]
   (apply rfe/push-state route)))

(re-frame/reg-event-db
 ::navigated
 (fn [db [_ new-match]]
   (let [old-match (:page db)
         controllers (rfc/apply-controllers (:controllers old-match) new-match)]
     (assoc db :page (assoc new-match :controllers controllers)))))

In my views.cljs I have some logic to change what page is displayed based on the dictionary entry :page

(defn page []
  (let [current @(rf/subscribe [::subs/page])]
    [:div {:style {:justify-content :center}}
     [navbar]
     (case (:name (:data current))
       :home [main-panel]
       :posts [posts-panel]
       [:div])]))

Routing sort of works so I am able to push forward with my website for now but I would love for the final product to not have '#' in the URL when navigating it.

What am I missing? Web dev is still pretty new to me so all suggestions are very welcome.


Solution

  • Try to pass {:use-fragment false} into rfe/start!