Skip to content
프로젝트

소개

XHR2를 통해 브라우저에서 에셋을 미리 불러오는 1KB 크기의 JavaScript 라이브러리입니다. 다양한 파일 형식의 에셋을 불러오고 통합 진행 이벤트를 발생시킬 수 있습니다.

개발 도구

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

preload-it

A small browser library for preloading assets with composite progress events.

Installation

npm install preload-it
import Preload from 'preload-it'

const preload = Preload({
  stepped: true,
  timeout: 30_000,
  concurrency: 4
})

preload.onprogress = ({ progress, item }) => {
  console.log(`${progress}%`, item.url)
}

preload.onerror = item => {
  console.error(item.url, item.failureReason, item.status)
}

const items = await preload.fetch([
  '/images/cover.jpg',
  '/video/trailer.mp4'
])

The package also provides UMD bundles for direct browser use:

<script src="https://unpkg.com/preload-it@2/dist/preload-it.min.js"></script>

API

Preload(options)

Creates a preloader instance. stepped defaults to true, giving every asset equal weight in aggregate progress. Set it to false to weight progress by bytes when response sizes are available. timeout is an optional XHR timeout in milliseconds and defaults to 0 (disabled).

concurrency is an optional positive integer that limits simultaneous requests. It defaults to unlimited concurrency, preserving the behavior of previous releases. Queued items are included in state immediately and are marked as cancelled if cancel() runs before they start.

fetch(urls)

Starts one batch and resolves with all item results after every request settles. HTTP and network failures do not reject the batch; they call onerror and appear in the resolved array with error: true and a failureReason.

Each instance supports one active batch at a time. Starting an overlapping batch rejects with a usage error. A later, sequential call is supported and replaces state with the new batch.

Callbacks

  • onprogress({ progress, item }) runs when aggregate progress changes.
  • onfetched(item) runs when an individual non-cancelled request settles.
  • onerror(item) runs for HTTP, network, and timeout failures.
  • oncomplete(items) runs after a non-cancelled batch settles.
  • oncancel(items) runs after a cancelled batch settles.

The lowercase callback names are retained for compatibility.

cancel()

Aborts the active batch. Aborted items have status: 0, canceled: true, and failureReason: 'abort'. The batch Promise resolves with its current items; oncancel runs instead of oncomplete.

dispose()

Cancels active work and revokes all object URLs created by the instance. Call it when the loaded assets are no longer needed. Repeated calls are safe.

preload.dispose()

Result items

Every item contains url, completion, downloaded, total, error, and canceled. Settled items may also contain status, fileName, type, size, blobUrl, and failureReason (http, network, timeout, or abort). TypeScript declarations are included.

Browser support

The automated suite runs in current Chromium, Firefox, and WebKit releases.

License

MIT

관련 프로젝트 살펴보기

불러오는 중…