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

next-data-streaming

v1.0.5

Published

Next.js progressive partial data streaming library

Downloads

9

Readme

next-data-streaming

npm version

Next.js progressive partial data streaming library.

This library is a twist on Next.js React Server Components (RSC) streaming, but instead of using <Suspense /> to stream pieces of JSX, you can stream data. That lets you forget about confusing styling in server components (for example CSS in JS not being compatible with RSC). Just focus on fetching data in RSCs and let client components arrange this data in a nice looking UI.

For example, here you see a website that loads right away with SSR article content but later streams data for navigation, user and related articles (all of them are streamed independently of each other):

demo

YouTube video about this library:

youtube

How to use it?

Install library:

npm i next-data-streaming

Place <NextDataStreaming /> component in your page. Here's an example:

import { NextDataStreaming } from 'next-data-streaming';
import { Client } from './client';
// and some other imports ...

export default function Page() {
  return (
    <NextDataStreaming
        // Some data that will be available right away (no streaming)
      data={{
        article: article,
      }}
      // Some data that will be independently streamed to Client component
      dataStream={{
        navigation: new Promise<NavigationItem[]>((resolve) => setTimeout(() => resolve(navigation), 2000)),
        user: new Promise<User>((resolve) => setTimeout(() => resolve(user), 3000)),
        relatedArticles: new Promise<RelatedArticle[]>((resolve) => setTimeout(() => resolve(relatedArticles), 4000)),
      }}
      // Your Client component that will receive `data` and `dataStream` as props
      ClientComponent={Client}
    />
  );
}

Create your Client component. Don't forget to place "use client" directive on top of this component file. Use ClientComponentProps to properly type your props. Here's an example:

'use client';

import { ClientComponentProps } from 'next-data-streaming';
// and some other imports ...

type ClientProps = ClientComponentProps<
  {
    article: Article;
  },
  {
    navigation: NavigationItem[];
    user: User;
    relatedArticles: RelatedArticle[];
  }
>;

export const Client = ({ data: { article }, dataStream: { navigation, user, relatedArticles } }: ClientProps) => {
  return (
    <main>
      <Navigation navigation={navigation} user={user} />
      <Article article={article} />
      <RelatedArticles relatedArticles={relatedArticles} />
    </main>
  );
};

And that's it. Data is data will be available right away, and data in dataStream will initially be undefined and become target types with time, so in your client components you have to handle loading state, for example:

export const RelatedArticles = ({ relatedArticles }: RelatedArticlesProps) => (
  <Wrapper>
    {relatedArticles
      ? relatedArticles.map(({ title, content, href }, index) => (
          <Link key={index} href={href}>
            <Item>
              <Title>{title}</Title>
              <Paragraph>{content}</Paragraph>
            </Item>
          </Link>
        ))
      : new Array(3).fill(null).map((_, index) => <Item key={index} $isLoading />)}
  </Wrapper>
);

How this library works?

Inside, this library converts dataStream promises to components surrounded by <Suspense />. The contents of <Suspense /> are streamed as stringified JSON to a bunch of hidden <div /> tags. Later, on client side, the library extract contents of these <div /> tags to get the data. Currently MutationObserver is used to listen to changes to streamed data. Hacky as hell but it seems to work just right.

Final note

If you have some ideas how to improve the library, open an Issue or PR. Also, you can directly write to me:

https://github.com/michal-wrzosek/

[email protected]


This library was built using react-component-lib