Use a release uuid to manage caching

master
paradust7 2022-09-21 00:29:04 +00:00
parent 6a78413ad8
commit c0c68e22ba
8 changed files with 69 additions and 33 deletions

View File

@ -29,3 +29,6 @@ emcc --clear-cache --clear-ports
./pull_minetest.sh
./build_fsroot.sh
./build_minetest.sh
# Finished product
./build_www.sh

View File

@ -62,33 +62,3 @@ if ! $INCREMENTAL; then
fi
emmake make
echo "Installing into www/"
rm -rf "$WWW_DIR"
mkdir "$WWW_DIR"
FILES="minetest.js minetest.wasm minetest.worker.js"
for I in $FILES; do
cp src/"$I" "$WWW_DIR"
done
if [ -f src/minetest.wasm.map ]; then
cp src/minetest.wasm.map "$WWW_DIR"
fi
cp "$BASE_DIR/static/index.html" "$WWW_DIR"
cp "$BASE_DIR/static/launcher.js" "$WWW_DIR"
cp "$BASE_DIR/static/.htaccess" "$WWW_DIR"
cp "$BUILD_DIR/fsroot.tar.zst" "$WWW_DIR/base.pack"
echo "DONE"
popd
popd
# Optional step to deploy to webserver
if [ -f deploy.sh ]; then
./deploy.sh
fi

55
build_www.sh Executable file
View File

@ -0,0 +1,55 @@
#!/bin/bash -eux
source common.sh
# Generate a random hash for this release
# This is used as a prefix for cache invalidation
SEEDFILE="/tmp/minetest_build_uuid_seed"
dd status=none if=/dev/urandom bs=64 count=1 > "$SEEDFILE"
md5sum -b "$SEEDFILE" > "$SEEDFILE".hash
RELEASE_UUID=`cut -b -12 "$SEEDFILE".hash`
RELEASE_DIR="$WWW_DIR/$RELEASE_UUID"
PACKS_DIR="$RELEASE_DIR/packs"
echo "Installing release $RELEASE_UUID into www/"
rm -rf "$WWW_DIR"
mkdir "$WWW_DIR"
mkdir "$RELEASE_DIR"
mkdir "$PACKS_DIR"
# Copy emscripten generated files
pushd "$BUILD_DIR/minetest/src"
EMSCRIPTEN_FILES="minetest.js minetest.wasm minetest.worker.js"
for I in $EMSCRIPTEN_FILES; do
cp "$I" "$RELEASE_DIR"
done
if [ -f minetest.wasm.map ]; then
cp minetest.wasm.map "$RELEASE_DIR"
fi
popd
apply_substitutions() {
local srcfile="$1"
local dstfile="$2"
sed "s/%__RELEASE_UUID__%/$RELEASE_UUID/g" "$srcfile" > "$dstfile"
}
# Copy static files, replacing $RELEASE_UUID with the id
pushd "$BASE_DIR/static"
apply_substitutions htaccess_toplevel "$WWW_DIR"/.htaccess
apply_substitutions index.html "$WWW_DIR"/index.html
apply_substitutions htaccess_release "$RELEASE_DIR"/.htaccess
apply_substitutions launcher.js "$RELEASE_DIR"/launcher.js
popd
# Copy base file system pack
cp "$BUILD_DIR/fsroot.tar.zst" "$PACKS_DIR/base.pack"
echo "DONE"
# Optional script to customize deployment
# Use this to add extra data packs, deploy to webserver, etc
if [ -f deploy.sh ]; then
./deploy.sh
fi

View File

@ -1,4 +1,7 @@
#!/bin/bash -eux
# Incremental build for making changes to only minetest / irrlicht
export INCREMENTAL=true
./build_minetest.sh
./build_www.sh

1
static/htaccess_release Normal file
View File

@ -0,0 +1 @@
Header set Cache-Control "public, max-age=31536000, immutable"

View File

@ -6,6 +6,6 @@
<title>Minetest for the Web</title>
</head>
<body>
<script type="text/javascript" src="launcher.js"></script>
<script type="text/javascript" src="%__RELEASE_UUID__%/launcher.js"></script>
</body>
</html>

View File

@ -1,3 +1,7 @@
// These are relative paths
const RELEASE_DIR = '%__RELEASE_UUID__%'; // set by build_www.sh
const PACKS_DIR = RELEASE_DIR + '/packs';
const rtCSS = `
body {
font-family: arial;
@ -194,7 +198,7 @@ var pendingPacks = 0;
function fetchPack(name) {
pendingPacks += 1;
const xhr = new XMLHttpRequest();
xhr.open('GET', name + '.pack', true);
xhr.open('GET', PACKS_DIR + '/' + name + '.pack', true);
xhr.responseType = 'arraybuffer';
xhr.onprogress = (event) => {
console.log(`Fetched ${event.loaded} of ${event.total}`);
@ -481,6 +485,6 @@ fetchPacks();
// Start loading the wasm module
const mtModuleScript = document.createElement("script");
mtModuleScript.type = "text/javascript";
mtModuleScript.src = "minetest.js";
mtModuleScript.src = RELEASE_DIR + "/minetest.js";
mtModuleScript.async = true;
document.body.appendChild(mtModuleScript);