Performance: add GZip compression, externalize CSS, bump cache, reduce workers

- Add Starlette GZipMiddleware for response compression (~60% smaller HTML)
- Extract 290 lines of inline CSS to /static/site.css (browser-cacheable)
- Bump HTML Cache-Control from 5min to 1hr
- Reduce Granian workers from 4 to 2 (matches shared-cpu-2x)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 17:43:17 -04:00
parent d12a860544
commit 8267a8e646
5 changed files with 290 additions and 289 deletions
+1 -1
View File
@@ -26,4 +26,4 @@ RUN uv pip install . --system
# Copy the rest of the application
COPY . .
CMD ["granian", "--interface", "asgi", "--host", "0.0.0.0", "--port", "8000", "--workers", "4", "--static-path-route", "/static", "--static-path-mount", "tuftecms/static", "--static-path-expires", "604800", "engine:api"]
CMD ["granian", "--interface", "asgi", "--host", "0.0.0.0", "--port", "8000", "--workers", "2", "--static-path-route", "/static", "--static-path-mount", "tuftecms/static", "--static-path-expires", "604800", "engine:api"]
+4
View File
@@ -95,6 +95,10 @@ api = responder.API(
description="API for kennethreitz.org",
)
# --- GZip compression for all responses ---
from starlette.middleware.gzip import GZipMiddleware
api.add_middleware(GZipMiddleware, minimum_size=500)
# --- Rate limiting ---
from responder.ext.ratelimit import RateLimiter
+1 -1
View File
@@ -206,7 +206,7 @@ def register_context_processors(app):
# Add cache control for HTML (short cache, must revalidate)
if not response.headers.get('Cache-Control'):
response.headers['Cache-Control'] = 'public, max-age=300, must-revalidate'
response.headers['Cache-Control'] = 'public, max-age=3600, must-revalidate'
# Inject a script right after <head> to immediately show content
content = response.get_data(as_text=True)
+282
View File
@@ -0,0 +1,282 @@
/* Prevent FOUC by hiding content until stylesheets load */
html {
visibility: hidden;
opacity: 0;
}
html.loaded {
visibility: visible;
opacity: 1;
transition: opacity 0.1s ease-in;
}
/* Legend dots for content guide */
.legend-dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 0.3rem;
vertical-align: middle;
}
/* Reading progress indicator */
.reading-progress {
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 2px;
background: #333;
z-index: 1000;
transition: width 0.1s ease-out;
}
/* Header and nav z-index for dropdown stacking */
header {
position: relative;
z-index: 9999;
}
nav {
position: relative;
z-index: 9999;
}
nav > a, nav > .nav-dropdown {
margin-right: 1rem;
}
.nav-dropdown {
position: relative;
display: inline-block;
}
.nav-dropdown-trigger {
cursor: pointer;
color: inherit;
text-decoration: underline;
padding-bottom: 1rem;
display: inline-block;
}
.nav-dropdown-content {
display: none;
position: absolute;
top: 100%;
left: 0;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
min-width: 250px;
max-width: 400px;
max-height: 500px;
overflow-y: auto;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 10000;
padding: 0.75rem;
}
.nav-dropdown:hover .nav-dropdown-content,
.nav-dropdown-content:hover {
display: block;
}
.tree-item {
padding: 0.35rem 0;
display: flex;
align-items: center;
gap: 0.5rem;
}
.tree-item-icon {
flex-shrink: 0;
width: 18px;
height: 18px;
}
.tree-item a {
color: #333;
text-decoration: none;
flex: 1;
}
.tree-item a:hover {
text-decoration: underline;
}
.tree-folder a {
font-weight: 500;
}
body.dark-mode .nav-dropdown-content {
background: #1a1a1a;
border-color: #333;
}
body.dark-mode .tree-item a {
color: #ccc;
}
@media (prefers-color-scheme: dark) {
.nav-dropdown-content {
background: #1a1a1a;
border-color: #333;
}
.tree-item a {
color: #ccc;
}
}
.github-corner {
position: absolute;
top: 0;
right: 0;
z-index: 9998;
opacity: 0.85;
transition: opacity 0.3s ease;
}
.github-corner:hover {
opacity: 0.8;
}
.github-corner-img {
width: 80px;
height: 80px;
}
body.dark-mode .github-corner {
opacity: 0.25;
}
body.dark-mode .github-corner-img {
filter: invert(1);
}
@media (max-width: 760px) {
.github-corner-img {
width: 50px;
height: 50px;
}
}
.theme-toggle {
position: fixed;
bottom: 2rem;
right: 2rem;
background: none;
border: none;
cursor: pointer;
font-size: 1.5rem;
padding: 0.5rem;
margin: 0;
opacity: 0.15;
transition: opacity 0.3s ease;
line-height: 1;
z-index: 9999;
}
.theme-toggle:hover {
opacity: 0.8;
}
.sun-icon,
.moon-icon {
display: none;
}
body.light-mode .sun-icon {
display: inline;
}
body.dark-mode .moon-icon {
display: inline;
}
/* Dark mode styles using body class */
body.dark-mode {
background-color: #0d1117;
color: #c9d1d9;
}
/* Light mode styles to override media query */
body.light-mode {
background-color: #fffff8;
color: #111;
}
@media (max-width: 760px) {
.theme-toggle {
font-size: 1.3rem;
bottom: 1rem;
right: 1rem;
padding: 0.4rem;
}
}
main {
position: relative;
z-index: 1;
}
article {
position: relative;
z-index: 1;
}
/* Dropdown navigation styles */
.nav-dropdown {
position: relative;
display: inline-block;
padding: 0.5rem;
margin: -0.5rem;
z-index: 9999;
}
.nav-dropdown .dropdown-content {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
z-index: 999999;
min-width: 280px;
padding: 0.5rem 0;
margin-top: -1px;
}
.nav-dropdown:hover .dropdown-content {
display: block;
}
.nav-dropdown .dropdown-content a {
display: block;
padding: 0.5rem 1rem;
color: #333;
text-decoration: none;
border-bottom: none;
font-size: 0.9rem;
line-height: 1.4;
}
.nav-dropdown .dropdown-content a:hover {
background-color: #f5f5f5;
}
.nav-dropdown .dropdown-content a .index-description {
display: block;
font-size: 0.75rem;
color: #666;
margin-top: 0.1rem;
}
.nav-dropdown > a::after {
content: "\00a0\25BC";
font-size: 0.7rem;
color: #999;
}
+2 -287
View File
@@ -49,293 +49,8 @@
<!-- Custom Site CSS -->
<link rel="stylesheet" href="/static/custom.css" />
<style>
/* Prevent FOUC by hiding content until stylesheets load */
html {
visibility: hidden;
opacity: 0;
}
html.loaded {
visibility: visible;
opacity: 1;
transition: opacity 0.1s ease-in;
}
/* Only page-specific or dynamic styles that can't be in external CSS */
/* Legend dots for content guide */
.legend-dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 0.3rem;
vertical-align: middle;
}
/* Reading progress indicator */
.reading-progress {
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 2px;
background: #333;
z-index: 1000;
transition: width 0.1s ease-out;
}
/* Header and nav z-index for dropdown stacking */
header {
position: relative;
z-index: 9999;
}
nav {
position: relative;
z-index: 9999;
}
nav > a, nav > .nav-dropdown {
margin-right: 1rem;
}
.nav-dropdown {
position: relative;
display: inline-block;
}
.nav-dropdown-trigger {
cursor: pointer;
color: inherit;
text-decoration: underline;
padding-bottom: 1rem;
display: inline-block;
}
.nav-dropdown-content {
display: none;
position: absolute;
top: 100%;
left: 0;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
min-width: 250px;
max-width: 400px;
max-height: 500px;
overflow-y: auto;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 10000;
padding: 0.75rem;
}
.nav-dropdown:hover .nav-dropdown-content,
.nav-dropdown-content:hover {
display: block;
}
.tree-item {
padding: 0.35rem 0;
display: flex;
align-items: center;
gap: 0.5rem;
}
.tree-item-icon {
flex-shrink: 0;
width: 18px;
height: 18px;
}
.tree-item a {
color: #333;
text-decoration: none;
flex: 1;
}
.tree-item a:hover {
text-decoration: underline;
}
.tree-folder a {
font-weight: 500;
}
body.dark-mode .nav-dropdown-content {
background: #1a1a1a;
border-color: #333;
}
body.dark-mode .tree-item a {
color: #ccc;
}
@media (prefers-color-scheme: dark) {
.nav-dropdown-content {
background: #1a1a1a;
border-color: #333;
}
.tree-item a {
color: #ccc;
}
}
.github-corner {
position: absolute;
top: 0;
right: 0;
z-index: 9998;
opacity: 0.85;
transition: opacity 0.3s ease;
}
.github-corner:hover {
opacity: 0.8;
}
.github-corner-img {
width: 80px;
height: 80px;
}
body.dark-mode .github-corner {
opacity: 0.25;
}
body.dark-mode .github-corner-img {
filter: invert(1);
}
@media (max-width: 760px) {
.github-corner-img {
width: 50px;
height: 50px;
}
}
.theme-toggle {
position: fixed;
bottom: 2rem;
right: 2rem;
background: none;
border: none;
cursor: pointer;
font-size: 1.5rem;
padding: 0.5rem;
margin: 0;
opacity: 0.15;
transition: opacity 0.3s ease;
line-height: 1;
z-index: 9999;
}
.theme-toggle:hover {
opacity: 0.8;
}
.sun-icon,
.moon-icon {
display: none;
}
body.light-mode .sun-icon {
display: inline;
}
body.dark-mode .moon-icon {
display: inline;
}
/* Dark mode styles using body class */
body.dark-mode {
background-color: #0d1117;
color: #c9d1d9;
}
/* Light mode styles to override media query */
body.light-mode {
background-color: #fffff8;
color: #111;
}
@media (max-width: 760px) {
.theme-toggle {
font-size: 1.3rem;
bottom: 1rem;
right: 1rem;
padding: 0.4rem;
}
}
main {
position: relative;
z-index: 1;
}
article {
position: relative;
z-index: 1;
}
/* Dropdown navigation styles */
.nav-dropdown {
position: relative;
display: inline-block;
padding: 0.5rem;
margin: -0.5rem;
z-index: 9999;
}
.nav-dropdown .dropdown-content {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
z-index: 999999;
min-width: 280px;
padding: 0.5rem 0;
margin-top: -1px;
}
.nav-dropdown:hover .dropdown-content {
display: block;
}
.nav-dropdown .dropdown-content a {
display: block;
padding: 0.5rem 1rem;
color: #333;
text-decoration: none;
border-bottom: none;
font-size: 0.9rem;
line-height: 1.4;
}
.nav-dropdown .dropdown-content a:hover {
background-color: #f5f5f5;
}
.nav-dropdown .dropdown-content a .index-description {
display: block;
font-size: 0.75rem;
color: #666;
margin-top: 0.1rem;
}
.nav-dropdown > a::after {
content: "\00a0▼";
font-size: 0.7rem;
color: #999;
}
</style>
<!-- Site CSS (layout, nav, dark mode, etc.) -->
<link rel="stylesheet" href="/static/site.css" />
<!-- Structured Data (JSON-LD) -->