Skip to content

Commit fd50d60

Browse files
committed
🚀 Performance optimization: Smart caching strategy and safe optimizations
- Update cache headers: CSS/JS to 24h, images to 1 week (from 10min) - Add safe performance script with font optimization and lazy loading - Maintain layout integrity while improving page load times - Estimated savings: 90% reduction in CSS/JS re-downloads
1 parent fa3e822 commit fd50d60

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎docs/_headers‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,57 @@
1717
Cross-Origin-Opener-Policy: same-origin
1818
Cross-Origin-Resource-Policy: same-origin
1919

20+
# Cache headers for different asset types
2021
/assets/*
22+
# Cache static assets for 24 hours (good balance of performance and updateability)
23+
Cache-Control: public, max-age=86400, must-revalidate
24+
2125
# Less restrictive CSP for assets
2226
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://gc.zgo.at https://safeaiaus.goatcounter.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self' https://safeaiaus.goatcounter.com; frame-src 'none'; object-src 'none'; base-uri 'self'; form-action 'self'
27+
28+
# Cache CSS files for 24 hours (good balance)
29+
*.css
30+
Cache-Control: public, max-age=86400, must-revalidate
31+
32+
# Cache JavaScript files for 24 hours (good balance)
33+
*.js
34+
Cache-Control: public, max-age=86400, must-revalidate
35+
36+
# Cache images for 1 week (they change less frequently)
37+
*.png
38+
Cache-Control: public, max-age=604800, must-revalidate
39+
40+
*.jpg
41+
Cache-Control: public, max-age=604800, must-revalidate
42+
43+
*.jpeg
44+
Cache-Control: public, max-age=604800, must-revalidate
45+
46+
*.gif
47+
Cache-Control: public, max-age=604800, must-revalidate
48+
49+
*.svg
50+
Cache-Control: public, max-age=604800, must-revalidate
51+
52+
*.webp
53+
Cache-Control: public, max-age=604800, must-revalidate
54+
55+
# Cache fonts for 1 week (they rarely change)
56+
*.woff
57+
Cache-Control: public, max-age=604800, must-revalidate
58+
59+
*.woff2
60+
Cache-Control: public, max-age=604800, must-revalidate
61+
62+
*.ttf
63+
Cache-Control: public, max-age=604800, must-revalidate
64+
65+
*.eot
66+
Cache-Control: public, max-age=604800, must-revalidate
67+
68+
# Cache HTML pages for shorter time (1 hour)
69+
*.html
70+
Cache-Control: public, max-age=3600, must-revalidate
71+
72+
*.md
73+
Cache-Control: public, max-age=3600, must-revalidate
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Safe Performance Optimizations for SafeAI-Aus
2+
// This script only adds lightweight optimizations that won't break the layout
3+
4+
(function() {
5+
'use strict';
6+
7+
// Safe font optimization - only adds font-display: swap to Google Fonts
8+
function optimizeFonts() {
9+
const fontLinks = document.querySelectorAll('link[href*="fonts.googleapis.com"]');
10+
fontLinks.forEach(link => {
11+
if (!link.href.includes('display=')) {
12+
link.href += (link.href.includes('?') ? '&' : '?') + 'display=swap';
13+
}
14+
});
15+
}
16+
17+
// Safe image optimization - only adds lazy loading to images below the fold
18+
function optimizeImages() {
19+
const images = document.querySelectorAll('img:not([loading])');
20+
images.forEach(img => {
21+
// Only add lazy loading to images that are below the fold
22+
if (img.offsetTop > window.innerHeight) {
23+
img.loading = 'lazy';
24+
}
25+
});
26+
}
27+
28+
// Safe resource hints - only adds DNS prefetch for external domains
29+
function addResourceHints() {
30+
const externalDomains = [
31+
'fonts.googleapis.com',
32+
'fonts.gstatic.com',
33+
'gc.zgo.at',
34+
'safeaiaus.goatcounter.com'
35+
];
36+
37+
externalDomains.forEach(domain => {
38+
if (!document.querySelector(`link[href="//${domain}"]`)) {
39+
const link = document.createElement('link');
40+
link.rel = 'dns-prefetch';
41+
link.href = `//${domain}`;
42+
document.head.appendChild(link);
43+
}
44+
});
45+
}
46+
47+
// Initialize only safe optimizations
48+
function init() {
49+
// Run immediately
50+
optimizeFonts();
51+
addResourceHints();
52+
53+
// Run after DOM is ready
54+
if (document.readyState === 'loading') {
55+
document.addEventListener('DOMContentLoaded', optimizeImages);
56+
} else {
57+
optimizeImages();
58+
}
59+
}
60+
61+
// Start optimizations
62+
init();
63+
64+
})();

‎mkdocs.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extra_css:
5656

5757
# Add custom JavaScript for SEO and social media optimization
5858
extra_javascript:
59+
- assets/performance-safe.js
5960
- assets/extra.js
6061

6162
markdown_extensions:

0 commit comments

Comments
 (0)