diff --git a/Makefile b/Makefile index ec88f9d..393460b 100644 --- a/Makefile +++ b/Makefile @@ -26,9 +26,9 @@ runtest: $(MAKE) run PROF=test,$(PROF) runsite: setup - (sleep 3 && open "http://127.0.0.1:$(PORT)") & + (sleep 3 && open "http://127.0.0.1:$(PORT)/$$(basename $$PWD)") & ( trap "kill 0" SIGINT SIGTERM EXIT; \ - ( python -m SimpleHTTPServer $(PORT) & ); \ + ( cd .. && python -m SimpleHTTPServer $(PORT) & ); \ lein -o with-profile $(PROF) cljsbuild auto $(CLJSBUILD) ) install: leinbuild diff --git a/demo/demo.cljs b/demo/demo.cljs index 32bb37a..7d1fb95 100644 --- a/demo/demo.cljs +++ b/demo/demo.cljs @@ -33,13 +33,14 @@ [github-badge]]) (defn ^:export mountdemo [p] - (when p (reset! page p)) + (when p (page/set-start-page p)) (reagent/render-component [demo] (.-body js/document))) (defn gen-page [p timestamp] (reset! page p) (let [body (reagent/render-component-to-string [demo]) - title @page/title-atom] + title @page/title-atom + load-page (case p "index.html" "" p)] (str "
@@ -53,7 +54,7 @@ "))) diff --git a/demo/reagentdemo/page.cljs b/demo/reagentdemo/page.cljs index adaba73..4c32eb9 100644 --- a/demo/reagentdemo/page.cljs +++ b/demo/reagentdemo/page.cljs @@ -8,27 +8,44 @@ [goog.history EventType])) (def page (atom "")) +(def base-path (atom nil)) +(def html5-history false) (defn create-history [] (when reagent/is-client (let [proto (-> js/window .-location .-protocol)] (if (and (.isSupported Html5History) (case proto "http:" true "https:" true false)) - (doto (Html5History.) - (.setUseFragment false)) + (do (set! html5-history true) + (doto (Html5History.) + (.setUseFragment false))) (History.))))) (defn setup-history [] (when-let [h (create-history)] (events/listen h EventType/NAVIGATE - (fn [e] (reset! page (.-token e)))) + (fn [e] (reset! page (subs (.-token e) + (count @base-path))))) (add-watch page ::history (fn [_ _ oldp newp] - (.setToken h newp))) + (.setToken h (str @base-path newp)))) (.setEnabled h true) h)) (def history (setup-history)) +(defn set-start-page [p] + (when html5-history + ;; Find base-path for html5 history + (let [loc (-> js/window .-location .-pathname) + split #".[^/]*" + loc-parts (re-seq split loc) + page-parts (re-seq split (case p "" "." p)) + base (str (apply str + (drop-last (count page-parts) loc-parts)) + "/")] + (reset! base-path (string/replace base #"^/" "")))) + (reset! page p)) + (def title-atom (atom "")) (def page-map (atom nil))