Skip to content
This repository has been archived by the owner on Dec 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #20 from Pageworks/develop
Browse files Browse the repository at this point in the history
Pjax v2.3.0
  • Loading branch information
Kyle Andrews authored Aug 26, 2019
2 parents 7d0f0ab + 5b2c318 commit 5ce322a
Show file tree
Hide file tree
Showing 15 changed files with 6,878 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.DS_Store
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.3.0 - 2019-08-26

### Added

- Adds: developers can enable the `requireCssBeforeComplete` flag to force Pjax to fetch and append all the CSS stylesheets before firing the `pjax:complete` event

## 2.2.1 - 2019-08-23

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ You can define custom Pjax options using the following:
| importScripts | boolean | `true` |
| importCSS | boolean | `true` |
| scriptImportLocation | HTMLElement | `document.head` |
| requireCssBeforeComplete | boolean | `false` |

`elements` is the base element users should click on to trigger a page transition.

Expand All @@ -127,6 +128,8 @@ When `importCSS` is `true` Pjax will dynamically fetch and append custom `<style

`scriptImportLocation` is the `HTMLElement` that the dynamically fetched `<script>` elements will be appended upon. Defaults to `document.head`.

When `requireCssBeforeComplete` is `true` Pjax will delay firing the `pjax:complete` event until all the CSS fetch request have been resolved.

### Pjax Events

Pjax fires a handful of events on the `document` that you can listen for.
Expand Down
1 change: 1 addition & 0 deletions lib/parse-options.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/parse-options.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pageworks/pjax",
"version": "2.2.1",
"version": "2.3.0",
"description": "Turns any website into a SPA using Fetch and link prefetching.",
"keywords": [
"pjax",
Expand Down
86 changes: 84 additions & 2 deletions pjax.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pjax.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare namespace Pjax{
importScripts?: boolean;
importCSS?: boolean;
scriptImportLocation?: HTMLElement|HTMLHeadElement;
requireCssBeforeComplete?: boolean;
}

export interface IScrollPosition{
Expand Down
1 change: 1 addition & 0 deletions src/lib/parse-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export default (options:PJAX.IOptions = null)=>{
parsedOptions.importScripts = (options !== null && options.importScripts !== undefined) ? options.importScripts : true;
parsedOptions.importCSS = (options !== null && options.importCSS !== undefined) ? options.importCSS : true;
parsedOptions.scriptImportLocation = (options !== null && options.scriptImportLocation !== undefined && options.scriptImportLocation instanceof HTMLElement) ? options.scriptImportLocation : document.head;
parsedOptions.requireCssBeforeComplete = (options !== null && options.requireCssBeforeComplete !== undefined) ? options.requireCssBeforeComplete : false;
return parsedOptions;
}
120 changes: 113 additions & 7 deletions src/pjax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import PJAX from './global';

export default class Pjax{

public static VERSION:string = '2.2.1';
public static VERSION:string = '2.3.0';

public options : PJAX.IOptions;
private _cache : PJAX.ICacheObject;
Expand Down Expand Up @@ -373,11 +373,47 @@ export default class Pjax{
}

if(this.options.importCSS){
this.handleCSS(tempDocument);
if (!this.options.requireCssBeforeComplete)
{
this.handleCSS(tempDocument);
}
else
{
this.handleSynchronousCss(tempDocument)
.then(()=>{
// Check if Pjax needs to wait for the continue event
if(!this.options.customTransitions){
if(this.options.titleSwitch){
document.title = tempDocument.title;
}
this.handleSwitches(switchQueue);
}else{
this._cachedSwitch = {
queue: switchQueue,
title: tempDocument.title
};

if (this._transitionFinished)
{
if (this.options.titleSwitch)
{
document.title = this._cachedSwitch.title;
}
this.handleSwitches(this._cachedSwitch.queue);
}
}
});
}
}

if (this.options.importCSS && this.options.requireCssBeforeComplete)
{
return;
}

// Check if Pjax needs to wait for the continue event
if(!this.options.customTransitions){
if (!this.options.customTransitions)
{
if(this.options.titleSwitch){
document.title = tempDocument.title;
}
Expand Down Expand Up @@ -636,9 +672,10 @@ export default class Pjax{
* Append any `<style>` or `<link rel="stylesheet">` elements onto the current documents head
* @param newDocument - `HTMLDocument`
*/
private handleCSS(newDocument:HTMLDocument):void{

if(newDocument instanceof HTMLDocument){
private handleCSS(newDocument:HTMLDocument) : void
{
if (newDocument instanceof HTMLDocument)
{
const newStyles:Array<HTMLLinkElement> = Array.from(newDocument.querySelectorAll('link[rel="stylesheet"]'));
const currentStyles:Array<HTMLElement> = Array.from(document.querySelectorAll('link[rel="stylesheet"], style[href]'));
const stylesToAppend:Array<HTMLLinkElement> = [];
Expand All @@ -654,7 +691,8 @@ export default class Pjax{
}
});

if(appendStyle){
if (appendStyle)
{
stylesToAppend.push(newStyle);
}
});
Expand Down Expand Up @@ -686,6 +724,74 @@ export default class Pjax{
}
}

private handleSynchronousCss(newDocument:HTMLDocument) : Promise<any>
{
return new Promise((resolve)=>{
if (newDocument instanceof HTMLDocument)
{
const newStyles:Array<HTMLLinkElement> = Array.from(newDocument.querySelectorAll('link[rel="stylesheet"]'));
const currentStyles:Array<HTMLElement> = Array.from(document.querySelectorAll('link[rel="stylesheet"], style[href]'));
const stylesToAppend:Array<HTMLLinkElement> = [];
let fetched = 0;

newStyles.forEach((newStyle)=>{
let appendStyle = true;
const newStyleFile = newStyle.getAttribute('href').match(/[^/]+$/g)[0];

currentStyles.forEach((currentStyle)=>{
const currentStyleFile = currentStyle.getAttribute('href').match(/[^/]+$/g)[0];
if (newStyleFile === currentStyleFile)
{
appendStyle = false;
}
});

if (appendStyle)
{
stylesToAppend.push(newStyle);
}
});

// Append the new `link` styles to the head
if (stylesToAppend.length)
{
stylesToAppend.forEach((style)=>{
fetch(style.href, {
method: 'GET',
credentials: 'include',
headers: new Headers({
'X-Requested-With': 'XMLHttpRequest'
})
})
.then(request => request.text())
.then(response => {
const newStyle = document.createElement('style');
newStyle.setAttribute('rel', 'stylesheet');
newStyle.setAttribute('href', style.href);
newStyle.innerHTML = response;
document.head.appendChild(newStyle);
})
.catch(error => {
console.error('Failed to fetch stylesheet', style.href, error);
})
.then(()=>{
fetched++;

if (fetched === stylesToAppend.length)
{
resolve();
}
});
});
}
else
{
resolve();
}
}
});
}

/**
* Handles the fetch `Response` based on the load type.
* @param response - `Reponse` object provided by the Fetch API
Expand Down
2 changes: 1 addition & 1 deletion testing/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var useTransition = true;
* IIFE for starting Pjax on load
*/
(function(){
var pjax = new Pjax({ debug:true, customTransitions: useTransition });
var pjax = new Pjax({ debug:true });

var prefetchLight = document.body.querySelector('.js-prefetch');
var loadLight = document.body.querySelector('.js-load');
Expand Down
Loading

0 comments on commit 5ce322a

Please sign in to comment.