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

img-in-queue

v0.0.8

Published

preload imgs in queue

Downloads

2

Readme

img-in-queue

NPM version build status Test coverage David deps npm download

串行预加载图片的中间件组件

背景

有些页面需要加载高清图,如果此时并发的请求过多,会造成网络请求缓慢。

chrome浏览器中,http1并发数限制为6,http2则没有并发数限制,了解更多

http1

http2

并行加载所有请求对网页的整体加载有很大的优化,但是如果我们如果需要快速展示第一张图片,那这样的请求速度肯定是满足不了需求的。

解决思路

  1. 串行加载图片,避免因网络分流而导致首张图片加载缓慢
  2. 在后台预加载后面的图片,加载完毕后替换img的src属性

queue

第一次加载为后台用new Img()的方式进行请求,第二次则为改变真实dom的src属性时发起的请求,由于存在缓存,请求时间几乎为0。

使用方法

import React from 'react';
import ReactDOM from 'react-dom'
import ImgQueue from 'img-in-queue'

let imgs = []
for(let i = 0; i < 20; i++){
  imgs.push({
    id: i,
    src: `https://github.com/luke93h/img-in-queue/blob/master/assets/2.jpg?raw=true&timestamp=${i}${Math.random()}`
    defaultSrc: i % 3 === 0 ? undefined : 'https://github.com/luke93h/img-in-queue/blob/master/assets/3.jpg?raw=true'
  })
}

class Index extends React.PureComponent {
  render() {
    return (
      <ImgQueue
        imgs={imgs}
        defaultSrc='https://github.com/luke93h/img-in-queue/blob/master/assets/2.jpg?raw=true'
      >
        {imgs => (
          imgs.map(img => (
            <img src={img.src} key={img.id}></img>
          ))
        )}
      </ImgQueue>
    );
  }
}
ReactDOM.render(
  <Index />,
  mountNode)

API

ImgInQueue

| 属性 | 说明 | 类型 | 默认值 | | :--------: | :-----: | :----: | :----: | | imgs | 图片数组 | ImgProps[ ] | [ ] | | defaultSrc | 加载未完成时显示的url | string | '' |

Img

| 属性 | 说明 | 类型 | 默认值 | | :--------: | :-----: | :----: | :----: | | src | 图片src | string | - | | id | 图片唯一id | string | - | | defaultSrc | 加载未完成时显示的url,优先级比ImgInQueue中的高 | string | - |

开源协议

MIT