Skip to content

Commit

Permalink
Merge pull request #99 from jocxfin/fix-service-worker-bug
Browse files Browse the repository at this point in the history
Fix service worker bug causing infinite load #97
  • Loading branch information
jocxfin authored Feb 21, 2025
2 parents ece7fc5 + ff28e1e commit 208c1a6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN apk update && apk upgrade --no-cache && \
pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5069
CMD ["gunicorn", "-w", "2", "-t", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:5069", "app:app_asgi"]
CMD ["sh", "-c", "rm -rf /app/__pycache__ && gunicorn -w 2 -t 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:5069 app:app_asgi"]
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def serve_manifest():

@app.route(config.BASE_PATH + '/service-worker.js')
def serve_sw():
return send_file('service-worker.js', mimetype='application/javascript')
response = send_file('service-worker.js', mimetype='application/javascript')
response.headers['Cache-Control'] = 'no-store'
return response

app_asgi = WsgiToAsgi(app)
11 changes: 10 additions & 1 deletion service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ self.addEventListener('fetch', (event) => {

caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
if (responseToCache) {
cache.put(event.request, responseToCache);
}
});

return response;
});
})
);
});

function hardReload() {
self.skipWaiting();
clients.matchAll({ type: 'window' }).then(windowClients => {
windowClients.forEach(windowClient => windowClient.navigate(windowClient.url));
});
}
31 changes: 26 additions & 5 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ async function generatePassword() {
if (data.passwords && Array.isArray(data.passwords)) {
data.passwords.forEach((pwd, index) => {
if (index < 5) {
document.querySelector(`.multipw${index}`).textContent = pwd;
const element = document.querySelector(`.multipw${index}`);
if (element) {
element.textContent = pwd;
}
refreshpw.classList.remove('loading');
}
});
} else {
passwordInput.value = data.password;
scrambleAnimation(data.password);
if (passwordInput) {
passwordInput.value = data.password;
scrambleAnimation(data.password);
}
refreshpw.classList.remove('loading');
}
})
Expand All @@ -85,14 +90,18 @@ async function generatePassword() {

function scrambleAnimation(finalPassword) {
let scrambled = Array.from({ length: finalPassword.length }, () => getRandomCharacter());
passwordInput.value = scrambled.join('');
if (passwordInput) {
passwordInput.value = scrambled.join('');
}
const maxDelay = 300;

finalPassword.split('').forEach((char, index) => {
let delay = Math.random() * maxDelay;
setTimeout(() => {
scrambled[index] = char;
passwordInput.value = scrambled.join('');
if (passwordInput) {
passwordInput.value = scrambled.join('');
}
}, delay);
});
}
Expand Down Expand Up @@ -172,3 +181,15 @@ function copyPassword(index) {
document.body.removeChild(textArea);
}
}

function hardReload() {
if (navigator.serviceWorker && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({ action: 'hard-reload' });
} else {
window.location.reload(true);
}
}

if (!navigator.serviceWorker || !navigator.serviceWorker.controller) {
hardReload();
}

0 comments on commit 208c1a6

Please sign in to comment.