Skip to content

Instantly share code, notes, and snippets.

@chadwhitacre chadwhitacre/aspen_pjax.py
Last active Dec 20, 2015

Embed
What would you like to do?
"""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
You can’t perform that action at this time.