Skip to content
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

Add support for sending User-Agent string to prerenderer and fix breaking headers issue #20

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ denisvlr - https://github.com/denisvlr
mattrobenolt - https://github.com/mattrobenolt
thoop - https://github.com/thoop
rchrd2 - https://github.com/rchrd2
chazcb - https://github.com/chazcb
chazcb - https://github.com/chazcb
jdotjdot - https://github.com/jdotjdot
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ SEO_JS_IGNORE_EXTENSIONS = [
".txt",
# See helpers.py for full list of extensions ignored by default.
]

# Whether or not to pass along the original request's user agent to the prerender service.
# Useful for analytics, understanding where requests are coming from.
SEO_JS_SEND_USER_AGENT = True
```

## Backend settings
Expand Down Expand Up @@ -135,9 +139,10 @@ Backends must implement the following methods:

class MyBackend(SEOBackendBase):

def get_response_for_url(self, url):
def get_response_for_url(self, url, request=None):
"""
Accepts a fully-qualified url.
Optionally accepts the django request object, so that headers, etc. may be passed along to the prerenderer.
Returns an HttpResponse, passing through all headers and the status code.
"""
raise NotImplementedError
Expand Down
4 changes: 2 additions & 2 deletions django_seo_js/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def build_absolute_uri(self, request):
"""
return request.build_absolute_uri()

def get_response_for_url(self, url):
def get_response_for_url(self, url, request=None):
"""
Accepts a fully-qualified url.
Returns an HttpResponse, passing through all headers and the status code.
Expand All @@ -63,7 +63,7 @@ def __init__(self, *args, **kwargs):
def build_django_response_from_requests_response(self, response):
r = HttpResponse(response.content)
for k, v in response.headers.items():
if k not in IGNORED_HEADERS:
if k.lower() not in IGNORED_HEADERS:
r[k] = v
r['content-length'] = len(response.content)
r.status_code = response.status_code
Expand Down
5 changes: 4 additions & 1 deletion django_seo_js/backends/prerender.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _get_token(self):
raise ValueError("Missing SEO_JS_PRERENDER_TOKEN in settings.")
return settings.PRERENDER_TOKEN

def get_response_for_url(self, url):
def get_response_for_url(self, url, request=None):
"""
Accepts a fully-qualified url.
Returns an HttpResponse, passing through all headers and the status code.
Expand All @@ -29,6 +29,9 @@ def get_response_for_url(self, url):
headers = {
'X-Prerender-Token': self.token,
}
if request and settings.SEND_USER_AGENT:
headers.update({'User-Agent': request.META.get('HTTP_USER_AGENT')})

r = self.session.get(render_url, headers=headers, allow_redirects=False)
assert r.status_code < 500

Expand Down
2 changes: 1 addition & 1 deletion django_seo_js/middleware/escaped_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def process_request(self, request):

url = self.backend.build_absolute_uri(request)
try:
return self.backend.get_response_for_url(url)
return self.backend.get_response_for_url(url, request)
except Exception as e:
logger.exception(e)

Expand Down
2 changes: 1 addition & 1 deletion django_seo_js/middleware/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def process_request(self, request):

url = self.backend.build_absolute_uri(request)
try:
return self.backend.get_response_for_url(url)
return self.backend.get_response_for_url(url, request)
except Exception as e:
logger.exception(e)
2 changes: 2 additions & 0 deletions django_seo_js/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@
PRERENDER_TOKEN = getattr(django_settings, 'SEO_JS_PRERENDER_TOKEN', None)
PRERENDER_URL = getattr(django_settings, 'SEO_JS_PRERENDER_URL', None)
PRERENDER_RECACHE_URL = getattr(django_settings, 'SEO_JS_PRERENDER_RECACHE_URL', None)

SEND_USER_AGENT = getattr(django_settings, 'SEO_JS_SEND_USER_AGENT', True)