venantius/accountant

is there a recommended way to use with gitlab pages, served from non-root directory?

Closed this issue · 9 comments

I've made a template app using "lein new reagent myproject". The template is here.

Is there a recommended of modifying accountant in the template file, so that it will be usable with a gitlab or github pages app? gitlab and github pages do not serve the app from root but instead from a directory. For example, "https://bumblehead.gitlab.io/myproject/" or "http://bumblehead.gitlab.io/myproject/".

Should I change my app routing from '/' and '/about' to '/myproject/' and '/myproject/about'?

@iambumblehead

If you are using secretary it is possible to set a prefix to all your routing as;

  (secretary/set-config! :prefix "#")
  (configure-navigation...)

Could you perhaps give it a try?

@iku000888 it doesn't seem to have any effect

I'll continue searching for a solution and include set-config! in my search. I pasted and compiled this file and it does work. I added a hash-style url link and behaviour and that works. I'll play with it more this weekend.

@iku000888 I changed my file to match the test secretary file and found that hash urls are still not working in my template file. It would appear the reason are these accountant/configure-navigation! lines in the template file. When the value of :nav-handler's path is '/#/about', the value of :path-exists? path is '/'

edit, I added this to remove the hash and it seems to work,

       (let [hashless (clojure.string/replace path #"^/#" "")]
         (secretary/dispatch! hashless)))

@iambumblehead

So I cooked up a reduced sample for your perusal hosted on github paged https://iku000888.github.io

Source at: https://github.com/iku000888/iku000888.github.io

Could you comment on what is different from the expected behavior?

@iku000888 I apologize for the inconvenience to you and thank you for going to this trouble for me. Thank you. I will paste your file in the stead of my own and run it and report back.

@iku000888 your file works perfectly :). I'll compare the differences to find problems in my own file and report back. Thank you.

@iku000888 I notice this declare near the top of your file, which pre-emptively create foo and bar before they are referenced by the vdom functions and later defined by defroute...

Would you recommend a way of using defroute-generated uris without this circling reference? I'm looking at secretary to try and understand what calls it makes to generate uris, so that I might reproduce those calls inside the vdom functions and remove the circularity...

@iambumblehead

Glad it works out!
In terms of getting rid of the declare the only other method I know is resolve
The trade off is a compiler warning saying page-foo and page-bar are not defined.

i.e.

(ns iku000888.page
  (:require
   [accountant.core :as accountant]
   [secretary.core :as secretary :include-macros true :refer [defroute]]
   [reagent.core :as reagent :refer [atom]]))

(def current-page
  (atom page-foo))

(defn app []
  [:div [@current-page]])

(defroute "/" []
  (accountant/navigate! "#/foo/"))

(defroute foo "/foo/" {:as params}
  (reset! current-page
          (resolve 'page-foo)))

(defroute bar "/bar/" {:as params}
  (reset! current-page
          (resolve 'page-bar)))

(defn page-foo []
  [:div
   "foo"
   [:a {:href (bar)} "bar"]])

(defn page-bar []
  [:div
   "bar"
   [:a {:href (foo)} "foo"]])

(secretary/set-config! :prefix "#")

(defn init []
  (accountant/configure-navigation!
   {:nav-handler
    (fn [path]
      (let [[_ fragment] (re-find #"#(.*)" path)]
        (secretary/dispatch! (or fragment "/"))))
    :path-exists?
    (fn [path]
      (secretary/locate-route path))})
  (accountant/dispatch-current!)
  (reagent/render [app]
                  (.getElementById js/document "app")))

(init)

I think the problem is inherently circular so I would be interested if there is a good solution ;)
Cheers!

Closing as it really is not about accountant perse, 🙏

I am thinking the circular issue would not happen if we use bidi, so I encourage checking it out.