Nikaple / assets-retry

:repeat: Non-intrusive assets retry implementation. 无侵入式的静态资源自动重试
MIT License
303 stars 57 forks source link
assets-ensure assets-retry cdn-ensure cdn-failover cdn-retry css-retry ensure-bundle fallback js-ensure js-failover js-retry

English | 简体中文

Auto Assets Retry

styled with prettier Build Status

A tiny non-intrusive library to retry your assets (scripts, stylesheets, images) when they failed to load, only 3 KB gzipped, even works with dynamic import!

Demo GIF

Demo URL

Table of Contents

Installation

Install with npm

$ npm install assets-retry --save

Then, inject the library at the beginning of the page with proper webpack configurations. See example

Use inline script directly

If you don't want to spend your time fiddling around with webpack configurations, you can inline the minified file with a script tag, and place it at the beginning of the page.

Usage

All you have to provide is the domain parameter, which are the domains to retry when assets failed to load.

// information of assets
var assetsRetryStatistics = window.assetsRetry({
    // domain list, only resources in the domain list will be retried.
    domain: ['your.first.domain', 'your.second.domain/namespace'],
    // maximum retry count for each asset, default is 3
    maxRetryCount: 3,
    // onRetry hook is how you can customize retry logic with, default is x => x
    onRetry: function(currentUrl, originalUrl, statistics) {
        return currentUrl
    },
    // for a given resource (except background-images in css),
    // either onSuccess or onFail will be eventually called to
    // indicate whether the resource has been successfully loaded
    onSuccess: function(currentUrl) {
        console.log(currentUrl, assetsRetryStatistics[currentUrl])
    },
    onFail: function(currentUrl) {
        console.log(currentUrl, assetsRetryStatistics[currentUrl])
    }
})

When the initialization is finished, following content gains the power of retrying automatically.

Config

The assetsRetry function takes an AssetsRetryOptions, which is defined as follows:

interface AssetsRetryOptions {
    maxRetryCount: number
    onRetry: RetryFunction
    onSuccess: SuccessFunction
    onFail: FailFunction
    domain: Domain
}
type RetryFunction = (
    currentUrl: string,
    originalUrl: string,
    retryCollector: null | RetryStatistics
) => string | null
interface RetryStatistics {
    retryTimes: number
    succeeded: string[]
    failed: string[]
}
type SuccessFunction = (currentUrl: string) => void
type FailFunction = (currentUrl: string) => void
type Domain = string[] | { [x: string]: string }

FAQ

  1. Q: Stylesheets or background images are not retried from backup domain, why?

    A: Due to security policies of browsers, access to cssRules is not allowed for cross origin stylesheets by default. To fix this:

    1. Add crossorigin="anonymous" attribute on link element for cross origin stylesheets.
    2. Make sure that Access-Control-Allow-Origin HTTP Header is correct.

Browser Support

Chrome logo Edge logo Firefox logo Internet Explorer logo Opera logo Safari logo ios logo android logo
47+ ✔ 15+ ✔ 32+ ✔ 10+ ✔ 34+ ✔ 10+ ✔ 10+ ✔ 4.4+ ✔

NPM scripts

Acknowledgement

RealWorldBrowserStack

  1. The example projects are based on amazing RealWorld demo apps.
  2. Cross browser testing are based on the rather excellent BrowserStack UI testing technology.