Rendering Elm to static HTML with Selenium

2020-07-04

Suppose you have a simple page on a website with part of its content generated by an Elm program. To also make (the initial view of) the content available to users or simple web crawlers who do not run JavaScript, it can be useful to generate a static HTML copy. This can also help to prevent flickering when the website loads.

Previously I used elm-static-html static-html-from-elm for this, but in combination with Html.Styled from elm-css the output was not exactly the same as when running the Elm code in a browser and there were encoding problems. Hence, why not use a browser to generate the static HTML?

We can do this using Selenium.

First, we make an HTML file which works in your local browser, say render.html: See also the Elm guide about Embedding in HTML.

Second, we use Selenium to control a “headless”, i.e. invisible, Chrome to open the HTML file, run the Elm script and print out the innerHTML. For example, save the following as static-via-selenium.py.

Finally, the following Makefile will build the .js, .min.js and .static.html files. For .min.js we follow the Elm guide about minification.

You can now use the content of MyApp.static.html as a static replacement by putting it inside the <div id="MyApp"></div> in index.html. We leave it as an exercise for the reader to also automate this last step.