包详细信息

@bigcommerce/script-loader

bigcommerce14.1kMIT2.2.5

A library for loading JavaScript files asynchronously

自述文件

@bigcommerce/script-loader

Build Status

A library for loading JavaScript files asynchronously. It loads script files by injecting script tags into DOM during runtime.

Install

You can install this library using npm.

npm install --save @bigcommerce/script-loader

Usage

For scripts

To load a single script:

import { createScriptLoader } from '@bigcommerce/script-loader';

const loader = createScriptLoader();

loader.loadScript('https://cdn.foo.bar/main.js')
    .then(() => {
        console.log('Loaded!');
    });

To load multiple scripts:

loader.loadScripts([
    'https://cdn.foo.bar/vendor.js',
    'https://cdn.foo.bar/main.js',
]);

To load a script with async attribute:

loader.loadScript('https://cdn.foo.bar/main.js', { async: true });

To preload or prefetch a script:

loader.preloadScript('https://cdn.foo.bar/chunk.js');
loader.preloadScript('https://cdn.foo.bar/another-chunk.js', { prefetch: true });

A prefetched script is a low priority resource, therefore it will be downloaded in the background when the browser is idle. On the other hand, a script without prefetch option will be marked as a high priority resource and downloaded immediately.

Please note that the preloaded or prefetched script won't be executed. It will just be downloaded to the browser cache. To attach it to the document and execute it, you will need to call loadScript with the same URL.

To preload or prefetch multiple scripts:

loader.preloadScripts([
    'https://cdn.foo.bar/chunk.js',
    'https://cdn.foo.bar/another-chunk.js',
]);

loader.preloadScripts([
    'https://cdn.foo.bar/chunk.js',
    'https://cdn.foo.bar/another-chunk.js',
], { prefetch: true });

For browsers that don't have the ability to preload or prefetch resources, scripts will be loaded using regular Ajax requests instead.

For stylesheets

To load a single stylesheet:

import { createStylesheetLoader } from '@bigcommerce/script-loader';

const loader = createStylesheetLoader();

loader.loadStylesheet('https://cdn.foo.bar/main.css')
    .then(() => {
        console.log('Loaded!');
    });

To load multiple stylesheets:

loader.loadStylesheet([
    'https://cdn.foo.bar/vendor.css',
    'https://cdn.foo.bar/main.css',
]);

To prepend, instead of append, a stylesheet to the head of a document:

loader.loadStylesheet('https://cdn.foo.bar/main.css', { prepend: true });

To preload or prefetch a stylesheet:

loader.preloadStylesheet('https://cdn.foo.bar/chunk.css');
loader.preloadStylesheet('https://cdn.foo.bar/another-chunk.css', { prefetch: true });

Similar to a preloaded script, a preloaded or prefetched stylesheet won't take an effect until it is attached to the document. To do it, you will need to call loadStylesheet with the same URL.

loader.preloadStylesheets([
    'https://cdn.foo.bar/chunk.css',
    'https://cdn.foo.bar/another-chunk.css',
]);

loader.preloadStylesheets([
    'https://cdn.foo.bar/chunk.css',
    'https://cdn.foo.bar/another-chunk.css',
], { prefetch: true });

Contribution

To release:

npm run release

To see other available commands:

npm run

License

MIT

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.2.5 (2025-05-01)

2.2.3 (2024-01-18)

2.2.2 (2020-06-23)

Bug Fixes

  • common: CHECKOUT-4954 Upgrade request-sender version (201651b)

2.2.1 (2020-05-25)

2.2.0 (2020-05-25)

Features

  • core: CHECKOUT-4909 Pass in attributes to stylesheet (8bfc8a4)

2.1.0 (2019-10-24)

Features

  • common: PAYPAL-7 Pass in merchant ID on PayPal button script for PayPal Express Checkout (bdfbcd5)
  • common: PAYPAL-7 Pass in merchant ID on PayPal button script for PayPal Express Checkout (fc9e7c9)
  • common: PAYPAL-7 Pass in merchant ID on PayPal button script for PayPal Express Checkout (b6613e3)
  • common: PAYPAL-7 Pass in merchant ID on PayPal button script for PayPal Express Checkout (fe07c6e)

2.0.0 (2019-10-03)

⚠ BREAKING CHANGES

  • core: Previously, when a script is loaded, we return the associated event object inside a promise object to the caller. With this change, we return an empty promise object instead. This change is necessary because we can't guarantee that scripts can be loaded in the same way across different browsers. For example, some browsers don't support rel="preload", so as a fallback, we have to preload scripts using regular XHR calls. In that case, we don't have an event object to return to the caller. We could potentially mock the event object to keep the return values consistent. But considering it is not a very useful thing to return, we've decided to make a breaking change and return nothing instead.

Bug Fixes

  • core: CHECKOUT-4455 Provide fallback for browsers that don't support preload attribute (0fc52e7)
  • core: CHECKOUT-4455 Stop resolving promise with load event (670fd2c)

1.0.0 (2019-09-23)

Features

  • core: CHECKOUT-4400 Add ability to prefetch scripts and stylesheets (072d567)

0.2.0 (2019-09-20)

Features

  • common: CHECKOUT-4400 Add StylesheetLoader for loading stylesheets (06fd92b)
  • common: CHECKOUT-4400 Load multiple scripts in parallel and run them in series (4d140a6)

0.1.6 (2018-08-23)

Bug Fixes

  • common: CHECKOUT-3462 Remove Node engine field (d2fd2f3)

0.1.5 (2018-05-28)

Bug Fixes

  • common: CHECKOUT-3191 Fix sourcemaps by enabling inlineSources (1c06351)

0.1.4 (2018-05-14)

0.1.3 (2018-05-10)

0.1.2 (2018-03-19)

Bug Fixes

  • core: CHECKOUT-3012 Do not cache failed responses (792de4a)

0.1.1 (2018-03-15)

Bug Fixes

  • core: CHECKOUT-3012 Avoid loading the same script twice (871ae47)

0.1.0 (2018-02-26)

Features

  • core: CHECKOUT-2739 Add ScriptLoader responsible for loading JS files asynchronously (06620da)