Skip to content

Instantly share code, notes, and snippets.

@aaronc
Last active August 29, 2015 14:11
Show Gist options
  • Save aaronc/c47a79aff8862244226d to your computer and use it in GitHub Desktop.
Save aaronc/c47a79aff8862244226d to your computer and use it in GitHub Desktop.
An idea for server-side prerendering in freactive. This code DOES NOT work in any current version of freactive!
(ns freactive.server-side-prerender-concept
(:refer-clojure :exclude [atom])
(#+cljs :require-macros #+clj :require [freactive.macros :refer [atom]])
#+cljs (:require [freactive.dom :as dom])
#+clj (:require [freactive.dom-prerender :as dom])
(#+clj :require #+cljs :require-macros [freactive.util :refer [cfn]]))
(defn app-view [state]
[:div {:on-click (cfn [e])} ;; cfn is a macro that evals to fn on the client side and nil on the server
[:ul
[:li (:a state)]
[:li (:b state)]]])
#+cljs
(defonce app-state
(atom
(when-let [init js/__init_state]
(edn/read-string init))))
#+cljs
(dom/mount! (.getElementById js/document "root") (app-view app-state))
#+clj
(defn main-route []
(let [init-state {:a 1 :b 2}]
(dom/prerender
[:html
[:head]
[:body
[:div#root (app-view (atom init-state))]
[:script "var __init_state = \"" (pr-str init-state) "\";"]
[:script {:src "js/app.js"}]]])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment