hexo-offline is intended to provide offline experience for hexo built static website. It uses ServiceWorker under the hood. Simply install this plugin to your website and it should be offline ready by caching most of static assets.
See here for v1 docs.
npm i hexo-offlineOnce installed, run hexo clean && hexo generate to activate offline experience.
hexo-offline requires Hexo 5, 6, 7 or 8 and Node.js ^20.17.0 or >=22.5.0.
If the website serves all content from the origin server, you don't have to add any config. Simply install and run hexo clean && hexo generate.
While hexo-offline aims to provide zero-config offline enhancement to your hexo project, it does offer full list of options control from workbox-build. Create a hexo-offline.config.cjs in the hexo root directory
// offline config passed to workbox-build.
module.exports = {
globPatterns: ["**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}"],
};Again, the config is demo only and you don't have to copy and paste if you serve all contents from the origin server.
globPatterns and the other glob options are matched against the files Hexo generates, relative to the public folder. The service worker is always generated as service-worker.js in the public folder, so globDirectory and swDest are not supported.
hexo-offline runs when Hexo generates the site, after every page has been rendered and before the files are written to the public folder. It adds service-worker.js and the Workbox runtime to the generated files and injects the register script into index.html, so they are written and deployed like any other generated file, including with hexo generate --deploy.
The service worker is generated whenever the site is generated for publishing: hexo generate, hexo generate --deploy, hexo deploy --generate, or a plain hexo deploy when there is no public folder yet. A plain hexo deploy with an existing public folder deploys it as it is. The service worker is not generated by hexo server or hexo generate --watch (or while Hexo watches for changes through its API), so local previews don't install one. Because hexo generate --watch writes the public folder without the service worker (and removes one generated earlier), run hexo generate again before deploying.
To try the service worker locally, run hexo generate and serve the public folder on a different port from the one you use for hexo server, for example hexo server --static --port 4001. A service worker installed on a port keeps serving its cached pages there until it is unregistered in the browser's developer tools.
globDirectoryandswDestare no longer supported and are ignored with a warning. Remove them fromhexo-offline.config.cjs.- The service worker is now generated together with the rest of the site, so
hexo generate --deployandhexo deploy --generatedeploy it. You no longer need to runhexo generateandhexo deployseparately. hexo serverandhexo generate --watchno longer generate a service worker.skipWaitingandclientsClaimnow default totrue. See Updating to new content.- Node.js ^20.17.0 or >=22.5.0 is required.
- Run
hexo cleanonce after upgrading, so the files v4 wrote directly to the public folder (such as an oldworkbox-*.js) are removed.
By default hexo-offline sets skipWaiting: true and clientsClaim: true, so a newly deployed service worker takes over as soon as it is installed. After you deploy, the first page view may still show the cached content. The new service worker downloads the updated files in the background, and once it has finished installing, the next refresh or visit shows the new content.
A page that is already open when the new service worker takes over keeps its old HTML, but any files it requests afterwards come from the new version. If a deploy renames or significantly changes theme scripts or styles, such a page may look or behave oddly until it is reloaded.
To keep Workbox's default behaviour, where a new service worker waits until every tab of the site is closed, set both options to false in hexo-offline.config.cjs. If the file already exists, add them to the object it already exports rather than adding a second module.exports:
module.exports = {
skipWaiting: false,
clientsClaim: false,
};Suppose that you have used two CDN scripts:
- https://cdn.example.com/script-name/script-version.js
- https://cdn.another-example.org/script-name/script-version.cssAdd this config to root hexo-offline.config.cjs
// offline config passed to workbox-build.
module.exports = {
runtimeCaching: [
{
urlPattern: /^https:\/\/cdn\.example\.com\/.*/,
handler: "CacheFirst",
},
{
urlPattern: /^https:\/\/cdn\.another-example\.org\/.*/,
handler: "CacheFirst",
},
],
};For more information, see Workbox RuntimeCaching.
Note:
- As the CDN resources are runtime cached, a resource is cached only after a user agent visits a page that references it. Therefore, if you have included a CDN resource
example.com/script.jsinsome-page.htmlonly, a user who visits onlyindex.htmlwould not haveexample.com/script.jsin cache. - We use the
CacheFirsthandler because CDN resources with a specific version are not supposed to change in the future.