Performance: File size limits
This affects page load speed and server response time by controlling how large uploaded files can be, preventing slow uploads and server overload.
from flask import Flask, request app = Flask(__name__) app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 # 5 MB limit @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] file.save('/uploads/' + file.filename) return 'Upload complete' if __name__ == '__main__': app.run()
from flask import Flask, request app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] file.save('/uploads/' + file.filename) return 'Upload complete' if __name__ == '__main__': app.run()
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No file size limit | N/A | N/A | Blocks server response causing delayed LCP | [X] Bad |
| Set MAX_CONTENT_LENGTH limit | N/A | N/A | Faster server response, better LCP | [OK] Good |
MAX_CONTENT_LENGTH in a Flask app do?app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
return 'File uploaded!'app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
@app.route('/upload', methods=['POST'])
def upload():
if request.content_length > app.config['MAX_CONTENT_LENGTH']:
return 'File too large', 413
file = request.files['file']
return 'Upload successful'Pass the test and this pattern moves unknown