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

zan-cdn-fallback

v0.0.6

Published

### 原理

Downloads

3

Readme

cdn fallback

原理

当js加载失败时会触发onerror事件,所以拿到加载失败的url,把host换成备用的,然后重新加载。

例如:目前我们使用的cdn域名有两个: b.yzcdn.cn、su.yzcdn.cn,当我们去请求 https://b.yzcdn.cn/v2/vendor/jquery.js 时,如果失败了,我们就把b.yzcdn.cn换成su.yzcdn.cn重新加载一个这个js文件。

这种方式虽然能保证资源的重新加载,但对js这种有严格的执行顺序的有的时候重新加载会出错。

使用方式

要保证每个文件都能回退重新加载,那就得把cdn-fallback代码放到head的第一个script标签里,并保证这段代码是inline进去的。这样在window下会有一个_cdnFallback方法,所有通过src加载的js都添加上onerror="_cdnFallback(this)"处理方法,如下:

<script onerror="_cdnFallback(this)" src="https://b.yzcdn.cn/v2/vendor/jquery.min.js"></script>

iron项目

在iron里已经存在一个压缩过的文件了,在 resource/static/cdn_fallback.min.js中,使用 $view->loadCSS$view->loadJS 来创建link和script标签。

在base.html里要添加$view->loadStaticContent('cdn_fallback.min.js');来加载cdn-fallbackjs到head里。

<head>
    <script>
    	<?php $view->loadStaticContent('cdn_fallback.min.js'); ?>
    </script>
    ...
</head>

如果自己手动写script src,那只要添加上 onerror处理就ok了。

node项目

在 base.html head 标签里面添加下面一段代码

{% block head_script %}
 {{ inlineJs('/node_modules/@youzan/cdn-fallback/dist/index.js') }}
{% endblock %}

Zan 框架里面的 loadJs 方法默认已经加了 onerror="_cdnFallback(this)",业务方无需再手动添加;如果自己手动写script src,那只要添加上 onerror处理就ok了。

其它项目

把cdn-fallback包安装到本地,再把cdn-fallback/dist/index.js放到 html的head里。

日志记录

默认情况下加载失败会打 5% 的日志到日志平台: log.qima-inc.com。当10分钟超过200值就会报警。 由于网络抖动,偶而保一次警是正常的。如果持续报警,那就得报告 冬瓜 处理