Fix smaller performance issues

This commit is contained in:
2026-02-19 19:03:39 +01:00
parent 91d9fa3a21
commit 88450fd8b6
4 changed files with 74 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ from pathlib import Path
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import RedirectResponse
from fastapi.responses import FileResponse, RedirectResponse
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.gzip import GZipMiddleware
from loguru import logger
@@ -104,6 +104,15 @@ app.add_middleware(GZipMiddleware, minimum_size=500)
# Static files
app.mount("/static", StaticFiles(directory=Path(__file__).parent / "static"), name="static")
@app.get("/sw.js", include_in_schema=False)
async def service_worker():
"""Serve Service Worker from root scope so it can intercept all page requests."""
response = FileResponse(Path(__file__).parent / "static/sw.js", media_type="application/javascript")
response.headers["Service-Worker-Allowed"] = "/"
response.headers["Cache-Control"] = "no-cache"
return response
# Include admin router (always available)
app.include_router(admin_router)

48
src/web/static/sw.js Normal file
View File

@@ -0,0 +1,48 @@
const CACHE_NAME = 'linkedin-shell-v1';
const PRECACHE_URLS = [
'/static/tailwind.css',
'/static/tailwind-employee.css',
'/static/logo.png',
'/static/favicon.png',
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(PRECACHE_URLS);
}).then(function() {
return self.skipWaiting();
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames
.filter(function(name) { return name !== CACHE_NAME; })
.map(function(name) { return caches.delete(name); })
);
}).then(function() {
return self.clients.claim();
})
);
});
self.addEventListener('fetch', function(event) {
if (!event.request.url.includes('/static/')) {
return;
}
event.respondWith(
caches.match(event.request).then(function(cached) {
return cached || fetch(event.request).then(function(response) {
var clone = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(event.request, clone);
});
return response;
});
})
);
});

View File

@@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}LinkedIn Posts{% endblock %}</title>
<link rel="icon" type="image/png" href="/static/favicon.png">
<link rel="preload" href="/static/tailwind.css" as="style">
<link rel="stylesheet" href="/static/tailwind.css">
<style>
body { background-color: #3d4848; }
@@ -362,5 +363,12 @@
</script>
{% block scripts %}{% endblock %}
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').catch(function() {});
});
}
</script>
</body>
</html>

View File

@@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{{ session.company_name or 'Unternehmen' }} - LinkedIn Posts{% endblock %}</title>
<link rel="icon" type="image/png" href="/static/favicon.png">
<link rel="preload" href="/static/tailwind.css" as="style">
<link rel="stylesheet" href="/static/tailwind.css">
<style>
body { background-color: #3d4848; }
@@ -206,5 +207,12 @@
</script>
{% block scripts %}{% endblock %}
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').catch(function() {});
});
}
</script>
</body>
</html>