Skip to content

1.0.1

Compare
Choose a tag to compare
@optimalisatie optimalisatie released this 07 Jul 14:09
· 83 commits to master since this release
  • Added data-l HTML attribute based config to enable usage with a strict Content-Security-Policy.
  • Added polyfill for IntersectionObserver
  • Added $async based polyfill loading.

Example using $async with data-c based config

<script data-c='[
  {
    "ref": "lazy",
    "src": "dist/lazy-data-attr+polyfill.js",
    "attributes": {
     "data-l": "[\"selector\", 0.006, \"0px\"]"
    },
    "load_timing": "domReady",
    "cache": "localstorage"
  },
  {
    "ref": "lazy-polyfill",
    "src": "dist/intersectionobserver-polyfill.js",
    "load_timing": {
      "type": "method",
      "method": "$lazypoly"
    },
    "cache": "localstorage"
  }
]'>
/* $async IIFE with timing and API module */
</script>

In the example, the $async timing method method defines window.$lazypoly which will automatically load the polyfill for browsers that require the polyfill. It uses localStorage for instant loading.

Alternatively, when using $lazy without $async, you can manually define window.$lazypoly with a function that returns a Promise or a object containing a .then method.

window.$lazypoly = function() {

   // load polyfill
   // ...

   return {
      then: function(callback) {

         // wait until polyfill is loaded and resolve callback

         callback();
      }
   }
};

When using $async you can alternatively use window.$lazypoly with a string or a object to pass to $async.js which could load anything.

Alternatively, when including $lazy inline, the data-poly attribute enables to define a string to pass to $async.

<script data-l='... lazy config ...' data-poly='... config to pass to $async to load polyfill ...'>
// dist/lazy-data-attr+polyfill.js
</script>

lazy-polyfill