# @sanity/diff

> Generates diffs between documents and primitive types

Latest version **6.15.0** (published 2026-09-17) · MIT license · 738.9K weekly downloads

## Install

```sh
npm install @sanity/diff
pnpm add @sanity/diff
yarn add @sanity/diff
bun add @sanity/diff
```

## Health

**Score 85/100 (A)** — status: active.

Positive: moderate downloads; has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

## Facts

| | |
|---|---|
| Version | 6.15.0 |
| Published | 2026-09-17 |
| First published | 2020-09-09 |
| Weekly downloads | 738.9K |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22.12 |
| Dependencies | 1 |
| Unpacked size | 74.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 6322 |
| Author | Sanity.io <hello@sanity.io> |
| Maintainers | sanity-svc.npm, sanity-io |
| Keywords | cms, content, diff, headless, realtime, sanity |

## Links

- npm: https://www.npmjs.com/package/@sanity/diff
- Repository: https://github.com/sanity-io/sanity
- Homepage: https://www.sanity.io/
- Issues: https://github.com/sanity-io/sanity/issues
- npm.io page: https://npm.io/package/@sanity/diff

## Dependencies (1)

- [@sanity/diff-match-patch](https://npm.io/package/@sanity/diff-match-patch.md) ^3.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 6.15.0 (latest) — 2026-09-17
- 6.16.0-next.21 (next) — 2026-09-19
- 6.11.0 (stable) — 2026-08-25
- 4.22.1 (maintenance-v4) — 2026-08-19
- 5.31.2 (maintenance-v5) — 2026-08-19
- 6.5.0-workbench.20260714145319 (workbench) — 2026-07-14
- 6.0.0-next-major.20260611102044 (next-major) — 2026-06-11
- 5.25.0-canary.20260507124900 (canary) — 2026-05-07
- 5.14.2-alpha.15 (alpha) — 2026-03-11
- 4.20.2-fix-tsa-expression-not-allowed.2.5e90622c2d (fix-tsa-expression-not-allowed) — 2025-12-04
- 4.20.2-fix-babel-traverse-bug.2.03a7495fed (fix-babel-traverse-bug) — 2025-12-04
- 4.1.2-rita-prerelease-ref.28 (rita-prerelease-ref) — 2025-07-24
- 4.1.2-4.2.0-rita-prerelease-tag.0.28 (4.2.0-rita-prerelease-tag.0) — 2025-07-23
- 4.1.1-bump-pkg-utils.6 (bump-pkg-utils) — 2025-07-22
- 3.99.1-bitter-lemon.5 (bitter-lemon) — 2025-07-18
- … 4185 more at https://npm.io/package/@sanity/diff/versions

## README

# @sanity/diff: Tools for diffing data structures

`@sanity/diff` is a library for calculating and presenting _diffs_ of content.

## Concepts and architecture

- The main data structure is `Diff` which represents a difference between two versions. This is a
  nested data structure so if it's an `ObjectDiff`, then its children will have `Diff` as well.

  The `Diff` is built on top of the _unchanged_, _added_ and _removed_ primitives. This means that
  it will contain both versions at the same time and it's always trivial to recreate the old/new
  version (by ignoring the added/removed parts of the diff).

  Note that for arrays and objects, unchanged/added/removed only have a "shallow" meaning. An
  `ObjectDiff` will have a _unchanged field_ if the field was present in both the old and new
  version - regardless of whether there's any internal changes.

- `Diff` also supports _annotations_. These contain information about when a change was introduced
  and who was responsible for it.

- To construct a `Diff` you need to represent the versions as `Input` types and use `diffInput(from,to)`
  to create the diff. The primary reason for a separate `Input` type is to support passing along
  annotations. In addition, this allows us to optimize based on the object equality of the inputs.

- There are multiple ways of _presenting_ a diff: Sometimes you want to only show the fields that
  has changed, and other times you want to show the full new (or old!) document interspersed with the
  changes.

  This library does _not_ contain any UI components, but instead provides various
  presentation-related helper functions.

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