skip to Main Content

Is there a simple way to change document title and description (or other [:html [:head [:meta tags) from ClojureScript Reagent application? For example on every bidi route match change the title and description to match the new page content.

Preferably this should work without using js/window so that the same code can be used in a browser as well as in server isomorphic pre-rendering (which I need for SEO).

In the JavaScript/React world there are react-document-meta and react-side-effect which can probably be converted to Reagent components. But this way of handling side effects seems like a hack and probably simpler solution can be done in pure ClojureScript.

2

Answers


  1. I’m not using bidi, but a similar bidirectional router silk together with re-frame. For every page change, we trigger a :set-current-route event which will be handled centrally.

    The handler for :set-current-route will then

    (set! (.-title js/document) "dynamic title")
    
    Login or Signup to reply.
  2. I am also not using bidi, but here is the solution I came up with for updating the HTML document title using reitit in reagent. I call this function from my main route-wrapping component’s :component-did-update:

    (defn update-html-title! []
      (set! (.-title js/document)
            (->> (.. js/window -location -pathname)
                 (rf/match-by-path router)
                 :data
                 :title)))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search