npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

little-pjax

v0.4.0

Published

A simple, small and modern implementation of pjax

Downloads

9

Readme

little pjax

CC0-1.0 npm version styled with prettier

pushState + ajax (fetch) = pjax

A simple, small and modern implementation of pjax

Prior arts

  • pjax-api: A full-featured pjax written in TypeScript
  • barba.js: A library that provides smooth transition
  • jquery-pjax: A jQuery plugin for pjax
  • pjax: A standalone pjax

Install

npm install --save-dev little-pjax

Usage

import PJAX from "little-pjax";

const pjax = new PJAX("#pjax-root", "a[href]:not[target]");

pjax.addEventListener("loadend", e => console.log("finish loading"), false);

PJAX will replace:

  • document.title
  • A container element
  • URL

Target anchor elements are selected via the second argument from a container element.
If href of a target anchor element is not same Origin, it will be ignored.

<script> in a container element will be loaded after the element is replaced.

The pages visited before are cached inside PJAX.
Cache stores page data for one session. When reloading a page, it gets cleared.

PJAX will NOT work:

  • with Shift + click, Ctrl + click, Alt + click
  • if a click event was prevented before
  • if the new URL is same with the current one

PJAX does NOT support:

  • Multiple container elements
  • Other elements in <head> such as <meta name="twitter:card">, <script>
  • Inheritance of element's event handlers to the new elements

You have to recall element.addEventListener() after contentLoaded or load event.

You can use the contentLoaded event to hook to sync <meta name="twitter:card">.

API

PJAX()

PJAX < EventTarget

const pjax = new PJAX(container, target);
Options

container: string = body
A group of selectors of a container element.

target : string = a[href]:not([target])
A group of selectors of target HTMLAnchorElement.

Methods

Same as EventTarget. addEventListener and removeEventListener.

Events

All events are instances of CustomEvent and fired from an instance of PJAX.

loadstart -> beforeunload -> unload -> contentLoaded -> loadend

loadstart -> beforeunload -------- (canceled) --------> loadend

loadstart

Fired when have started loading the next page.

beforeunload

Cancelable.

Fired before fetching new resouces.

When canceled, PJAX will stop loading and will not fire unload and contentLoaded.

Properties

contentLoadedEvent.detail.url: string
a URL of the new page.

unload

Fired before replacing a container element.

contentLoaded

Fired after replacing a container element and before executing JavaScript.

Properties

contentLoadedEvent.detail.document: Document
Document of the new page.

You can use it, for example, to sync <meta name="twitter:card">.

pjax.addEventListener("contentLoaded", event => {
  const newDocument = event.detail.document;
  const oldCard = document.head.querySelector('meta[name="twitter:card"]');
  const newCard = newDocument.head.querySelector('meta[name="twitter:card"]');
  oldCard.replaceWith(newCard);
});

loadend

Fired when have finished all progress.

LICENSE

CC0-1.0