Nebula
Nebula copied to clipboard
[SW] Prevent maintenance mode from being cached by the service worker
I'm sure the chances of this happening are pretty low, but I'd like to try to prevent caching the maintenance page if possible.
Here is the entirety of the HTML when WordPress is in maintenance mode:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Maintenance</title>
</head>
<body>
<h1>Briefly unavailable for scheduled maintenance. Check back in a minute.</h1>
</body>
</html>
Therefore, it does not pull in any scripts or anything. However, the active Service Worker may still cache this page.
The only way I've figured to detect maintenance mode is to check the root WordPress directory for a file named .maintenance
. (Which, for reference, only contains the following line):
<?php $upgrading = time(); ?>
When in maintenance mode, the theme files do not even get loaded, so no hook or check can be applied. Therefore the only thing we have to rely on is the sw.js file itself...
Perhaps we can parse the response of the fetch, but that seems like overkill since every single file would be checked every single time to try to catch something that is only active for a few seconds occasionally.
ChatGPT seems to think that the maintenance page is https://example.com/wp-admin/install.php
but I don't know how accurate that is (at least in the eyes of the service worker).