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

@beck24/stencil-component-prefetch

v1.0.1

Published

A component for prefetching dependencies for components in a StencilJS app

Downloads

34

Readme

Built With Stencil

stencil-component-prefetch

A web component for pre-fetching the code for additional components used later in the app.

By default the browser fetches the component code as-needed when a component is added to the DOM.

In certain cases this be problematic when network latency is bad or disconnected. An example is when pages are separate components the code must be downloaded prior to the page rendering, this may result in a noticeable lag between when a user clicks on a nav element and when the page actually transitions.

A number of other cases where this is an issue are discussed here along with a proposed prefetchComponent() api: https://github.com/ionic-team/stencil/pull/1558

Hopefully one day that api will come to fruition. Until then we have this component.

See the demo: https://beck24.github.io/stencil-component-prefetch/

See the component properties/methods: https://github.com/beck24/stencil-component-prefetch/blob/master/src/components/stencil-component-prefetch/readme.md

Installation

Via NPM

Install the module

npm install @beck24/stencil-component-prefetch

Usage

In your app-root component:

Add the pre-fetch component to your rendered markup

<stencil-component-prefetch />

Import Build and Element from stencil core

import { Component, h, Build, Element } from '@stencil/core';

Make an element reference variable

export class AppRoot {
  @Element() el: HTMLElement;

Use the componentDidLoad lifecycle hook to fetch a reference to the prefetcher and pre-fetch your components

componentDidLoad() {

  const componentsConfig = [
    {
      tag: 'my-custom-toast'
    },
    {
      tag: 'app-menu',
    },
    {
      tag: 'page-search',
      props: {
        prefetching: true,
      }
    },
    {
      tag: 'my-other-heavy-component',
      props: {
        enabled: true,
        name: 'some-name'
      }
    }
  ];

  if (Build.isBrowser) {
    // only pre-fetch if it's a real browser
    const prefetch = this.el.querySelector('stencil-component-prefetch');

    prefetch.setComponents(componentsConfig);
  }
}

By default the components are pre-fetched after a delay of 1500ms to ensure it isn't interfering with more immediate loading/rendering tasks.
This behavior can be changed with the setDelay method if desired

// set a delay of 0 - fetch immediately
prefetch.setDelay(0).then(() => {
  prefetch.setComponents(componentsConfig);
});

Method

The component forces pre-fetching by sequentially adding registered components to the DOM in a hidden div and immediately removing them.
The addition of the component triggers the browser to fetch the code if it hasn't already. The immediate removal keeps the DOM clean of unwanted/duplicate components.

This seems to work well, with a few things to watch out for.

You should not pre-fetch components that:

  1. automatically change the route
  2. automatically manipulate DOM elements outside of themselves
  3. automatically change/affect state
  4. automatically make API calls that may be unwanted/unnecessary immediately

There are ways to work around these limitations if they are your own stencil components.

State-changing Component prefetch example

If you have a component you want to prefetch, but it modifies state you can add a new prop to the component

@Prop() prefetching: boolean = false;

Prevent the component actually rendering if prefetching

render() {
  if (this.prefetching) {
    return null;
  }

  return (
    <div>My rendered component</div>
  )
}

Early-return on lifecycle hooks that perform actions unwanted during pre-fetching

componentWillLoad() {
  if (this.prefetching) {
    return;
  }

  // all state stuff ignored during pre-fetch
  this.store.mapStateToProps(this, state => {

  });
}

componentDidLoad() {
  if (this.prefetching) {
    return;
  }

  // state-modifying stuff
}

When prefetching this component just set the prop prefetching to true in the app-root

componentDidLoad() {

  const componentsConfig = [
    {
      tag: 'my-state-changing-component',
      props: {
        prefetching: true,
      }
    }
  ];

  if (Build.isBrowser) {
    const prefetch = this.el.querySelector('stencil-component-prefetch');

    prefetch.setComponents(componentsConfig);
  }
}