Last active
December 20, 2015 02:49
-
-
Save chadwhitacre/6059536 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
"""This is an Aspen hook for supporting PJAX. | |
Install it in your project's configure-aspen.py like so: | |
website.hooks.outbound += [aspen_pjax.outbound] | |
""" | |
from pyquery import PyQuery as pq # http://pythonhosted.org/pyquery/ | |
def outbound(response): | |
"""Do PJAX by pulling an element out of the rendered page. | |
""" | |
if not response.headers.get('Content-Type', '').startswith('text/html'): | |
return | |
if response.request.headers.get('X-PJAX') is None: | |
return | |
if response.request.headers.get('X-PJAX-CONTAINER') is None: | |
return | |
selector = response.request.headers['X-PJAX-CONTAINER'] | |
response.body = pq(response.body)(selector).html().encode('utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment