# next-isomorphic

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

Latest version **0.0.2** (published 2023-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install next-isomorphic
pnpm add next-isomorphic
yarn add next-isomorphic
bun add next-isomorphic
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2023-11-04 |
| First published | 2023-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Freddy L. |
| Maintainers | neociber94 |
| Keywords | nextjs, isomorphic, cookie, react |

## Links

- npm: https://www.npmjs.com/package/next-isomorphic
- npm.io page: https://npm.io/package/next-isomorphic

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.2 (latest) — 2023-11-04
- 0.0.1 — 2023-11-04

## README

# next-isomorphic

Share state between the client and server using cookies.

## Install

```bash
# npm
npm install next-isomorphic
```

```bash
# yarn
yarn add next-isomorphic
```

```bash
# pnpm
pnpm add next-isomorphic
```

## Usage

Define your initial state on the server

```ts
// 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

```ts
// 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.

```tsx
'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.

---
_Source: https://npm.io/package/next-isomorphic · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
