Created
July 10, 2020 03:45
-
-
Save chrisgrande/76107f167d042d43c2562c14a6b8e8a1 to your computer and use it in GitHub Desktop.
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
class TurbolinksMiddleware(object): | |
""" | |
Send the `Turbolinks-Location` header in response to a visit that was redirected, | |
and Turbolinks will replace the browser's topmost history entry. | |
""" | |
def __init__(self, get_response): | |
self.get_response = get_response | |
def __call__(self, request): | |
response = self.get_response(request) | |
is_turbolinks = request.META.get('HTTP_TURBOLINKS_REFERRER') | |
is_response_redirect = response.has_header('Location') | |
if is_turbolinks: | |
if is_response_redirect: | |
location = response['Location'] | |
prev_location = request.session.pop('_turbolinks_redirect_to', None) | |
if prev_location is not None: | |
# relative subsequent redirect | |
if location.startswith('.'): | |
location = prev_location.split('?')[0] + location | |
request.session['_turbolinks_redirect_to'] = location | |
else: | |
if request.session.get('_turbolinks_redirect_to'): | |
location = request.session.pop('_turbolinks_redirect_to') | |
response['Turbolinks-Location'] = location | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment