Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
}
},
"noDefaultExport": "off",
"useNamingConvention": "off"
"useNamingConvention": "off",
"noImplicitBoolean": "off"
},
"correctness": {
"noNodejsModules": "off",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import type { NextConfig } from 'next';
import nextra from 'nextra';

const basePath = process.env.CI ? '/next-shared-cache' : '';

const withNextra = nextra({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.jsx',
staticImage: true,
});

export default withNextra({
const nextConfig: NextConfig = {
output: 'export',
basePath,
images: { unoptimized: true },
env: {
NEXT_PUBLIC_BASE_URL: basePath,
},
eslint: {
ignoreDuringBuilds: true,
},
});
};

const withNextra = nextra({});

export default withNextra(nextConfig);
19 changes: 10 additions & 9 deletions docs/cache-handler-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
"private": true,
"type": "module",
"scripts": {
"build:docs": "next build",
"dev:docs": "next dev",
"build:docs": "next build && pagefind --site .next/server/app --output-path out/_pagefind",
"dev:docs": "next dev --turbo",
"eslint:check": "next lint",
"eslint:fix": "next lint --fix",
"start:docs": "serve out"
},
"dependencies": {
"next": "14.2.15",
"nextra": "3.2.4",
"nextra-theme-docs": "3.2.4",
"react": "18.3.1",
"react-dom": "18.3.1"
"next": "15.1.6",
"nextra": "4.2.4",
"nextra-theme-docs": "4.2.4",
"react": "19.0.0",
"react-dom": "19.0.0"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "22.13.1",
"@types/react": "18.3.12",
"@types/react-dom": "18.3.1",
"@types/react": "19.0.8",
"@types/react-dom": "19.0.3",
"eslint": "9.20.0",
"pagefind": "1.3.0",
"serve": "14.2.4",
"typescript": "5.7.3"
}
Expand Down
Binary file removed docs/cache-handler-docs/public/favicon-16x16.png
Binary file not shown.
Binary file removed docs/cache-handler-docs/public/favicon-32x32.png
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import type { MetaRecord } from 'nextra';

const meta: MetaRecord = {
'-- Getting Started': {
type: 'separator',
title: 'Getting Started',
Expand All @@ -12,14 +14,12 @@ export default {
},
redis: 'Built-in Redis Handler',
'cluster-example': {
title: 'Redis Cluster example',
title: 'Redis Cluster example',
href: 'https://github.com/mauroaccornero/cache-handler-redis-cluster-example',
newWindow: true,
},
'k8s-example': {
title: 'Kubernetes example',
title: 'Kubernetes example',
href: 'https://github.com/ezeparziale/nextjs-k8s',
newWindow: true,
},
'-- API Reference': {
type: 'separator',
Expand All @@ -32,19 +32,19 @@ export default {
type: 'separator',
title: 'Troubleshooting',
},
troubleshooting: '',
troubleshooting: 'Troubleshooting',
'-- More': {
type: 'separator',
title: 'More',
},
'official-example': {
title: 'Official Next.js template',
title: 'Official Next.js template',
href: 'https://github.com/vercel/next.js/tree/canary/examples/cache-handler-redis',
newWindow: true,
},
'next.js-link': {
title: 'Next.js Configuring caching Docs',
title: 'Next.js Configuring caching Docs',
href: 'https://nextjs.org/docs/app/building-your-application/deploying#configuring-caching',
newWindow: true,
},
};

export default meta;
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ A Promise that resolves when the cache entry has been successfully deleted.

This method is called by the `CacheHandler` class when [`revalidateTag` ↗](https://nextjs.org/docs/app/api-reference/functions/revalidateTag) is called. It should delete all cache entries that are associated with the specified tag. It will be called for each Handler from the `handlers` array in parallel.

The tag list is stored in the `CacheHandlerValue` object and can be accessed via the `tags` property. If you use a remote cache store like Redis, it will be too expensive to retrieve all keys just to check tags. So, you may want to store tags in a separate set inside your cache store or [create an index](https://redis.io/docs/interact/search-and-query/indexing/) for tags.
The tag list is stored in the `CacheHandlerValue` object and can be accessed via the `tags` property. If you use a remote cache store like Redis, it will be too expensive to retrieve all keys just to check tags. So, you may want to store tags in a separate set inside your cache store or [create an index](https://redis.io/docs/interact/search-and-query/indexing/) for tags.

### `delete`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# onCreation

The `onCreation` static method of the `CacheHandler` class is designed to facilitate the creation of a custom cache instance with configurable behavior. It accepts a single argument, `onCreationHook`, which is a function that returns a `CacheHandlerConfig` object.

## `onCreationHook`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Callout } from 'nextra/components';

# neshClassicCache

`neshClassicCache` allows you to cache the results of expensive operations, like database queries, and reuse them across multiple requests. Unlike the [`neshCache`](/functions/nesh-cache) or [`unstable_cache` ↗](https://nextjs.org/docs/app/api-reference/functions/unstable_cache) function, `neshClassicCache` must be used in a Next.js Pages Router allowing users to cache data in the `getServerSideProps` and API routes.

This function is experimental, the API may change in the future. Use with caution.

<Callout type="info">
Cache entries created with `neshClassicCache` can be revalidated only by the
[`revalidateTag`
↗](https://nextjs.org/docs/app/api-reference/functions/revalidateTag) method.
</Callout>
> [!NOTE]
>
> Cache entries created with `neshClassicCache` can be revalidated only by the [`revalidateTag` ↗](https://nextjs.org/docs/app/api-reference/functions/revalidateTag) method.

## Parameters

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Callout } from 'nextra/components';

# `experimental-redis-cluster` Handler

```js
Expand All @@ -20,10 +18,9 @@ const redisHandler = createClusterHandler({
// ...
```

<Callout type="warning" emoji="⚠️">
This Handler is currently experimental and subject to change or removal in
future updates without a major version increment. Use with caution.
</Callout>
> [!WARNING]
>
> This Handler is currently experimental and subject to change or removal in future updates without a major version increment. Use with caution.

The `experimental-redis-cluster` Handler uses the Redis Cluster as the cache store. It is a simple and fast cache handler that is suitable for most applications. This Handler is ideal for applications that require fast and efficient cache management without the need for advanced features like full-text search for custom revalidation strategies.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Callout } from 'nextra/components';

# `local-lru` Handler

```js
Expand All @@ -15,10 +13,9 @@ const localHandler = createLocalHandler({

The `local-lru` Handler uses a [`lru-cache` ↗](https://www.npmjs.com/package/lru-cache) instance as the cache store. It stores the cache in memory and evicts the least recently used entries when the cache reaches its limits. You can use this Handler as a fallback cache when the shared cache is unavailable.

<Callout type="info">
The `local-lru` Handler stores the cache in memory. Make sure to set the
limits according to your server's memory capacity.
</Callout>
> [!NOTE]
>
> The `local-lru` Handler is not suitable for production environments. It is intended for development and testing purposes only.

## API

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Callout } from 'nextra/components';

# `redis-stack` Handler

```js
Expand All @@ -19,10 +17,9 @@ const redisHandler = createRedisHandler({

The `redis-stack` Handler uses Redis with `RedisJSON` and `RedisSearch` modules as the cache store. It is a full-featured cache handler that supports JSON objects and full-text search, offering a trade-off between performance and flexibility. While it may sacrifice some speed compared to the [`redis-strings`](/handlers/redis-strings) Handler, it empowers you to manage cache on demand more efficiently and faster. It also allows for more versatile cache usage from your application, such as creating custom revalidating strategies and functions. The `redis-stack` Handler is suitable for applications that require advanced cache management and search capabilities.

<Callout type="info">
Make sure that you have the `RedisJSON` and `RedisSearch` modules installed
and enabled in your Redis server.
</Callout>
> [!NOTE]
>
> Make sure that you have the `RedisJSON` and `RedisSearch` modules installed and enabled in your Redis server.

## API

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Callout } from 'nextra/components';

## Getting Started with `@neshca/cache-handler`
# Getting Started with `@neshca/cache-handler`

This section guides you through the initial setup and basic usage of `@neshca/cache-handler`, helping you integrate advanced caching solutions into your Next.js applications seamlessly.

### Prerequisites
## Prerequisites

- **Node.js:** 18.17 or newer.
- **Next.js:** 13.5.1 or newer (below 15.0.0).
- **Redis (optional):** 4.6.0 or newer.

### Quick Start Installation
## Quick Start Installation

```sh copy
npx create-next-app --example cache-handler-redis cache-handler-redis-app
Expand All @@ -24,7 +22,7 @@ yarn create next-app --example cache-handler-redis cache-handler-redis-app
pnpm create next-app --example cache-handler-redis cache-handler-redis-app
```

### Manual installation
## Manual installation

1. **Install `@neshca/cache-handler`**:
Execute this command in your Next.js project root directory:
Expand All @@ -40,7 +38,7 @@ pnpm create next-app --example cache-handler-redis cache-handler-redis-app
npm install redis
```

### Basic Custom Configuration
## Basic Custom Configuration

1. **Create a Cache Handler File**:
In your project root, create a file named `cache-handler.mjs` for your cache configuration.
Expand Down Expand Up @@ -89,11 +87,9 @@ pnpm create next-app --example cache-handler-redis cache-handler-redis-app
export default CacheHandler;
```

<Callout type="info">
Do not import <code>@neshca/cache-handler</code> to your client components
or pages. It is only meant to be used in <code>cache-handler.mjs</code>{' '}
files.
</Callout>
> [!NOTE]
>
> Do not import <code>@neshca/cache-handler</code> to your client components or pages. It is only meant to be used in <code>cache-handler.mjs</code> files.

3. **Integrate with Next.js**:
Update your `next.config.js` to utilize the cache handler, ensuring it's only active in production:
Expand Down Expand Up @@ -136,15 +132,13 @@ pnpm create next-app --example cache-handler-redis cache-handler-redis-app
}
```

<Callout type="info">
Use <code>NEXT_RUNTIME</code> environment variable to ensure that the
instrumentation is only executed in Node.js. Use dynamic import to avoid
bundling issues.
</Callout>
> [!NOTE]
>
> Use <code>NEXT_RUNTIME</code> environment variable to ensure that the instrumentation is only executed in Node.js. Use dynamic import to avoid bundling issues.

This instrumentation will fill the cache with the initial data when the application starts. The duration of this process may vary based on the number and size of pages, routes, and fetch calls. For more information refer to the [Populating the Cache with the initial data](/usage/populating-cache-on-start) section.

### Running Your Application
## Running Your Application

**Start Your Next.js Application**:

Expand All @@ -155,7 +149,7 @@ npm run build
npm run start
```

### Next Steps
## Next Steps

With the setup complete, explore `@neshca/cache-handler`'s advanced features:

Expand Down
62 changes: 62 additions & 0 deletions docs/cache-handler-docs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Footer, Layout, Navbar } from 'nextra-theme-docs';
import { Banner } from 'nextra/components';
import { getPageMap } from 'nextra/page-map';
import type { ReactNode } from 'react';
import 'nextra-theme-docs/style.css';

const banner = (
<Banner storageKey="version-1.9.0">
<div>
🎉 Version 1.9.0 is out! This is the final release supporting Next.js
13.5.1-14.x. The upcoming version 2.0.0 will require Next.js 15.
</div>
</Banner>
);

const navbar = (
<Navbar
logo={<pre>@neshca/cache-handler</pre>}
projectLink="https://github.com/caching-tools/next-shared-cache"
/>
);

const footer = (
<Footer>
<span>
MIT {new Date().getFullYear()} ©{' '}
<a
href="https://github.com/caching-tools/next-shared-cache"
rel="noreferrer noopener"
target="_blank"
>
@neshca/cache-handler
</a>
.
</span>
</Footer>
);

type RootLayoutProps = {
children: ReactNode;
};

export default async function RootLayout({
children,
}: RootLayoutProps): Promise<ReactNode> {
return (
<html lang="en" dir="ltr" suppressHydrationWarning>
<body>
<Layout
banner={banner}
navbar={navbar}
pageMap={await getPageMap()}
docsRepositoryBase="https://github.com/caching-tools/next-shared-cache/tree/canary/docs/cache-handler-docs"
footer={footer}
>
{/* @ts-expect-error - will be fixed when all react types will be updated to v19*/}
{children}
</Layout>
</body>
</html>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# `@neshca/cache-handler`

Welcome to `@neshca/cache-handler` (pronounced [`/ˈnæʃkʌ/`](http://ipa-reader.xyz/?text=%CB%88n%C3%A6%CA%83k%CA%8C)), a specialized ISR/Data cache API crafted for Next.js applications. This library simplifies configuring shared cache strategies in distributed environments, such as multiple, independent application instances. It offers a flexible and easy-to-integrate solution for custom cache strategies, especially for Redis.

---
Expand All @@ -13,7 +15,7 @@ Next.js applications are often deployed in a self-hosted distributed environment
- **Support for Next.js Routers**: Full support and one setup for the Pages and the App Router.
- **`neshCache` Function**: Utilize the [`neshCache`](/functions/nesh-cache) function to replace `unstable_cache` for more control over caching.
- **`neshClassicCache` Function**: Use the CacheHandler in the Pages Router to cache the result of expensive operations in the `getServerSideProps` and API routes.
- **Pre-populate the Cache with Initial Data**: Automatically pre-populate the cache with the initial data when the application starts using the [instrumentation hook](/usage/populating-cache-on-start).
- **Pre-populate the Cache with Initial Data**: Automatically pre-populate the cache with the initial data when the application starts using the [instrumentation hook](/usage/populating-cache-on-start).

## Getting Started

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Callout } from 'nextra/components';
# `redis-stack` Handler example

## `redis-stack` Handler example

<Callout type="info">
In this example, we assume that in your deployment, you have `REDIS_URL`
environment variable set to the URL of your Redis instance. You can use any
other way to set the URL.
</Callout>
> [!NOTE]
>
> In this example, we assume that in your deployment, you have `REDIS_URL` environment variable set to the URL of your Redis instance. You can use any other way to set the URL.

See the [`redis-stack` Handler](/handlers/redis-stack) documentation for more information and requirements for the Redis server.

Expand Down
Loading