How to use the "on demand" plugin in November 2022
by Γscar Otero
2 min read
The on_demand
plugin is the attempt of
Lume to provide some server-side rendering behaviour to a static site. The idea
is simple: omit some pages in the build process (with ondemand: true
) in order
to build them when they are requested.
This allows insert dynamic content in the pages like
in this example, a site with the pages
showing the current time. You can see the
code repository, where the
current time is defined as a
helper in the _config.ts
file
and then
called in the Nunjucks layout
used by both pages.
This plugin was tested only in Deno Deploy but it should work on any hosting with Deno. But it is not all rosy in the garden, because Deno Deploy has some limitations that makes more difficult to use this plugin, compared with the Deno CLI that you have installed locally:
No support for dynamic imports
One of the most requested features in Deno Deploy is the ability to import modules dynamically. One year and half since the issue was created, it's still not possible and looks like it won't be in the short term. Lume uses dynamic imports to load pages and data in JavaScript, JSX and TypeScript, so if your on demand pages use any of these formats, they will fail on Deno Deploy. The only way to skip this limitation is by generating a file that imports statically all files that, under normal conditions, would be imported dynamically (this file is generated automatically by the ondemand plugin). It's not an elegant solution but it's the only solution that works at this moment.
No support for NPM modules
As of version 1.12.0, Lume uses npm:
modules for some dependencies. NPM
especifiers
are not yet supported in Deno Deploy.
Fortunately we can use import maps to use the esm.sh version of the NPM
packages. In the repository of the plugin demo, you can
see the import_map.json file
needed to map all NPM lume dependencies to the esm.sh equivalent.
And this is the state of the dynamic pages in Lume for now. I hope these
limitations disappear in the short term (I guess NPM modules will be supported
soon). If you know of other hosting providers with Deno support and you get the
on demand
plugin to work there, please, let me know.