-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error page optimisations #299
Draft
RealOrangeOne
wants to merge
5
commits into
main
Choose a base branch
from
error-page-optimisations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2c4fad
Use simpler error pages when HTML isn't needed
RealOrangeOne 8109908
Cache 404s for 15 minutes
RealOrangeOne 89ae354
Test missing and wildcard Accept headers
RealOrangeOne 4cad762
Flip comment
RealOrangeOne 489ae30
Allow Cloudflare to cache 404s
RealOrangeOne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,9 +1,68 @@ | ||||
from django.http import HttpResponseNotFound, HttpResponseServerError | ||||
from django.views import defaults | ||||
from django.views.decorators.cache import cache_control | ||||
from django.views.decorators.csrf import requires_csrf_token | ||||
from django.views.decorators.vary import vary_on_headers | ||||
|
||||
|
||||
def page_not_found(request, exception, template_name="patterns/pages/errors/404.html"): | ||||
return defaults.page_not_found(request, exception, template_name) | ||||
def get_quality(media_type): | ||||
return float(media_type.params.get("q", 1)) | ||||
|
||||
|
||||
def show_html_error_page(request): | ||||
# If there is no `Accept` header, serve the simpler page | ||||
if not request.headers.get("Accept"): | ||||
return False | ||||
|
||||
accepted_types = sorted(request.accepted_types, key=get_quality, reverse=True) | ||||
|
||||
if len(accepted_types) == 1 and accepted_types[0].match("*/*"): | ||||
return False | ||||
|
||||
html_type = next( | ||||
( | ||||
accepted_type | ||||
for accepted_type in accepted_types | ||||
if accepted_type.match("text/html") | ||||
), | ||||
None, | ||||
) | ||||
|
||||
# If HTML isn't accepted, don't serve it | ||||
if html_type is None: | ||||
return False | ||||
|
||||
max_quality = get_quality(accepted_types[0]) | ||||
|
||||
# If HTML isn't the highest quality, don't serve it | ||||
if get_quality(html_type) < max_quality: | ||||
return False | ||||
|
||||
return True | ||||
|
||||
|
||||
@requires_csrf_token | ||||
@vary_on_headers("Accept") | ||||
@cache_control(max_age=900) # 15 minutes | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this maybe set torchbox.com/tbx/core/utils/cache.py Line 23 in 03bc0e5
|
||||
def page_not_found( | ||||
request, exception=None, template_name="patterns/pages/errors/404.html" | ||||
): | ||||
if show_html_error_page(request): | ||||
return defaults.page_not_found(request, exception, template_name) | ||||
|
||||
# Serve a simpler, cheaper 404 page if possible | ||||
return HttpResponseNotFound( | ||||
"Page not found", content_type="text/plain; charset=utf-8" | ||||
) | ||||
|
||||
|
||||
@requires_csrf_token | ||||
@vary_on_headers("Accept") | ||||
def server_error(request, template_name="patterns/pages/errors/500.html"): | ||||
return defaults.server_error(request, template_name) | ||||
if show_html_error_page(request): | ||||
return defaults.server_error(request, template_name) | ||||
|
||||
# Serve a simpler, cheaper 500 page if possible | ||||
return HttpResponseServerError( | ||||
"Server error", content_type="text/plain; charset=utf-8" | ||||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do these need csrf? Doesn't that skip the cache completely?