Skip to content

collective/collective.lazysizes

Repository files navigation

collective.lazysizes

This package integrates lazysizes, a lightweight lazy loader, into Plone.

lazysizes is a fast, SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), iframes, scripts/widgets and much more. It also prioritizes resources by differentiating between crucial in view and near view elements to make perceived performance even faster.

By using this package you can expect reductions of up to 80% in load time, 75% in page size and 50% in number of requests.

These are some sites using collective.lazysizes:

As long as we have tested, lazysizes seems not to interfere with image indexing made by crawlers like Googlebot.

Got an idea? Found a bug? Let us know by opening a support ticket.

To enable this package in a buildout-based installation:

  1. Edit your buildout.cfg and add collective.lazysizes to the list of eggs to install:
[buildout]
...
eggs =
    collective.lazysizes

After updating the configuration you need to run ''bin/buildout'', which will take care of updating your system.

Go to the 'Site Setup' page in a Plone site and click on the 'Add-ons' link.

Check the box next to collective.lazysizes and click the 'Activate' button.

This package adds a transformer to the transform chain to integrate lazysizes into Plone.

The transformer looks for all the <img>, <iframe> and <blockquote> tags inside the content and does the following:

  • appends a lazyload class
  • if the tag is an <img>, transforms the src attribute into a data-src and uses a gray square in its place to maintain valid HTML code (this placeholder is loaded using the data URI scheme to avoid a new request to the server)
  • if the tag is an <iframe>, transforms the src attribute into a data-src
  • if the tag is a <blockquote> containing a tweet, it adds a data-twitter attribute and removes the <script> tag associated with the Twitter widget to avoid a useless request

These transforms can be applied to all users or only to anonymous.

This add-on uses a very opinionated approach on how to handle static resources in Plone. We just deprecated resource registries in favor of a simpler approach: a viewlet in plone.htmlhead. This simplifies maintainance among multiple Plone versions and avoids bundling of unrelated resources.

We use webpack to process static resources on this package. webpack processes SCSS and JS files, minifies the resulting CSS and JS, and optimizes all images.

To contribute, you should start the instance in one shell and start webpack watcher on another with the following command:

$ bin/watch-lazysizes

Then go to webpack/app folder and edit SCSS and JS files; webpack watcher will automatically create the final resources in the right place.

There are also other commands added to handle more complex scenarios.

The following command will set the buildout node installation in the system PATH, this way you can use webpack as described on their documentation.

$ bin/env-lazysizes

The following command generates JS and CSS without the minify step (it can be used to check the code being generated in a human readable way).

$ bin/debug-lazysizes

The following command rebuilds static files and exit (insted of keep watching the changes):

$ bin/build-lazysizes

Static resources on this package are generated using webpack and aren't included in VCS. If you release using zest.releaser you have to upload manually the files to PyPI or you will end with a broken distribution:

  • run longtest and fullrelease, as usually
  • answer "no" when asked to upload to PyPI and continue normally
  • do a checkout to the tag you're releasing
  • run bin/build-lazysizes to update static files
  • create the distribution files using python setup.py sdist bdist_wheel as usual
  • upload the files using twine upload dist/*

In case of errors you will have to create a new release as the PyPI Warehouse doesn't allow for a filename to be reused.