Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步加载 #49

Closed
stkevintan opened this issue Jan 11, 2018 · 2 comments
Closed

异步加载 #49

stkevintan opened this issue Jan 11, 2018 · 2 comments

Comments

@stkevintan
Copy link

stkevintan commented Jan 11, 2018

目前 unpkg 在国内的速度实在太慢了。而且官方网站提供的加载方式是阻塞模式的,遇到网络不好的时候会阻塞页面里其他脚本的加载,这个体验是非常不好的。(disqus使用了iframe方式避免了这个问题)。下面我的一个小建议:

  1. 将av-min依赖合并到Valine中,或者在valine里面动态加载。
  2. 将配置使用url参数的形式写在script的src链接后面: 例如:
window.VALINECONFIG = {
    el: '#comment',
    notify: false,
    verify: false
  }
<script src=".../Valine.min.js?config=VALINECONFIG" async></script>

附上一个我目前使用的解决方案:

function loadValine() {
  const config = window["VALINECONFIG"];
  if (!config) return;
  const urls = [`//cdn1.lncld.net/static/js/3.0.4/av-min.js`,`//unpkg.com/valine/dist/Valine.min.js`];
  const asyncloader = url =>
    new Promise((resolve, reject) => {
      const script = document.createElement("script");
      script.src = url;
      script.addEventListener("load", _ => resolve(), false);
      script.addEventListener("error", _ => reject(), false);
      document.body.appendChild(script);
    });
  Promise.all(urls.map(asyncloader))
    .then(() => new window["Valine"](config))
    .catch(e => U.log("load Valine Failed,", e));
}
@xCss
Copy link
Owner

xCss commented Jan 17, 2018

我看你的解决方案中的${baseURL}是你网站本地的吧?
那既然下载下来了,为何还要用unpkg

@stkevintan
Copy link
Author

那我改成unpkg好了。。。。

@xCss xCss closed this as completed May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants