polyfill.io: Fix conditional loading snippet

This commit is contained in:
Rico Sta. Cruz 2018-08-20 09:03:18 +08:00
parent 2e381a4c77
commit d5a6d6fb04
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 5 additions and 15 deletions

View File

@ -14,26 +14,22 @@ intro: |
```html ```html
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script> <script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
``` ```
{: .-wrap} {: .-wrap}
This is the default script for Polyfill.io. This is the default script for Polyfill.io.
### References ### References
- [API example](https://polyfill.io/v2/docs/api) _(polyfill.io)_ * [API example](https://polyfill.io/v2/docs/api) _(polyfill.io)_
- [List of features](https://polyfill.io/v2/docs/features) _(polyfill.io)_ * [List of features](https://polyfill.io/v2/docs/features) _(polyfill.io)_
## Optimized ## Optimized
### For modern browsers ### For modern browsers
```html ```html
<script>(function(d,s){ <script>if(!(window.Promise&&[].includes&&Object.assign&&window.Map)){document.write('<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></scr'+'ipt>')}</script>
if(window.Promise&&[].includes&&Object.assign&&window.Map)return;
var sc=d.getElementsByTagName(s)[0],js=d.createElement(s);
js.src='https://cdn.polyfill.io/v2/polyfill.min.js';
sc.parentNode.insertBefore(js, sc);
}(document,'script'))</script>
``` ```
This only includes polyfill.io when necessary, skipping it for modern browsers for faster load times. This only includes polyfill.io when necessary, skipping it for modern browsers for faster load times.
@ -41,13 +37,7 @@ This only includes polyfill.io when necessary, skipping it for modern browsers f
### Extra features ### Extra features
```html ```html
<script>(function(d,s){ <script>if(!(window.fetch&&window.Promise&&[].includes&&Object.assign&&window.Map)){document.write('<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,fetch"></scr'+'ipt>')}</script>
if(window.fetch&&window.Promise&&[].includes&&Object.assign&&window.Map)return;
var sc=d.getElementsByTagName(s)[0],js=d.createElement(s);
js.src='https://cdn.polyfill.io/v2/polyfill.min.js?features=default,fetch';
sc.parentNode.insertBefore(js, sc);
}(document,'script'))</script>
``` ```
{: data-line="2,4"}
This is the same as the previous, but also adds a polyfill for `window.fetch()`. We add a `window.fetch` check and loads the additional `fetch` feature. This is the same as the previous, but also adds a polyfill for `window.fetch()`. We add a `window.fetch` check and loads the additional `fetch` feature.