CORS & Gzip¶
Keywords: cross origin requests, response compression, gzip encoding, cors headers, middleware cors, compress response
CORS (Cross-Origin Resource Sharing)¶
Enable CORS to allow requests from other domains (useful for APIs consumed by a frontend on a different port/domain).
Allow all origins¶
# wsgi.py
from asok import Asok
app = Asok()
app.config['CORS_ORIGINS'] = '*'
Allow specific origins¶
app.config['CORS_ORIGINS'] = ['http://localhost:3000', 'https://myapp.com']
What it does¶
When enabled, Asok adds these headers to responses:
Access-Control-Allow-Origin: <origin>
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Access-Control-Allow-Headers: Content-Type, X-CSRF-Token, Authorization
Preflight OPTIONS requests are handled automatically with a 204 No Content response.
Disabled by default¶
app.config['CORS_ORIGINS'] = None # Default — no CORS headers
Gzip Compression¶
Compress text responses to reduce bandwidth.
Enable¶
# wsgi.py
from asok import Asok
app = Asok()
app.config['GZIP'] = True
How it works¶
Only compresses
text/*content types (HTML, CSS, JSON, etc.)Only compresses if the response is larger than
GZIP_MIN_SIZE(default: 500 bytes)Only compresses if the client sends
Accept-Encoding: gzipAdds
Content-Encoding: gzipheader to compressed responses
Configure minimum size¶
app.config['GZIP_MIN_SIZE'] = 1024 # Only compress responses > 1KB
When to use¶
Development: Leave it off (
GZIP = False)Production without reverse proxy: Turn it on
Production with Nginx/Caddy: Let the reverse proxy handle gzip instead