🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

lazy-with-retry

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazy-with-retry

A react library that provides a lazy loading component with retry, refresh and fallback functionality.

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

Lazy with Retry

Commitizen friendly

lazy-with-retry is a utility library designed to enhance the React lazy function by adding retry and fallback mechanisms. It is particularly useful for dynamically importing components in React applications where network issues or other failures might prevent a component from loading successfully. This library ensures a better user experience by providing retries, fallback components, and even page refreshes as a last resort.

Why Use lazy-with-retry?

Dynamic imports in React can fail due to various reasons, such as network instability or server issues. By default, React's lazy function does not provide a way to handle these failures gracefully. lazy-with-retry solves this problem by:

  • Allowing multiple retries with customizable intervals.
  • Providing a fallback component to display when retries fail.
  • Optionally refreshing the page to attempt recovery.
  • Offering hooks for custom error handling and retry logic.

Installation

npm install lazy-with-retry

Parameters

The lazyWithRetry function accepts the following parameters:

ParameterTypeDescription
importFunction() => Promise<{ default: ComponentType<unknown> }>The function to dynamically import the component.
optionsTLazyRetryOptionsConfiguration options for retries, refreshes, and error handling.

TLazyRetryOptions

OptionTypeDescription
failFallbackComponentComponentType<unknown>A fallback component to display if retries fail. Default is an empty component.
retriesnumberNumber of retry attempts. Default is 2.
intervalnumberTime in milliseconds between retries. Default is 500.
forceRefreshOptionsTForceRefreshOptionsConfiguration for forcing a page refresh on failure.
onRefresh(error: Error, refreshLeft: number) => voidCallback for when a refresh is attempted.
onRetry(error: Error, retriesLeft: number) => voidCallback for when a retry is attempted.
onFailure(error: Error) => voidCallback for when all retries fail.

TForceRefreshOptions

OptionTypeDescription
refreshRetriesnumberNumber of page refresh attempts. Default is 0.
cacheKeyPrefixstringPrefix for session storage keys. Default is 'retry-lazy-refresh-for'.
sessionCacheKeystringUnique key for session storage to prevent infinite refresh loops.

Usage

Here is an example of how to use lazy-with-retry:

import React from 'react';
import { lazyWithRetry } from 'lazy-with-retry';
import FallbackComponent from './components/fallback-component';

const HelloWorld = lazyWithRetry(() => import('./components/hello-world'), {
  failFallbackComponent: FallbackComponent,
  retries: 3,
  interval: 1000,
  forceRefreshOnFailure: {
    forceRefresh: true,
    refreshRetries: 2,
    sessionCacheKey: 'hello-world-refresh',
  },
  onRetry: (error, retriesLeft) => {
    console.error(`Retrying... ${retriesLeft} retries left`, error);
  },
  onRefresh: (error, refreshLeft) => {
    console.warn(`Refreshing... ${refreshLeft} refreshes left`, error);
  },
  onFailure: (error) => {
    console.error('Failed to load component', error);
  },
});

const App = () => (
  <React.Suspense fallback={<div>Loading...</div>}>
    <HelloWorld />
  </React.Suspense>
);

export default App;

License

This project is licensed under the MIT License. See the LICENSE file for details.

Keywords

react

FAQs

Package last updated on 25 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts