A page of web components shows nothing until its scripts have run. A prerendered page has the shadow tree of every component in its markup, as a declarative shadow root, so the browser draws the page as soon as the markup arrives. When the scripts arrive, each component takes over its own tree.
scripts/prerender.mjs makes such a page. It opens a page in a headless Chrome and writes it
back out with the browser's own getHTML. It works for a page with open shadow roots, whatever
it was built with.
The script is in the npm package, as the command baredom-prerender.
Serve the page, then:
node scripts/prerender.mjs http://localhost:8000/page.html page.prerendered.html
With the package installed, the same is:
npx baredom-prerender http://localhost:8000/page.html page.prerendered.html
Give more pairs of a URL and a file to write more pages. One browser opens them all, one after the other, which is much quicker than a run for each page:
node scripts/prerender.mjs http://localhost:8000/ index.html http://localhost:8000/about/ about/index.html
A page that fails stops the run. The pages before it stay written.
It needs Node 22 or later, and Chrome. Set CHROME_PATH when Chrome is not found, and
CHROME_FLAGS for more flags, such as --no-sandbox in a container.
Write the output beside the page it was made from, so the relative URLs in it still resolve.
The script waits until every custom element on the page is defined, and then for two frames.
A page that loads more after that puts data-loading on its html element and removes it when
it is ready. The script waits for that too.
It then waits until every image has loaded or failed, in the page and in every shadow tree. An
image that the browser loads lazily is loaded for this, and is lazy again in what is written.
So an x-image is written as loaded, and the same page gives the same file each time. The script
stops and names an image that never finishes.
The directory of an output file is made when it is not there.
With --static before the URL, the output has no script and no noscript element, and its
html element has data-static. The page can use that attribute in its style to hide what
needs a script. bare-html/mirror.html does.
x-image whose picture
the page has already is loaded at once, with no shimmer and no fade.sync. It is not made again.list.append(row), shows every row twice. A script must bring the page to its state and add
nothing. BareMirror's sync does that: it finds the keyed rows that are there and makes only
the ones that are missing.x-table-row and x-tab included. The script stops and names an
element that is never defined.Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |