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

carpool

v0.5.0

Published

Tools for asynchronously updating your current HTML with HTML from another URL.

Downloads

12

Readme

Carpool

Tools for asynchronously updating your current HTML with HTML from another URL.


Why use Carpool?

Let’s say you are building a website and you want to nicely transition between pages of that site without ever actually reloading a page, instead loading the HTML for each new page asynchronously and replacing the HTML of the existing page with the new page.

There are plenty of libraries that will handle all of this for you – preventing default link behavior, updating your browser history, fetching the contents of the new page, replacing the contents of the current page, animating between pages, etc – but sometimes you don't want a library that handles all of these things.

Carpool specifically handles the loading and replacing of new HTML in the above scenario.

If you plan to build a single-page site, you can use Carpool to fetch and update the HTML, but you'll need something else to handle routing and transitions. I personally prefer roadtrip for handling routing and gsap for handling animated transitions.

Installing Carpool

yarn add carpool

or 

npm install carpool --save

Using Carpool

Here's a simple, but fairly complete example of building a single page site with Carpool and roadtrip

import Carpool from 'carpool';
import roadtrip from 'roadtrip';
import TimelineLite from 'gsap/TimelineLite';

window.APP = window.APP || {};

//
//   Carpool
//
//////////////////////////////////////////////////////////////////////

// `contentSelector` is the DOM selector for the element that will be
// replaced with new content. An element with this selector should
// exist on every page you want to load.

APP.carpool = new Carpool({
  contentSelector: '.js-content'
});

//
//   Routing
//
//////////////////////////////////////////////////////////////////////

let indexRoute = {
    beforeenter: function(route) {
      // Use Carpool to fetch the new HTML content associated with the
      // route and returns a Promise that we can use to identify
      // when the new HTML is ready to use
      route.data = APP.carpool.getRouteData(route);
    },

    enter: function(route, previousRoute) {
      // When the new HTML is ready, use Carpool to replace the existing
      // HTML with the new HTML
      route.data.then(function(response) {
        if (!route.isInitial) {
          APP.carpool.replaceHtml(response);
        }

        // Transition to this route. You can also create unique
        // transition depending on which route we're coming from
        // by using the `previousRoute` param. 

        new TimelineLite()
          .to('.heading', 1, {
            alpha: 1
          })
          .to('.text', 1, {
            alpha: 1
          });
      });
    },

    leave: function(route, nextRoute) {
      // Transition out of this route. When leaving the route
      // we need to use a Promise so we know when the out
      // animation has successfully finished. Only after the
      // promise is resolved will we transition in the new route

      return new Promise(function(resolve, reject) {
        new TimelineLite()
          .to('.heading', 1, {
            alpha: 0
          })
          .to('.text', 1, {
            alpha: 0
          })
          .eventCallback('onComplete', resolve);
      });
    }
}

let aboutRoute = {
    // Similar to indexRoute but with unique transitions
}

APP.router = roadtrip
  .add('/', indexRoute)
  .add('/about', aboutRoute);

APP.router.start();

API

Carpool(options)

Creates a new Carpool instance.

let carpool = new Carpool({
    contentSelector: '.js-content' // The selector for HTML element to load/replace
});

carpool.load(url)

If the url hasn't been previously loaded, this will fetch the HTML contents of the page at the given URL. If the page has been previously loaded, it will return the cached contents of that page. In either case, .load() will return a Promise that resolves with the page's HTML.

carpool.load('http://mysite.com/about').then(function(html) {
    // Do something with `html`
});

carpool.cache(url)

Immediately add the HTML for the current page to carpool's cache, using the passed url as a key. This can be useful for caching the contents of the initial page visit so it won't need to be fetched in the future.

carpool.addToCache('http://mysite.com/about');

carpool.replaceHtml(html)

Finds the element in the current document that matches the contentSelector selector, and replaces it with the passed html. This will also automatically update certain elements in the head of the document, such as <title>.

carpool.replaceHtml('<!doctype html>…');

carpool.getCache()

Returns the contents of carpool's cache, an object with keys equal to URLs and values equal to the HTML content for that URL.

carpool.getCache();

carpool.getRouteData(route)

Specifically intended to be used with roadtrip. This is a helper function for easy integration with roadtrip. Takes a roadtrip route parameter and automatically fetches the HTML content for that route and returns a Promise that resolves with that HTML content when ready. See the Using Carpool example earlier in these docs for its usage.

Browser Support

Carpool requires window.fetch and window.Promise which are available in most new browsers. I recommend you use the following polyfills if you need to support older browsers: