Skip to content

Latest commit

 

History

History

docs

<!DOCTYPE html PUBLIC ""
    "">
<html><head><meta charset="UTF-8" /><title>Readme</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div id="header"><h2>Generated by <a href="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/weavejester/codox">Codox</a></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">Sitefox</span> <span class="project-version"></span></span></a></h1></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Project</span></h3><ul class="index-link"><li class="depth-1 "><a href="index.html"><div class="inner">Index</div></a></li></ul><h3 class="no-link"><span class="inner">Topics</span></h3><ul><li class="depth-1  current"><a href="README.html"><div class="inner"><span>Readme</span></div></a></li></ul><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1 branch"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span></span></div></div></li><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>sitefox</span></div></div></li><li class="depth-2 branch"><a href="sitefox.auth.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>auth</span></div></a></li><li class="depth-2 branch"><a href="sitefox.db.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>db</span></div></a></li><li class="depth-2 branch"><a href="sitefox.deps.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>deps</span></div></a></li><li class="depth-2 branch"><a href="sitefox.html.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>html</span></div></a></li><li class="depth-2 branch"><a href="sitefox.logging.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>logging</span></div></a></li><li class="depth-2 branch"><a href="sitefox.mail.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>mail</span></div></a></li><li class="depth-2 branch"><a href="sitefox.reloader.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>reloader</span></div></a></li><li class="depth-2 branch"><a href="sitefox.tracebacks.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>tracebacks</span></div></a></li><li class="depth-2 branch"><a href="sitefox.ui.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>ui</span></div></a></li><li class="depth-2 branch"><a href="sitefox.util.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>util</span></div></a></li><li class="depth-2"><a href="sitefox.web.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>web</span></div></a></li><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree" style="top: -362px;"><span class="top" style="height: 371px;"></span><span class="bottom"></span></span><span>sitefoxtest</span></div></div></li><li class="depth-2"><a href="sitefoxtest.e2etests.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>e2etests</span></div></a></li></ul></div><div class="document" id="content"><div class="doc"><div class="markdown"><h1><a href="#readme" id="readme"></a>Readme</h1>
<p>Web framework for ClojureScript on Node. <strong>WIP</strong>.</p>
<p align="center">
  <img src="logo.svg?sanitize=true" alt="Sitefox logo" /><br />
</p>
<p>In the tradition of Django, Flask, and Rails. Designed for indie devs who ship fast. Battle tested on real sites.</p>
<p><a href="#philosophy">Philosophy</a> | <a href="#quick-start">Quick start</a> | <a href="https://chr15m.github.io/sitefox/">Documentation</a> | <a href="#api">API</a> | <a href="https://github.com/chr15m/sitefox/tree/main/examples">Examples</a> | <a href="#community">Community</a></p>
<pre><code class="language-clojure">(ns webserver
  (:require
    [promesa.core :as p]
    [sitefox.html :refer [render]]
    [sitefox.web :as web]))

(defn root [_req res]
  (-&gt;&gt; (render [:h1 "Hello world!"])
       (.send res)))

(p/let [[app host port] (web/start)]
  (.get app "/" root)
  (print "Serving on" (str "http:https://" host ":" port)))
</code></pre>
<h2><a href="#philosophy" id="philosophy"></a>Philosophy</h2>
<ul>
<li><a href="https://12factor.net/">12 factor</a>.</li>
<li>👇 Batteries included.</li>
</ul>
<h3><a href="#batteries-included" id="batteries-included"></a>Batteries included</h3>
<ul>
<li><a href="#web-server--routes">Routing</a></li>
<li><a href="#templates">Templates</a></li>
<li><a href="#database">Database + Key-value store</a></li>
<li><a href="#sessions">Sessions</a></li>
<li><a href="#authentication">Authentication</a></li>
<li><a href="#email">Email</a></li>
<li><a href="#forms">Forms</a></li>
<li><a href="#logging-and-errors">Logging</a></li>
<li><a href="#live-reloading">Live reloading</a></li>
</ul>
<h3><a href="#environment-variables" id="environment-variables"></a>Environment variables</h3>
<ul>
<li><code>PORT</code> - configure the port Sitefox web server binds to.</li>
<li><code>BIND_ADDRESS</code> - configure the IP address Sitefox web server binds to.</li>
<li><code>SMTP_SERVER</code> - configure the outgoing SMTP server e.g. <code>SMTP_SERVER=smtps:https://username:[email protected]/?pool=true</code>.</li>
<li><code>DATABASE_URL</code> - configure the database to connect to. Defaults to <code>sqlite:https://./database.sqlite</code>.</li>
</ul>
<h2><a href="#quick-start" id="quick-start"></a>Quick start</h2>
<p>The quickest way to start is using one of the <code>create</code> scripts which will set up an example project for you with one command. If you’re building a simple website without much front-end interactivity beyond form submission, the <a href="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/babashka/nbb">nbb</a> create script is the way:</p>
<pre><code>npm init sitefox-nbb mywebsite
</code></pre>
<p>This will create a folder called <code>mywebsite</code> containing your new project. Note you can use <a href="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/borkdude/scittle">Scittle</a> to run cljs client-side.</p>
<p>If you’re building a full-stack ClojureScript application the <a href="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/shadow-cljs">shadow-cljs</a> create script is the way:</p>
<pre><code>npm init sitefox-shadow-fullstack myapp
</code></pre>
<p>That will create a folder called <code>myapp</code> containing your new project.</p>
<h3><a href="#manually-installing-sitefox" id="manually-installing-sitefox"></a>Manually installing Sitefox</h3>
<p>Add Sitefox to your project as a dependency:</p>
<pre><code>{:deps
 {io.github.chr15m/sitefox {:git/tag "v0.0.19" :git/sha "bb4d64be7bce5989299c7891f0320e10b812d160"}}}
</code></pre>
<p>If you’re using <code>npm</code> you can install sitefox as a dependency that way. If you do that you will need to add <code>node_modules/sitefox/src</code> to your classpath somehow.</p>
<pre><code>npm i sitefox
</code></pre>
<p><strong>Note</strong>: M1 Mac users may need to set the Python version in npm like this:</p>
<pre><code>npm config set python python3
</code></pre>
<p>This is because the <code>node-sqlite3</code> build sometimes fails without the setting. See <a href="https://github.com/chr15m/sitefox/issues/20#issuecomment-1248719076">this issue</a> for more details.</p>
<h3><a href="#example-server" id="example-server"></a>Example server</h3>
<p>An example server with two routes, one of which writes values to the key-value database.</p>
<pre><code class="language-clojure">(ns my.server
  (:require
    [promesa.core :as p]
    [sitefox.web :as web]
    [sitefox.db :refer [kv]]))

(defn home-page [req res]
  ; send a basic hello world response
  (.send res "Hello world!"))

(defn hello [req res]
  ; write a value to the key-value database
  (p/let [table (kv "sometable")
          r (.write table "key" 42)]
    (.json res true)))

(defn setup-routes [app]
  ; flush all routes from express
  (web/reset-routes app)
  ; set up an express route for "/"
  (.get app "/" home-page)
  ; set up an express route for "/hello"
  (.post app "/hello" hello)
  ; statically serve files from the "public" dir on "/"
  ; (or from "build" dir in PROD mode)
  (web/static-folder app "/" "public"))

(defn main! []
  ; create an express server and start serving
  ; BIND_ADDRESS &amp; PORT env vars set host &amp; port.
  (p/let [[app _host _port] (web/start)]
    ; set up the routes for the first time
    (setup-routes app)))
</code></pre>
<p>More <a href="https://github.com/chr15m/sitefox/tree/main/examples">Sitefox examples here</a>.</p>
<h2><a href="#community" id="community"></a>Community</h2>
<p>If you need support with Sitefox you can:</p>
<ul>
<li>Join the <a href="https://app.slack.com/client/T03RZGPFR/C02LB2842UA">Clojure Slack #sitefox channel</a>.</li>
<li>You can also <a href="https://github.com/chr15m/sitefox/discussions">ask a question on the GitHub discussions page</a>.</li>
</ul>
<h2><a href="#api" id="api"></a>API</h2>
<h3><a href="#web-server-routes" id="web-server-routes"></a>Web server &amp; routes</h3>
<p>Sitefox uses the <a href="https://expressjs.com">express</a> web server with sensible defaults for sessions and logging. See the <a href="http:https://expressjs.com/en/guide/routing.html">express routing documentation</a> for details.</p>
<p>Create a new server with <code>web/start</code> and set up a route which responds with “Hello world!” as follows:</p>
<pre><code class="language-clojure">(-&gt; (web/start)
  (.then (fn [app host port]
    (.get app "/myroute"
      (fn [req res]
        (.send res "Hello world!"))))
</code></pre>
<p>Sitefox comes with an optional system to reload routes when the server is changed. Your express routes will be reloaded every time your server code is refreshed (e.g. by a shadow-cljs build). In this example the function <code>setup-routes</code> will be called when a rebuild occurs.</p>
<pre><code class="language-clojure">(defn setup-routes [app]
  ; flush all routes from express
  (web/reset-routes app)
  ; ask express to handle the route "/"
  (.get app "/" (fn [req res] (.send res "Hello world!"))))

; during the server setup hook up the reloader
(reloader (partial #'setup-routes app))
</code></pre>
<p>I recommend the <a href="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/funcool/promesa">promesa</a> library for managing promise control flow. This example assumes require <code>[promesa.core :as p]</code>:</p>
<pre><code class="language-clojure">(p/let [[app host port] (web/start)]
  ; now use express `app` to set up routes and middleware
  )
</code></pre>
<p>Also see these examples:</p>
<ul>
<li><a href="https://github.com/chr15m/sitefox/tree/main/examples/shadow-cljs">shadow-cljs server example</a>.</li>
<li><a href="https://github.com/chr15m/sitefox/tree/main/examples/nbb">nbb server example</a>.</li>
</ul>
<h3><a href="#templates" id="templates"></a>Templates</h3>
<p>Instead of templates, Sitefox offers shortcuts for server side Reagent rendering, merged wth HTML documents.</p>
<pre><code class="language-clojure">[sitefox.html :refer [render-into]]
</code></pre>
<p>You can load an HTML document and render Reagent forms into a selected element:</p>
<pre><code class="language-clojure">(def index-html (fs/readFileSync "index.html"))

(defn component-main []
  [:div
   [:h1 "Hello world!"]
   [:p "This is my content."]])

; this returns a new HTML string that can be returned
; e.g. with (.send res)
(render-into index-html "main" [component-main])
</code></pre>
<p>Sitefox uses <a href="https://www.npmjs.com/package/node-html-parser">node-html-parser</a> and offers shortcuts for working with HTML &amp; Reagent:</p>
<ul>
<li><code>html/parse</code> is shorthand for <code>node-html-parser/parse</code>.</li>
<li><code>html/render</code> is shorthand for Reagent’s <code>render-to-static-markup</code>.</li>
<li><code>html/$</code> is shorthand for the parser’s <code>querySelector</code>.</li>
<li><code>html/$$</code> is shorthand for the parser’s <code>querySelectorAll</code>.</li>
</ul>
<p>Also see the <a href="https://github.com/chr15m/sitefox/tree/main/examples/templates">templates example</a> project.</p>
<h3><a href="#database" id="database"></a>Database</h3>
<p>Sitefox makes it easy to start storing key-value data with no configuration. You can transition to more structured data later if you need it. It bundles <a href="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/lukechilds/keyv">Keyv</a> which is a database backed key-value store. You can access the key-value store through <code>db/kv</code> and the underlying database through <code>db/client</code>.</p>
<p>See the full <a href="https://chr15m.github.io/sitefox/sitefox.db.html">documentation for the db module</a>.</p>
<p>By default a local sqlite database is used and you can start persisting data on the server immediately without any configuration. Once you move to production you can configure another database using the environment variable <code>DATABASE_URL</code>. For example, to use a postgres database called “DBNAME” you can access it as follows (depending on your network/local setup):</p>
<pre><code>DATABASE_URL="postgres:https://%2Fvar%2Frun%2Fpostgresql/DBNAME"
DATABASE_URL=postgres:https://someuser:somepassword@somehost:5432/DBNAME
DATABASE_URL=postgres:https:///somedatabase
</code></pre>
<p>Note that you will also need to <code>npm install @keyv/postgres</code> if you want to use the Postgres backend.</p>
<p>To use the database and key-value interface first require the database module:</p>
<pre><code class="language-clojure">[sitefox.db :as db]
</code></pre>
<p>Now you can use <code>db/kv</code> to write a key-value to a namespaced “table”:</p>
<pre><code class="language-clojure">(let [table (db/kv "sometable")]
  (.set table "key" "42"))
</code></pre>
<p>Retrieve the value again:</p>
<pre><code class="language-clojure">(-&gt; (.get table "key")
  (.then (fn [val] (print val))))
</code></pre>
<p>You can use <code>db/client</code> to access the underlying database client. For example to make a query against the configured database:</p>
<pre><code class="language-clojure">(let [c (db/client)]
  (-&gt; (.query c "select * from sometable WHERE x = 1")
    (.then (fn [rows] (print rows)))))
</code></pre>
<p>Again, <a href="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/funcool/promesa">promesa</a> is recommended for managing control flow during database operations.</p>
<p>To explore key-value data from the command line use sqlite and jq to filter data like this:</p>
<pre><code>sqlite3 database.sqlite "select * from keyv where key like 'SOMEPREFIX%';" | cut -f 2 -d "|" | jq '.'
</code></pre>
<h4><a href="#sqlite3-full-stack-traces" id="sqlite3-full-stack-traces"></a>Sqlite3 full stack traces</h4>
<p>By default the <code>node-sqlite3</code> module does not provide full stack traces with line numbers etc. when a database error occurs. It’s possible to <a href="https://github.com/TryGhost/node-sqlite3/wiki/Debugging">turn on verbose stack traces</a> with a small performance penalty as follows:</p>
<pre><code class="language-clojure">(ns yourapp
  (:require
    ["sqlite3" :as sqlite3]))

(.verbose sqlite3)
</code></pre>
<h4><a href="#enabling-sqlite3-wal-mode" id="enabling-sqlite3-wal-mode"></a>Enabling Sqlite3 WAL mode</h4>
<p>If you want to run sqlite3 in production you may run into the error <code>SQLITE_BUSY: database is locked</code> when performing simultaneous database operations from different clients. It is possible to resolve these concurrency and locking issues by enabling <a href="https://www.sqlite.org/wal.html">write-ahead logging mode in sqlite3</a> as follows:</p>
<pre><code>(ns yourapp
  (:require
    [sitefox.db :refer [client]]))

(p/let [c (client)
        wal-mode-enabled (.query c "PRAGMA journal_mode=WAL;")]
  (js/console.log wal-mode-enabled))
</code></pre>
<p>This code can safely be placed in the main function of your server code.</p>
<h3><a href="#sessions" id="sessions"></a>Sessions</h3>
<p>Sessions are enabled by default and each visitor to your server will have their own session. The session data is persisted server side across page loads so you can use it to store authentication status for example. Sessions are backed into a namespaced <code>kv</code> table (see the database section above). You can read and write arbitrary JS data structures to the session using <code>req.session</code>.</p>
<p>To write a value to the session store (inside a route handler function):</p>
<pre><code class="language-clojure">(let [session (aget req "session")]
  (aset session "myvalue" 42))
</code></pre>
<p>To read a value from the session store:</p>
<pre><code class="language-clojure">(aget req "session" "myvalue")
</code></pre>
<h3><a href="#authentication" id="authentication"></a>Authentication</h3>
<p>Sitefox wraps the Passport library to implement authentication. You can add simple email and password based authentication to your app with three function calls:</p>
<pre><code class="language-clojure">(defn setup-routes [app]
  (let [template (fs/readFileSync "index.html")]
    (web/reset-routes app)
    ; three calls to set up email based authentication
    (auth/setup-auth app)
    (auth/setup-email-based-auth app template "main")
    (auth/setup-reset-password app template "main")
    ; ... add your additional routes here ... ;
    ))
</code></pre>
<p>The <code>template</code> string passed in is an HTML document and <code>"main"</code> is the selector specifying where to mount the auth UI. This will set up the following routes by default where you can send users to sign up, sign in, and reset their password:</p>
<ul>
<li><code>/auth/sign-in</code></li>
<li><code>/auth/sign-up</code></li>
<li><code>/auth/reset-password</code></li>
</ul>
<p>It is also possible to override the default auth UI Reagent forms and the redirect URLs to customise them with your own versions. See the <a href="https://chr15m.github.io/sitefox/sitefox.auth.html#var-setup-auth">auth documentation</a> for detail about how to supply your own Reagent forms. Also see the <a href="https://github.com/chr15m/sitefox/blob/main/src/sitefox/auth.cljs#L401">source code for the default Reagent auth forms</a> if you want to make your own.</p>
<p>When a user signs up their data is persisted into the default Keyv database used by Sitefox. You can retrieve the currently authenticated user’s datastructure on the request object:</p>
<pre><code class="language-clojure">(let [user (aget req "user")] ...)
</code></pre>
<p>You can then update the user’s data and save their data back to the database. The <code>applied-science.js-interop</code> library is convenient for this (required here as <code>j</code>):</p>
<pre><code class="language-clojure">(p/let [user (aget req "user")]
  (j/assoc! user :somekey 42)
  (auth/save-user user))
</code></pre>
<p>If you want to create a new table it is useful to key it on the user’s uuid which you can obtain with <code>(aget user "id")</code>.</p>
<p>See the <a href="https://github.com/chr15m/sitefox/tree/main/examples/authentication">authentication example</a> for more detail.</p>
<p>To add a new authentication scheme such as username based, or 3rd party oauth, consult the <a href="https://www.passportjs.org/">Passport docs</a> and <a href="https://github.com/chr15m/sitefox/blob/main/src/sitefox/auth.cljs#L210">auth.cljs</a>. Pull requests most welcome!</p>
<h3><a href="#email" id="email"></a>Email</h3>
<p>Sitefox bundles <a href="https://nodemailer.com">nodemailer</a> for sending emails. Configure your outgoing SMTP server:</p>
<pre><code>SMTP_SERVER=smtps:https://username:[email protected]/?pool=true
</code></pre>
<p>Then you can use the <code>send-email</code> function as follows:</p>
<pre><code class="language-clojure">(-&gt; (mail/send-email
      "[email protected]"
      "[email protected]"
      "This is my test email."
      :text "Hello, This is my first email from **Sitefox**. Thank you.")
    (.then js/console.log))
</code></pre>
<p>By default sent emails are logged to <code>./logs/mail.log</code> in json-lines format.</p>
<p>If you don’t specify an SMTP server, the email module will be in debug mode. No emails will be sent, outgoing emails will be written to <code>/tmp</code> for inspection, and <code>send-email</code> outcomes will also be logged to the console.</p>
<p>If you set <code>SMTP_SERVER=ethereal</code> the ethereal.email service will be used. After running <code>send-email</code> you can print the <code>url</code> property of the result. You can use the links that are printed for testing your emails in dev mode.</p>
<p>Also see the <a href="https://github.com/chr15m/sitefox/tree/main/examples/send-email">send-email example</a> project.</p>
<h3><a href="#forms" id="forms"></a>Forms</h3>
<p>See the <a href="https://github.com/chr15m/sitefox/tree/main/examples/form-validation">form validation example</a> which uses <a href="https://www.npmjs.com/package/node-input-validator">node-input-validator</a> and checks for CSRF problems.</p>
<h4><a href="#csrf-protection" id="csrf-protection"></a>CSRF protection</h4>
<p>To ensure you can <code>POST</code> without CSRF warnings you should create a hidden element like this (Reagent syntax):</p>
<pre><code class="language-clojure">[:input {:name "_csrf" :type "hidden" :default-value (.csrfToken req)}]
</code></pre>
<p>If you’re making an ajax <code>POST</code> request from the client side, you should pass the CSRF token as a header. A valid token is available as a string at the JSON endpoint <code>/_csrf-token</code> and you can fetch it using <code>fetch-csrf-token</code> and add it to the headers of a fetch request as follows:</p>
<pre><code class="language-clojure">(ns n (:require [sitefox.ui :refer [fetch-csrf-token]]))

(-&gt; (fetch-csrf-token)
    (.then (fn [token]
             (js/fetch "/api/endpoint"
                       #js {:method "POST"
                            :headers #js {:Content-Type "application/json"
                                          :X-XSRF-TOKEN token} ; &lt;- use token here
                            :body (js/JSON.stringify (clj-&gt;js some-data))}))))
</code></pre>
<p><strong>Note</strong>: you can fetch the CSRF token from a client side cookie instead if you set the environment variable <code>SEND-CSRF-TOKEN</code>. This was the default in previous Sitefox versions. When set, Sitefox will send the token on every GET request in the client side cookie <code>XSRF-TOKEN</code> and this can be retrieved with the <code>ui/csrf-token</code> function. This is a valid, but less secure form of CSRF protection.</p>
<p>In some rare circumstances you may wish to turn off CSRF checks (for example posting to an API from a non-browser device). If you know what you are doing you can use the <code>pre-csrf-router</code> to add routes which bypass the CSRF checking:</p>
<pre><code class="language-clojure">(defn setup-routes [app]
  ; flush all routes from express
  (web/reset-routes app)
  ; set up an API route bypassing CSRF checks
  (.post (j/get app "pre-csrf-router") "/api/endpoint" endpoint-unprotected-by-csrf)
  ; set up an express route for "/hello" which is protected as normal
  (.post app "/hello" hello))
</code></pre>
<h3><a href="#logging-and-errors" id="logging-and-errors"></a>Logging and errors</h3>
<p>By default the web server will write to log files in the <code>./logs</code> folder. These files are automatically rotated by the server. There are two types of logs:</p>
<ul>
<li><code>logs/access.log</code> which are standard web access logs in “combined” format.</li>
<li><code>logs/error.log</code> where tracebacks are written using <code>tracebacks/install-traceback-handler</code>.</li>
</ul>
<p>To send uncaught exceptions to the error log:</p>
<pre><code>(def admin-email (env-required "ADMIN_EMAIL"))
(def build-id (try (fs/readFileSync "build-id.txt") (catch :default _e "dev")))

(install-traceback-handler admin-email build-id)
</code></pre>
<p>Create <code>build-id.txt</code> based on the current git commit as follows:</p>
<pre><code>git rev-parse HEAD | cut -b -8 &gt; build-id.txt
</code></pre>
<p>If you want to get correct ClojureScript line numbers in tracebacks require <code>["source-maps-support" :as sourcemaps]</code> and then:</p>
<pre><code>(.install sourcemaps)
</code></pre>
<h4><a href="#404-and-500-errors" id="404-and-500-errors"></a>404 and 500 errors</h4>
<p>You can use the <a href="https://chr15m.github.io/sitefox/sitefox.db.html#var-setup-error-handler"><code>web/setup-error-handler</code></a> function to serve a page for those errors based on a Reagent component you define:</p>
<pre><code class="language-clojure">(defn component-error-page [_req error-code error]
  [:section.error
   [:h2 error-code " Error"]
   (case error-code
     404 [:p "We couldn't find the page you're looking for."]
     500 [:&lt;&gt; [:p "An error occurred:"] [:p (.toString error)]]
     [:div "An unknown error occurred."])])

(web/setup-error-handler app my-html-template "main" component-error-page)
</code></pre>
<p>You can combine these to catch both 500 Internal Server errors and uncaught exceptions as follow:</p>
<pre><code>(let [traceback-handler (install-traceback-handler admin-email build-id)]
    (web/setup-error-handler app template-app "main" component-error-page traceback-handler))
</code></pre>
<h3><a href="#live-reloading" id="live-reloading"></a>Live reloading</h3>
<p>Live reloading is supported using both <code>nbb</code> and <code>shadow-cljs</code>. It is enabled by default when using the npm create scripts. Examples have more details.</p>
<h2><a href="#who" id="who"></a>Who</h2>
<p>Sitefox was made by <a href="https://mccormick.cx">Chris McCormick</a> (<a href="https://twitter.com/mccrmx">@mccrmx on Twitter</a> and <a href="https://mccormick.cx/@chris">@[email protected] on Mastodon</a>). I iterated on it while building sites for myself and for clients.</p>
</div></div></div></body></html>