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

remix-superloader

v0.3.6

Published

[![CI](https://github.com/joeflateau/remix-superloader/actions/workflows/main.yml/badge.svg)](https://github.com/joeflateau/remix-superloader/actions/workflows/main.yml) [![npm](https://img.shields.io/npm/v/remix-superloader)](https://www.npmjs.com/packag

Downloads

87

Readme

remix-superloader

CI npm

Without remix-superloader, when you use loaded data with remix's useLoaderData hook, Date objects (and others) will be strings rather than Date objects due to JSON serialization. You must be sure to parse those back into dates. remix-superloader handles parsing/serializing Dates (and Maps, Sets, BigInts, RegExps) so you don't have to think about that. remix-superloader also handles inferring types, so the useSuperLoaderData hook is automatically properly typed.

Installation

npm i remix-superloader

Usage

import { useSuperLoaderData, encodeSuper } from 'remix-superloader';

export const loader = async ({ params }: { params: { slug?: string } }) =>
  encodeSuper({
    post: {
      title: 'My first Post',
      date: new Date(),
      slug: params.slug,
    },
  });

export default function Post() {
  const { post } = useSuperLoaderData<typeof loader>();

  // without superloader, post.date is a string
  // with superloader, post.date is a Date

  return (
    <div>
      <h2>{post.title}</h2>
      <p>{post.date.toLocaleDateString()}</p>
    </div>
  );
}

Supported types

remix-superloader will convert these types that are not able to be represented in standard json for you.

  • ✅ Date
  • ✅ RegExp
  • ✅ Map
  • ✅ Set
  • ✅ BigInt
  • 🚫 Error
  • 🚫 undefined

Advanced: Custom Headers

You can also pass headers (and other ResponseInit data) from your loader

import { encodeSuper, useSuperLoaderData } from 'remix-superloader';

const loader = async () =>
  encodeSuper(
    {
      date: new Date(),
    },
    {
      headers: {
        'Server-Timing': serverTiming,
      },
    }
  );

export default function Post() {
  const { date } = useSuperLoaderData<typeof loader>();

  return (
    <div>
      <h2>Hello, {date.toLocaleString()}</h2>
    </div>
  );
}

Advanced: LoaderFunction type

remix-superloader provides a generic alternative to remix's LoaderFunction

import {
  encodeSuper,
  useSuperLoaderData,
  SuperLoaderFunction,
} from 'remix-superloader';

const loader: SuperLoaderFunction<{ date: Date }> = async () =>
  encodeSuper(
    {
      date: new Date(),
    },
    {
      headers: {
        'Server-Timing': serverTiming,
      },
    }
  );

export default function Post() {
  const { date } = useSuperLoaderData<typeof loader>();

  return (
    <div>
      <h2>Hello, {date.toLocaleString()}</h2>
    </div>
  );
}

Advanced: Custom Types

You can customize the type converters by including a list of type (de)serializers.

import {
  encodeSuper,
  mapType,
  defaultMappedTypes,
  useSuperLoaderData,
} from 'remix-superloader';

class Greeter {
  constructor(public name: string) {}

  greet() {
    return `Hello, ${this.name}`;
  }
}

const customMappedTypes = [
  ...defaultMappedTypes,
  mapType(
    Greeter,
    (greeter) => greeter.name,
    (name) => new Greeter(name)
  ),
];

const loader = async () =>
  encodeSuper(
    {
      greeter: new Greeter('Joe'),
    },
    customMappedTypes
  );

export default function Post() {
  const { greeter } = useSuperLoaderData<typeof loader>(customMappedTypes);

  return (
    <div>
      <h2>{greeter.greet()}</h2>
    </div>
  );
}