npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

facade-script

v1.0.0

Published

Lazyloader for any script or embed

Downloads

443

Readme

npm version Minzipped size Tree shaking Dependency count Built With Stencil

<facade-script>

A little Custom Element to lazy-load or late-load any script or embed, only when the user needs it.

For example to only load a YouTube iFrame when the user scrolls down to it.

TLDR usage:

  • JS: <facade-script src="/lazyload/any/script.js"></facade-script> or
  • iFrame: <facade-script iframe src="/lazyload/any/embed.js"></facade-script> or
  • iFrame video: <facade-script iframe trigger="click" src="/any/youtube.js">Play</facade-script>

Perfect for the Mindful-loading pattern or Import on interaction pattern.

Loading can be triggered when you:

  • Scroll it into view (lazyload is the default)
  • Click it (for example your own Play button)

Step 1 of 2

Include the script in your page: (~8.2kb gzipped)

<script
  defer
  type="module"
  src="https://unpkg.com/facade-script/dist/facade-script/facade-script.esm.js"
></script>
<script
  defer
  nomodule
  src="https://unpkg.com/facade-script/dist/facade-script/facade-script.js"
></script>

☝️ Recommend using defer for minimal impact on page load speed and because there's typically no hurry to fetch this script, but that's up to you.

Step 2 of 2

Then you simply use the facade-script tag instead of a standard script or iframe...

To lazyload a script:

A <script> tag will only be added to the page when the user scrolls this into view.

<facade-script src="https://path/to/a/script.js"></facade-script>

To lazyload a YouTube video:

An <iframe> tag will only be added to the page when the user scrolls this into view.

Recommended: For improved accessibility, supply a title attribute for the iframe too. See props below.

<facade-script
  iframe
  src="https://www.youtube.com/embed/GTUruS-lnEo"
></facade-script>

To show a placeholder until the iframe embed has loaded:

<facade-script iframe src="https://www.youtube.com/embed/GTUruS-lnEo">
  <p>Loading...</p>
</facade-script>

Advanced use cases

once (boolean)

Use this to ensure a script is only loaded once on the page, even when there are multiple instances of the tag. Without this flag, every instance of this component will add the script to the page when triggered.

<facade-script once src="https://path/to/a/script.js"></facade-script>

global (boolean)

By default the script will be added to the page within the facade-script tags. Use the global option to add the script to the <head> instead.

<facade-script global once src="https://path/to/a/script.js"></facade-script>

trigger ("now" | "lazy" | "click")

When should the script be added to the page?

  • now - Immediately. Much like a standard script. Kinda pointless but useful when debugging.
  • lazy - When this element is scrolled into view. (Default)
  • click - When this element is clicked.
<facade-script
  iframe
  trigger="click"
  src="https://www.youtube.com/embed/GTUruS-lnEo"
>
  <button>Play video</button>
</facade-script>

wait="..." (milliseconds)

After being triggered, delay n milliseconds before adding the script or iframe to the page. You could combine it with trigger="now" to make the script run n milliseconds after DOM load.

<facade-script
  iframe
  wait="2000"
  src="https://www.youtube.com/embed/GTUruS-lnEo"
></facade-script>

props (to add attributes to your script or iframe)

Sometimes you need to set attributes on the <script> or <iframe> when it gets created.

Attributes can be supplied as a map of {key:value} supplied as JSON (or as an object if you're using JSX).

For example, iframes should have a title attribute: (Yes yes I know it's not very beautiful but that's the nature of JSON in HTML, so if you have a better solution that does not bloat the code unnecessarily then be sure to let me know 🤓)

<facade-script
  iframe
  src="https://www.youtube.com/embed/GTUruS-lnEo"
  props="{\"title\":\"Title of iframe\"}"
></facade-script>

Or in JSX:

<facade-script
  iframe
  src="https://www.youtube.com/embed/GTUruS-lnEo"
  props={{ title: 'Title of iframe' }}
></facade-script>

See this readme for a full list of config options.

Notes

  • Curently in Beta. Appears stable but still testing in the wild.
  • ~8.2kb when you use the CDN link (minified & gzipped). I have yet to see if I can make it smaller with some more analysis.
  • To do: Solutions for better accessibility of placeholders etc.
  • To do: Consider what happens during prerender. This should probably not render the script or iframe when running server-side.
  • But why is it called facade-script? Because of the Facade Pattern: As well as lazyloading it can render a fake UI or placeholder until the real third party loads.