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

assets-reload

v1.3.1

Published

switch all assets cdn name (script,link,background-image,img). when you open one website in a test domain you can switch cdn name to location.hostname

Downloads

5

Readme

assets-reload

intro

前端资源重载,大流量的网站,就得考虑资源加载失败的情况下,用户白屏等问题。

因此必须对资源进行重载, 本模块通过 script, link, img等标签上的 onerror回调来进行资源加载重试,并且规则可定制。

并且针对背景图也提供了替换方案。

when use

(function() {
  try {
    var isInTestDomain = location.hostname.indexOf("www.qq.com") > 0;
    // 资源重载自定义规则函数
    var cdnHostname = "www-img.tenpay.com";
    function assetsReloadFunc(url, type, sourceEl) {
      // 线上静态资源都请求cdn 只对cdn资源进行重试
      var isCdn = url && url.indexOf(cdnHostname) != -1;
      if (!isCdn) {
        return false;
      }

      var reloadTimes = 0;
      var urlSearch = url.split("?")[1];

      // 查看重试了几次
      if (urlSearch) {
        var matchRes = urlSearch.match(/reloadAssets=(\d+)&?/);
        if (matchRes && matchRes.length > 1) {
          reloadTimes = Number(matchRes[1]);
        }
      }
      // 非测试环境 第一次重试cdn域名资源
      var replaceUrl;
      if (isInTestDomain && reloadTimes == 0) {
        replaceUrl =
          url.replace(cdnHostname, location.hostname) +
          (urlSearch ? "&" : "?") +
          "reloadAssets=1";
        return replaceUrl;
      }
      if (!isInTestDomain) {
        if (reloadTimes == 0) {
          return url + (urlSearch ? "&" : "?") + "reloadAssets=1";
        } else if (reloadTimes <= 1) {
          // 此处阈值可以调整
          replaceUrl = url.replace(cdnHostname, location.hostname);
          return replaceUrl.replace(
            "reloadAssets=" + reloadTimes,
            "reloadAssets=" + (1 + reloadTimes)
          );
        }
      }
    }
    var assetsReloadItem = new AssetsReload(assetsReloadFunc);
    if (isInTestDomain) {
      // 如果标签上都没有绑定  onerror 则可以直接进行全量替换重试,不过建议 写上 onerror 而非全量替换
      assetsReloadItem.reloadAll("SCRIPT");
      assetsReloadItem.reloadAll("LINK");
      assetsReloadItem.reloadAll("IMG");
      setTimeout(function() {
        // 替换背景图
        assetsReloadItem.reloadBackgroundImage();
      }, 800);
    }
    // 全局绑定 attackCatch 函数
    // 在 <script src="test.js" onerror="attackCatch(this)">
    window.attackCatch = function(htmlNode) {
      assetsReloadItem.reload(htmlNode);
    };
  } catch (err) {
    console.error(err);
  }
})();