Skip to content

Commit 63d6e53

Browse files
authored
Frontend - Caching for Webnode (#865)
1 parent 4b5d8a7 commit 63d6e53

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

frontend/docker/startup.sh

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,60 @@ download_wasm_files() {
7979
echo "Error: OPENMINA_WASM_VERSION is not set. Exiting."
8080
exit 1
8181
fi
82-
82+
8383
WASM_URL="$OPENMINA_BASE_URL/openmina/releases/download/$OPENMINA_WASM_VERSION/openmina-$OPENMINA_WASM_VERSION-webnode-wasm.tar.gz"
8484
TARGET_DIR="/usr/local/apache2/htdocs/assets/webnode/pkg"
85-
85+
8686
mkdir -p "$TARGET_DIR"
8787

8888
echo "Downloading WASM files from $WASM_URL..."
8989
curl -s -L --retry 3 --retry-delay 5 -o "/tmp/openmina-$OPENMINA_WASM_VERSION-webnode-wasm.tar.gz" "$WASM_URL"
90-
90+
9191
if [[ $? -ne 0 ]]; then
9292
echo "Failed to download the WASM file after 3 attempts, exiting."
9393
exit 1
9494
else
9595
echo "WASM file downloaded successfully. Extracting to $TARGET_DIR..."
9696

9797
tar -xzf "/tmp/openmina-$OPENMINA_WASM_VERSION-webnode-wasm.tar.gz" -C "$TARGET_DIR"
98-
98+
9999
# Check if the extraction was successful
100100
if [[ $? -ne 0 ]]; then
101101
echo "Failed to extract the WASM file, exiting."
102102
exit 1
103103
else
104104
echo "WASM files extracted successfully to $TARGET_DIR"
105+
106+
# Inject caching logic into openmina_node_web.js
107+
OPENMINA_JS="$TARGET_DIR/openmina_node_web.js"
108+
inject_caching_logic "$OPENMINA_JS"
105109
fi
106110
fi
107111

108112
rm "/tmp/openmina-$OPENMINA_WASM_VERSION-webnode-wasm.tar.gz"
109113
}
110114

115+
inject_caching_logic() {
116+
local js_file="$1"
117+
if [ -f "$js_file" ]; then
118+
echo "Injecting caching logic into $js_file"
119+
120+
# Generate a unique hash
121+
local hash=$(openssl rand -hex 8)
122+
123+
sed -i "/module_or_path = fetch(module_or_path);/i\ module_or_path += \"\?v=${hash}\";" "$js_file"
124+
sed -i 's/module_or_path = fetch(module_or_path);/module_or_path = fetch(module_or_path, { cache: "force-cache", headers: { "Cache-Control": "max-age=31536000, immutable" } });/' "$js_file"
125+
if [[ $? -ne 0 ]]; then
126+
echo "Failed to inject caching logic into $js_file"
127+
else
128+
echo "Successfully injected caching logic into $js_file"
129+
fi
130+
else
131+
echo "Warning: $js_file not found. Caching logic not injected."
132+
fi
133+
}
134+
135+
111136
if [ -n "$OPENMINA_FRONTEND_ENVIRONMENT" ]; then
112137
echo "Using environment: $OPENMINA_FRONTEND_ENVIRONMENT"
113138
cp -f /usr/local/apache2/htdocs/assets/environments/"$OPENMINA_FRONTEND_ENVIRONMENT".js \

frontend/httpd.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,31 @@ SSLProxyEngine On
228228
</Location>
229229

230230
<IfModule unixd_module>
231+
232+
# Define development environments
233+
SetEnvIf Host "localhost|127.0.0.1" DEVELOPMENT
234+
235+
# Cache rules for WebNode assets
236+
<LocationMatch "/assets/webnode/pkg/openmina_node_web\.js">
237+
# Development environment - no cache
238+
Header set Cache-Control "no-store, no-cache, must-revalidate" env=DEVELOPMENT
239+
240+
# Production environment - aggressive caching
241+
Header set Cache-Control "public, max-age=31536000, immutable" env=!DEVELOPMENT
242+
</LocationMatch>
243+
<LocationMatch "/assets/webnode/pkg/openmina_node_web_bg\.wasm">
244+
# Development environment - no cache
245+
Header set Cache-Control "no-store, no-cache, must-revalidate" env=DEVELOPMENT
246+
247+
# Production environment - aggressive caching
248+
Header set Cache-Control "public, max-age=31536000, immutable" env=!DEVELOPMENT
249+
</LocationMatch>
250+
251+
# Make sure mod_headers is enabled
252+
<IfModule !mod_headers.c>
253+
LoadModule headers_module modules/mod_headers.so
254+
</IfModule>
255+
231256
#
232257
# If you wish httpd to run as a different user or group, you must run
233258
# httpd as root initially and it will switch.

frontend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"start": "npm install && ng serve --configuration local --open",
77
"start:dev": "ng serve --configuration development",
88
"build": "ng build",
9-
"build:prod": "ng build --configuration production",
9+
"build:prod": "npm run prebuild && ng build --configuration production",
1010
"tests": "npx cypress open --config baseUrl=http://localhost:4200",
1111
"tests:headless": "npx cypress run --headless --config baseUrl=http://localhost:4200",
1212
"docker": "npm run build:prod && docker buildx build --platform linux/amd64 -t openmina/frontend:latest . && docker push openmina/frontend:latest",
13-
"start:bundle": "npx http-server dist/frontend -p 4200"
13+
"start:bundle": "npx http-server dist/frontend -p 4200",
14+
"prebuild": "node scripts/update-webnode-version.js"
1415
},
1516
"private": true,
1617
"dependencies": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require('fs');
2+
const crypto = require('crypto');
3+
4+
// Generate a random hash
5+
const hash = crypto.randomBytes(16).toString('hex'); // Generates a 32-character random hex string
6+
7+
// Read and update index.html
8+
const indexPath = './src/index.html';
9+
let indexHtml = fs.readFileSync(indexPath, 'utf8');
10+
11+
// Enhanced Regex Pattern
12+
// Match 'const WEBNODE_VERSION = ' with optional whitespace around the equals and between quotes
13+
const versionRegex = /const\s+WEBNODE_VERSION\s*=\s*['"][^'"]*['"];/;
14+
15+
// Perform replacement
16+
indexHtml = indexHtml.replace(versionRegex, `const WEBNODE_VERSION = '${hash}';`);
17+
18+
// Write updated content to index.html
19+
fs.writeFileSync(indexPath, indexHtml);
20+
21+
console.log(`Updated WEBNODE_VERSION in ${indexPath} to ${hash}`);

frontend/src/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@
4242

4343
<body class="f-base">
4444
<script type="module">
45-
import('./assets/webnode/pkg/openmina_node_web.js')
45+
// Get version from build-time replaced variable or from meta tag
46+
// This will be replaced during build (don't change this line!!)
47+
const WEBNODE_VERSION = '13b85f46d3496a8608a86c8af21374bf';
48+
const webNodeUrl = `./assets/webnode/pkg/openmina_node_web.js?v=${WEBNODE_VERSION}`;
49+
50+
import(webNodeUrl)
4651
.then((v) => {
4752
window.webnode = v;
48-
const event = new CustomEvent('webNodeLoaded');
49-
window.dispatchEvent(event);
53+
window.dispatchEvent(new CustomEvent('webNodeLoaded'));
5054
})
5155
.catch((error) => {
5256
if (window.env?.configs.some(c => c.isWebNode)) {

0 commit comments

Comments
 (0)