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-isomorphic

v0.0.2

Published

Provides a system to share state between server and client using cookies

Downloads

5

Readme

next-isomorphic

Share state between the client and server using cookies.

Install

# npm
npm install next-isomorphic
# yarn
yarn add next-isomorphic
# pnpm
pnpm add next-isomorphic

Usage

Define your initial state on the server

// isomorphic.server.ts
import { createIsomorphicStore } from "next-isomorphic/server";

export const appStore = createIsomorphicStore("myPrefix", {
  isSidebarOpen: true as boolean, // required because ts infer this as `true`
  isDark: true as boolean,
});

// Export the type to allow the client to read the state type
export type AppStore = ReturnType<typeof appStore>;

Declare a client for use the state client-side

// isomorphic.client.ts
"use client";

import { createIsomorphicClient } from "next-isomorphic/client";
import { type AppStore } from "./isomorphic.server";

export const isomorphicClient = createIsomorphicClient<AppStore>({
  // You can also listen for change events
  onChange({ newState }) {
    if (newState.isDark) {
      document.documentElement.classList.add("dark");
    } else {
      document.documentElement.classList.remove("dark");
    }
  },
});

Use and change the state on the client. Any component can listen for the changes and change the value and affect all the others consumers.

'use client';

export function MyComponent() {
  const [isDark, setIsDark] = isomorphicClient.isDark.useValue();
  const [isSidebarOpen, setIsSidebarOpen] = isomorphicClient.isSidebarOpen.useValue();

  return <div>{/* jsx */}</div>;
}

How it works?

The state is preserved using insecure cookies (document.cookie), this can be set and read client and server side.

Because the cookies are not secure should not be used to store sensitive information, also cookies have a max size, this depend on the browser but for Google Chrome this is around 4096 bytes.

https://chromestatus.com/feature/4946713618939904

The values to store should be JSON serializable, so values like Date cannot be store, instead you could store the date as a number Date.now() or convert the date to string for example using date.toISOString() and create the date using it.

What information can I store?

Information like the user theme, language, and state for UI elements like if the sidebar is open or an element is visible can be store using the isomorphic store.