Coder Social home page Coder Social logo

sean-roberts / parallel-data Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 0.0 272 KB

Speed up the loading of your application by loading its required data in parallel to your app code!

License: MIT License

JavaScript 100.00%
performance performance-tuning data-loader parallel-download

parallel-data's Introduction

ParallelData

The Problem ๐ŸŒ

When building client heavy apps or single page apps (SPAs), the most common way to structure the load of your application is:

-> Request HTML
...
---> Request all CSS
---> Request all Javascript
...
------> JS is ready and executing; app is initializing
-------> Request Required App Data
...
-----------> App Data is all ready, present the full app to the user

The requesting of JS and CSS is usually handled in parallel - whoohoo. When that's ready, your app parses and starts executing but you have logic that requires some data to be loaded before it continues to present the full version of the app. That data could be user info/permissions data, app/domain specific data, or any type of data that you require before presenting the final version of your app. But if you look at the flows, we keep the information about fetching the data inside the app code meaning we require the user to wait until the app code is executing, requests the data, and processes the data request before it can continue.

What if we could speed up our app and request the required app data at the same time as when you request the app assets?

HTTP2 Push ๐ŸŒŸ

The HTTP2 Push capabilities are 100% targeted at solving these problems and that's awesome. But for some, using HTTP2 is not a available or a practical option - that's where ParallelData comes in.

A Solution โšก๏ธ

ParallelData gives you a drop in JavaScript snippet that allows you to define the data urls you want to request and it will request them immediately (in parallel with your main JS/CSS downloads). Without changing anything in your app code, your app's next requests for those endpoints will be given the responses as they were received when ParallelData received them. With that your app is now faster and no longer waiting for your app to load, parse, and initialize to start requesting it's data!

Note ๐Ÿ—’

The scope of ParallelData is limited to loading data in parallel. For prefetching/preloading assets (css, js, images, etc.) I would look into resource hints/prioritization: spec and web fundamentals

Request/Response Scenarios ๐Ÿ”„

  • If ParallelData has received the endpoint response before your app requests it, your app requests will immediately get the responses and respective events.
  • If ParallelData has not received the endpoint response before your app requests it, your app requests will get the responses and respective events when the original ParallelData responses are received.

Networking Options Support โœ…

  • XHR support (GET requests only)
  • Sending Headers (per request or for all requests)
  • Fetch support (GET requests only)

Please create an issue if there are more use-cases that make sense for ParallelData to support

Browser Support ๐ŸŒ

ParallelData is tested and works in IE 9+, Chrome, FireFox, Safari, and all modern mobile browsers.


How To Use ๐Ÿ‘๐Ÿป

  • Pick the version you want to use on your site from the dist directory
  • Copy that version's code into your HTML file for your app
  • Below that, start using the ParallelData API

The API โš’

ParallelData.getForXHR( url, options )

This method immediately kicks off a GET request to the specified URL with the provided options. Note, this only matches requests that are XMLHttpRequest based.

Function Parameters

  • url - the url to load in parallel. This must exactly match the url used when requesting data in the app
  • options - an object specifying request configuration
    • options.headers - an object specifying the request headers to be added
    • options.onParallelDataResponse - a function callback that is called when the XHR request's readystate is complete. We will pass in the XHR in the first parameter and a ParallelData context object that can be used to know if the request was already handed off to the app.

ParallelData.getForFetch( url, options )

This method immediately kicks off a GET request to the specified URL with the provided options. Note, this only matches requests that are window.fetch based. By using this function, we assume that your app uses window.fetch if it is available and falls back to XMLHttpRequest for requests if it is not. With that, ParallelData will do the same. If you use this function and window.fetch is not available, we will automatically fallback to calling ParallelData.getForXHR internally for you.

Function Parameters

  • url - the url to load in parallel. This must exactly match the url used when requesting data in the app.
  • options - an object specifying request configuration. We allow the full set of options that window.fetch allows.
    • Note, options.credentials is set to "include" by default and options.redirect is set to "follow" by default
  • options.onParallelDataResponse - a function callback that is called when the Fetch promise resolves. We will pass in the fetch response object in the first parameter and a ParallelData context object that can be used to know if the request was already handed off to the app.

ParallelData.configure( configOptions )

This method allows you to specify configuration at the library level. Currently, it's primary use is for setting options (like common headers) that should be used for all requests - allowing you to omit adding those options for each getForFetch() or getForXHR() call.

Function Parameters

  • configOptions - an object specifying configuration for ParallelData
    • configOptions.allRequests - an object specifying options to be applied to all requests
      • configOptions.allRequests.headers - an object specifying the headers to be added to all requests

Running examples ๐Ÿƒ

Web

Locally

Building library locally ๐Ÿ”จ

  • Clone this repo locally
  • Build options:
    • Run npm run dev to watch files and perform a build
    • Run npm run build to run a single build
  • Run examples locally to have quick access to sites that are running the built library

Testing library ๐Ÿ”

The tests for this library are interactive visual tests. This was decided because I did not want any testing framework to override anything in an non-browser fashion causing the results of the tests to hide something.

Web

Locally

parallel-data's People

Contributors

sean-roberts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parallel-data's Issues

Allow earlyResponseHandler to allow for more depth of early data fetching

If I am solving a 2+ depth serial set of requests, I might need data from a subsequent parallel data call in order to make my next call. If PD provided a method for getting access to responses early, it can handle more than one level of serial requests.

Idea:

ParallelData.getForXHR('url',{
  earlyResponseHandler: ( response )=>{
    // called with XHREvent object or Fetch Response object depending on the request
    if(response.parallelDataRequestTransitioned){
      // the app request for this endpoint hasn't happened yet
    }else {
      // the app hasn't started using this request yet so we can do more eager logic
    }
  }
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.