Pjax enables fast and easy AJAX navigation on any website using
fetch. No more full page reloads, no more multiple HTTP request, and written entirely in TypeScript.
Download Pjax via NPM:
npm i --save @pageworks/pjax
Once the package is installed import the package:
Then it's as simple as starting a new instance:
How Pjax Works
Pjax loads pages using the Fetch API and updates the browser's current URL using a
onmouseenter event for links and prefetches the pages HTML. Dpending on what the user does determines Pjax's response. If the user clicks the link before the server responds Pjax will notice that the user wants the page and will switch out the content as soon as the server responds. Finally, if the user remains hovered and the server has already responded Pjax will cache the new pages HTML content and will wait until the user clicks the link or triggers the
onmouseleave event causing Pjax to clear the cached HTML. When combining prefetching and the ability to swap out content without causing a full page reload results in very fast page load responses.
Under the hood Pjax is one HTTP request with a
What Pjax's All About
- Multiple container support
- Fully supports browser history (window popstates)
- Automagically falls back to standard navigation for external pages
- Automagically falls back to standard navigation for internal pages that do not have an appropriate DOM tree
- Allows for modern CSS page transitions (animation) easily
- Is very lightweight
Under the Hood
- Pjax attempts to prefetch internal links for the fastest possible load time
- Checks that all defined parts can be replaced:
- If the page doesn't meet the requirements Pjax will do nothing and standard navigation is used
- If the page meets requirements Pjax swaps the DOM elements
- Pjax updates the browser's current URL using
Start by setting up the basic
index.html file for your website.
Index | Pjax TestingHomeIndex PageIndexPage 1
In the main/application script for your project you can being using Pjax with the following:
;const pjax =debug: true;
You can define custom Pjax options using the following:
elements is the base element users should click on to trigger a page transition.
selectors is an array of containers that Pjax should swap.
history is true Pjax will use
window.history.pushState() to manipulate the browsers history.
cacheBust will add a
GET param to all request forcing the browser to perform the request instead of using a cached version of the page.
debug will tell Pjax to display all debug information.
titleSwitch when true will swap out the documents title during page transitions.
customTransitions when true Pjax won't actually switch out the content until the developers application sends a custom
customPreventionAttributes is an array of custom element attributes that Pjax will look for when attaching event listeners. The default prevention attribute that is
prevent-pjax however you can define additional attributes. For example, if you are using a custom lightcase modal libary you could tell Pjax not to hijack the events attached to any element that has a valid
href attribute when the element also has a
true Pjax will dynamically fetch and append all
<script> elements. Elements with a valid
true Pjax will dynamically fetch and append custom
<style> elements to the documents
<link> elements labeled as
rel="stylesheet" with a valid
href attribute will be appended. Custom styles will only be appended once.
scriptImportLocation is the
HTMLElement that the dynamically fetched
<script> elements will be appended upon. Defaults to
true Pjax will delay firing the
pjax:complete event until all the CSS fetch request have been resolved.
Pjax fires a handful of events on the
document that you can listen for.
Pjax listens for a
pjax:continue event on the
document. This is only used when the
customTransitions option is set to
true. Pjax will NOT swap content until it receives this event.
pjax:scriptContentLoaded will fire on the
document when all the new scripts have been fetched and appended to the body.
Pjax sets two custom status classes on the
document element that you can use in your CSS to style your page transitions. In the example below we set all elements to use the
wait cursor while the
dom-is-loading class is set. Once the
pjax:error events fire the
dom-is-loading class is removed and the
dom-is-loaded class is applied.
Pjax allows developers to manually trigger a page load by using the public static method