Skip to content

Files

Latest commit

 

History

History
532 lines (345 loc) · 17.2 KB

api.md

File metadata and controls

532 lines (345 loc) · 17.2 KB

Table of contents

  • rain.biff - Functions for using Rain on top of Biff.
  • rain.core - Functions to support rendering pages with Reitit and Hiccup.
    • bootstrap-data-tag - Returns a <script> tag in Hiccup format that encodes the :rain/bootstrap-data key from the Ring request as application/transit+json.
    • csrf-meta-tag - Returns a <meta> tag for a CSRF token in Hiccup format if :anti-forgery-token is in the request.
    • export-pages - Export static pages to a directory.
    • main-cljs-bundle-path - Returns the path to the main CLJS bundle if the manifest file exists.
    • main-cljs-bundle-tag - Returns a <script> tag for the main CLJS bundle if the manifest file exists.
    • main-stylesheet-path - Returns the path of the main stylesheet if the manifest file exists.
    • main-stylesheet-tag - Returns the main stylesheet <link> tag if the main CSS file is found.
    • meta-tags - Returns a list of <meta> tags in Hiccup format.
    • script-tags - Returns list of <script> tags in Hiccup format.
    • site-routes - Returns site routes wrapped with the wrap-page middleware.
    • static-pages - Return a map of page paths to page HTML, generated from the static routes.
    • stylesheet-tags - Returns a list of stylesheet <link> tags in Hiccup format.
    • wrap-page - A Ring middleware that adds support for page config keys in route definitions.
  • rain.re-frame - Functions to support rendering Reagent and Re-frame components on both the JVM and browser.
    • *app-db* - A dynamic var for the current Re-frame app DB.
    • atom - Returns a Reagent atom.
    • current-page - A Reagent component to render the current page.
    • dispatch - Dispatch a Re-frame event asynchronously.
    • dispatch-sync - Dispatch a Re-frame event synchronously.
    • dispatcher - Returns an event handler that dispatches a Re-frame event.
    • href-alpha - Returns a path for a named route.
    • reg-event-db - Register a Re-frame DB event handler.
    • reg-event-fx - Register a Re-frame effect event handler.
    • reg-sub - Register a Re-frame subscription.
    • set-page - Dispatch the [:rain.re-frame/set-page match] event to change the page when a new match is detected.
    • subscribe - Returns a Re-frame subscription.
    • wrap-rf - A Ring middleware to add support for Re-frame in server components.

Functions for using Rain on top of Biff.

Core Biff functions are taken verbatim from the current Biff code, except the XTDB dependency is removed. This enables using Biff with other databases like Postgres or Datomic.

(use-chime {:keys [biff/features biff/plugins biff.chime/tasks], :as ctx})

A Biff component for chime. Same as com.biffweb/use-chime, but without XTDB.

Source

(use-jetty {:biff/keys [host port handler], :or {host "localhost", port 8080}, :as ctx})

A Biff component for jetty. Same as com.biffweb/use-jetty, but without XTDB.

Source

(use-shadow-cljs {:keys [shadow-cljs/mode], :as ctx})

A Biff component for shadow-cljs.

Source


Functions to support rendering pages with Reitit and Hiccup.

(bootstrap-data-tag {:rain/keys [bootstrap-data], :as _request})

Returns a <script> tag in Hiccup format that encodes the :rain/bootstrap-data key from the Ring request as application/transit+json. If the :rain/bootstrap-data key is not found, returns nil.

Source

(csrf-meta-tag request)

Returns a <meta> tag for a CSRF token in Hiccup format if :anti-forgery-token is in the request. Otherwise returns nil.

Example:

[:meta {:name "csrf-token" :content "..."}]

Source

(export-pages pages dir)

Export static pages to a directory.

Source

(main-cljs-bundle-path)

Returns the path to the main CLJS bundle if the manifest file exists. Otherwise returns nil.

Example:

; Dev:
"/js/main.js"

; Prod:
"/js/main.9528D63C2BDE006EA0667792187CAD3C.js"

Source

(main-cljs-bundle-tag)

Returns a <script> tag for the main CLJS bundle if the manifest file exists. Otherwise returns nil.

Example:

; Dev
[:script {:src "/js/main.js"}]

; Prod
[:script {:src "/js/main.9528D63C2BDE006EA0667792187CAD3C.js"}]

Source

(main-stylesheet-path)

Returns the path of the main stylesheet if the manifest file exists. If no manifest file is found, try public/css/main.css instead. Otherwise returns nil.

Source

(main-stylesheet-tag)

Returns the main stylesheet <link> tag if the main CSS file is found. Otherwise returns nil.

Source

(meta-tags request)

Returns a list of <meta> tags in Hiccup format.

Included tags:

Source

(script-tags request)

Returns list of <script> tags in Hiccup format.

Included tags:

Source

(site-routes routes)

Returns site routes wrapped with the wrap-page middleware. This includes all routes except static routes.

Source

(static-pages routes & {:as ctx})

Return a map of page paths to page HTML, generated from the static routes.

The :static-paths key in the route data can be used to control which paths are generated by this function.

Includes the following route transformations:

  • If a route path ends with a slash, it will be renamed to end with index.html.
  • If a route path doesn't have an extension, it will be renamed to end with .html.

Source

(stylesheet-tags)

Returns a list of stylesheet <link> tags in Hiccup format.

Included tags:

Source

(wrap-page handler)

A Ring middleware that adds support for page config keys in route definitions.

Included page config keys:

  • :server-props — Render props for a server-side rendered page.
  • :static-props — Render props for a static page.
  • :static-paths — Generate paths to be rendered as static pages.

Source


Functions to support rendering Reagent and Re-frame components on both the JVM and browser.

A dynamic var for the current Re-frame app DB.

Client:

Alias of re-frame.db/app-db

Server:

An IDeref that always returns {}.

Source

(atom init-val)

Returns a Reagent atom.

Client:

Alias of reagent.core/atom.

Server:

Returns an IDeref that always returns the init-val.

Source

(current-page _)

A Reagent component to render the current page.

 **Client:**

 Renders the `:get` function in the current route match data as a Reagent
 component. Use `[:rain.re-frame/set-page match]` to set the current route
 match.

 **Server:**

 Not available. On the server, the `:get` function in the route match for
 the request is called directly by [`rain.re-frame/wrap-rf`](#rain.re-frame/wrap-rf) to produce
 Hiccup. This Hiccup is rendered in the `<div id="app">` within the final
 HTML document before sending it to the client.

 In addition, the `:server-props` or `:static-props` from the route match
 data will be serialized into a `<script>` tag using Transit to allow for
 client-side hydration.

Source

(dispatch event)

Dispatch a Re-frame event asynchronously.

Client:

Alias of re-frame.core/dispatch.

Server:

No-op. Dispatching events is not supported on the server.

Source

(dispatch-sync event)

Dispatch a Re-frame event synchronously.

Client:

Alias of re-frame.core/dispatch-sync.

Server:

No-op. Dispatching events is not supported on the server.

Source

(dispatcher event)

Returns an event handler that dispatches a Re-frame event.

Client:

When the event handler function is called, it invokes re-frame.core/dispatch with the event.

Server:

Returns nil. This will cause the handler attribute (e.g. :on-click) to be omitted from the final HTML, preventing hydration errors.

Source

(href-alpha router name)
(href-alpha router name path-params)

Returns a path for a named route.

EXPERIMENTAL

In the future, another function will be added to make the router argument implicit and this function will be removed.

Client:

Same implementation on client and server.

Server:

Same implementation on client and server.

Source

(reg-event-db id handler)
(reg-event-db id interceptors handler)

Register a Re-frame DB event handler.

Client:

Alias of re-frame.core/reg-event-db.

Server:

No-op. Dispatching events is not supported on the server.

Source

(reg-event-fx id handler)
(reg-event-fx id interceptors handler)

Register a Re-frame effect event handler.

Client:

Alias of re-frame.core/reg-event-fx.

Server:

No-op. Dispatching events is not supported on the server.

Source

(reg-sub query-id f)

Register a Re-frame subscription.

Client:

Alias of re-frame.core/reg-sub.

Server:

Re-implementation of re-frame.core/reg-sub.

Source

(set-page {:keys [db]} [_ match])

Dispatch the [:rain.re-frame/set-page match] event to change the page when a new match is detected.

 **Client:**

 Returns an effect map with `:db` and `:fx`.

 The `:db` key will contain a new Re-frame DB with updated keys:

 1. Keys from `:server-props` or `:static-props`
 2. `:page`: The current page.
 3. `:match`: The current route match.

 The `:fx` key will contain the effects from the `:fx` key in the current
 page's route match data.

 **Server:**

 Not available.

Source

(subscribe query)

Returns a Re-frame subscription.

Client:

Alias of re-frame.core/subscribe.

Server:

Re-implementation of re-frame.core/subscribe.

Source

(wrap-rf page-fn)

A Ring middleware to add support for Re-frame in server components.

Client:

Returns the original handler function unmodified since it's unnecessary to bind [rain.re-frame/*app-db*](#rain.re-frame/app-db) on the client. Rendering happens later in the rain.re-frame/current-page component.

Server:

Returns a wrapped handler function. The wrapped function accepts a map and binds the value to [rain.re-frame/*app-db*](#rain.re-frame/app-db). Then the handler function is called to render the page to Hiccup.

Note: To support Form-2 Reagent components, handler functions returning another inner function are accepted.

Source