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

defer-if

v1.0.5

Published

Extend Remix's `defer` to easily conditionally defer data

Downloads

12

Readme

defer-if

defer-if is a utility package that extends the functionality of Remix's defer method, allowing you to easily conditionally defer or not defer data based on some programmatic criteria.

Installation

npm install defer-if

Usage

This is a quick demo showing how to use defer-if to only defer data when the user agent is a mobile device.

import { deferIf } from "defer-if";
import { isMobileUserAgent } from "../your-utils";

export function loader({ request }) {
    const data = {
        value1: await fetchSomething(), // this will always block (never defer)
        value2: "This is a static value",
        value3: fetchSomethingElse(), // this will either block or defer based on `deferIf`
    };

    // Using deferIf
   return await deferIf(data, isMobileUserAgent(request));
}

export default function Component() {
    const data = useLoaderData<typeof loader>();

    return (
        <Suspense fallback="Loading...">
            <Await resolve={data.value3}>
                {(value) => <MyComponent /* ... props */ />}
            </Await>
        </Suspense>
    );
}

Documentation

deferIf accepts three arguments:

  1. data: An object containing key-value pairs where values can be Promises or any other values.
  2. predicate: A function that returns a boolean value or a boolean value itself. If the function returns true, the promise will be deferred (not awaited); otherwise, it will be awaited (blocking the response).
  3. options: An optional configuration object containing:
    • init: A number or ResponseInit value.
    • alwaysAwait: An array of keys that should always be awaited, even if the predicate returns true.
    • neverAwait: An array of keys that should never be awaited, even if the predicate returns false.

Contributing

  1. Fork the repository on GitHub.
  2. Clone the forked repository to your local machine.
  3. Run bun install to install the dependencies.
  4. Make your changes or add new features, and ensure that your code follows the existing style and conventions.
  5. If you've added new functionality, update the README.md file with relevant information and add tests.
  6. Run tests to ensure everything is working as expected.
  7. Commit your changes and push them to your forked repository.
  8. Create a pull request from your fork to the main repository with a clear and concise description of your changes.

License

This package is released under the MIT License.