Skip to content

Commit

Permalink
add a script to remove the old service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Nov 28, 2020
1 parent 3dce722 commit 23b4262
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
20 changes: 19 additions & 1 deletion default.nix
Expand Up @@ -11,6 +11,7 @@ in pkgs.stdenv.mkDerivation {
pkgs.zola
pkgs.nodePackages.html-minifier
pkgs.nodePackages.clean-css-cli
pkgs.nodePackages.uglify-js
];
buildPhase = ''
zola build
Expand All @@ -21,8 +22,25 @@ in pkgs.stdenv.mkDerivation {
-exec cleancss -O 1 -o {}.min {} \; \
-exec mv {}.min {} \;
echo "compressing JS"
find public \
-type f -name '*.js' \
-exec uglifyjs -o {}.min {} \; \
-exec mv {}.min {} \;
echo "compressing HTML"
html-minifier --collapse-whitespace --decode-entities --remove-comments --remove-attribute-quotes --remove-redundant-attributes --remove-optional-tags --remove-script-type-attributes --remove-style-link-type-attributes --input-dir public --output-dir public
html-minifier \
--collapse-whitespace \
--decode-entities \
--remove-comments \
--remove-attribute-quotes \
--remove-redundant-attributes \
--remove-optional-tags \
--remove-script-type-attributes \
--remove-style-link-type-attributes \
--file-ext html \
--input-dir public \
--output-dir public
'';

installPhase = ''
Expand Down
13 changes: 13 additions & 0 deletions static/remove-old-service-worker.js
@@ -0,0 +1,13 @@
// I had a service worker with elm-pages. Now it's gone, so we need to unregister
// it or it'll keep showing the wrong content. I'll probably drop this code
// eventually!
navigator.serviceWorker.getRegistrations().then(function(registrations) {
for (var i = 0; i < registrations.length; i++) {
registrations[i].unregister();
}

// do a hard reload to make sure we have all the latest assets etc.
if (registrations.length > 0) {
window.location.reload(true)
}
})
2 changes: 2 additions & 0 deletions templates/base.html
Expand Up @@ -24,6 +24,8 @@
<meta property="twitter:description" content="{{ config.description }}">
<meta property="og:type" content="website">
{% endblock -%}

<script src="remove-old-service-worker.js" defer></script>
</head>
<body>
<header>
Expand Down

0 comments on commit 23b4262

Please sign in to comment.