最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditionally load polyfills - Stack Overflow

programmeradmin5浏览0评论

I'm building a website which my target group is very general (ages 13-oo, so hello IE9, hello ancient android browser), so I need polyfills for some stuff (viewport, calc etc). Before I used Modernizr and some conditionals user agents to target IOS 6-7 etc. Then with yepnope.js I was loading the specific polyfills.

Now that modernizr 3.0 is out, I noticed that the Modernizr.load() is deprecated. Also the yepnope.js library is deprecated. As they say on their website

"There are new best practices that you should likely follow instead."

But I can't find any of them. After googling for some time everyone remend Modernizr and Yepnope. But this issue is so fresh (the deprecation, the new version of Modernizr), and I can't find any new alternative method.

Maybe using of some module loader (like require.js) will do the job? And if yes, how?

I'm building a website which my target group is very general (ages 13-oo, so hello IE9, hello ancient android browser), so I need polyfills for some stuff (viewport, calc etc). Before I used Modernizr and some conditionals user agents to target IOS 6-7 etc. Then with yepnope.js I was loading the specific polyfills.

Now that modernizr 3.0 is out, I noticed that the Modernizr.load() is deprecated. Also the yepnope.js library is deprecated. As they say on their website

"There are new best practices that you should likely follow instead."

But I can't find any of them. After googling for some time everyone remend Modernizr and Yepnope. But this issue is so fresh (the deprecation, the new version of Modernizr), and I can't find any new alternative method.

Maybe using of some module loader (like require.js) will do the job? And if yes, how?

Share Improve this question asked Sep 27, 2015 at 15:17 user1133297user1133297 2
  • Usually, the polyfill tests for an existing method before adding one of its own. They're typically small enough that you can throw them in whether you're certain you'll need them or not. – Blazemonger Commented Sep 27, 2015 at 15:29
  • Thank you for your answer. But If I want many polyfills why to make multiple http requests? Or by merging and uglify the js files, why to make it bigger at size? – user1133297 Commented Sep 27, 2015 at 15:32
Add a ment  | 

4 Answers 4

Reset to default 6

I maintain the polyfill service at https://cdn.polyfill.io which may meet your needs. We have a library of around 800 polyfills, which are bundled selectively and served only to users that need them. You can run the service yourself or just load the polyfills from our CDN.

The most prehensive write-up I've seen on this technique is Philip Walton's Loading Polyfills Only When Needed. It's too long to quote any parts here and should be read in its entirety so I'm not going to copy/paste sections into this answer.

I've spent my morning figuring this out myself. You can use jQuery's getScript method. I just answered a similar q here: https://stackoverflow./a/34518146/411436

from the yepnope repo

When it es to loading things conditionally, we suggest that you output a build for each bination of the things you're testing. This might sound like it will generate a lot of files (it might), but puters are pretty good at that. Then you can inline a script into your page that only loads (asynchronously!) a single built script that is tuned to the features of that user. All the performance win of conditional loading, and none of the latency problems of loading 100 things at once.

发布评论

评论列表(0)

  1. 暂无评论