Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
cache: 'npm'
cache-dependency-path: ./packages/nextjs-cache-handler/package-lock.json
- run: npm ci
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: npm run build --if-present
- run: npm test
6 changes: 3 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ jobs:
- name: Copy README file to package directory
run: |
cp ../../README.md .
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
cache: npm
cache: pnpm
node-version: lts/*
cache-dependency-path: ${{ env.rootDir }}/package-lock.json
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: pnpm install --frozen-lockfile
- name: Publish to npm
run: |
if [ "${{ github.event.inputs.prerelease }}" == "true" ]; then
Expand Down
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Turborepo
.turbo

# Dependencies
node_modules


# Build outputs
dist
.next

# Logs
*.log

# OS
.DS_Store

# IDE
.vscode
.idea
*.swp
*.swo

4 changes: 4 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pnpm configuration
shamefully-hoist=true
strict-peer-dependencies=false

98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,104 @@ This project was originally based on [`@neshca/cache-handler`](https://www.npmjs

For context or historical documentation, you may still reference the [original project](https://caching-tools.github.io/next-shared-cache).

## neshClassicCache

⚠️ Deprecated: This function was migrated from @neshca for compatibility purposes only. Use with caution - no further development or support is planned.

`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.


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

### Parameters

#### `fetchData`

This is an asynchronous function that fetches the data you want to cache. It must be a function that returns a `Promise`.

#### `commonOptions`

This is an object that controls how the cache behaves. It can contain the following properties:

- `tags` - An array of tags to associate with the cached result. Tags are used to revalidate the cache using the `revalidateTag` and `revalidatePath` functions.

- `revalidate` - The revalidation interval in seconds. Must be a positive integer or `false` to disable revalidation. Defaults to `export const revalidate = time;` in the current route.

- `argumentsSerializer` - A function that serializes the arguments passed to the callback function. Use it to create a cache key. Defaults to `JSON.stringify(args)`.

- `resultSerializer` - A function that serializes the result of the callback function.Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.

- `resultDeserializer` - A function that deserializes the string representation of the result of the callback function. Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.

- `responseContext` - The response context object. If provided, it is used to set the cache headers acoording to the `revalidate` option. Defaults to `undefined`.

### Returns

`neshClassicCache` returns a function that when invoked, returns a `Promise` that resolves to the cached data. If the data is not in the cache, the provided function will be invoked, and its result will be cached and returned. The first argument is the `options` which can be used to override the common [`options`](/functions/nesh-classic-cache#commonoptions). In addition, there is a `cacheKey` option that can be used to provide a custom cache key.

### Example

```jsx filename="src/pages/api/api-example.js" copy
import { neshClassicCache } from '@fortedigital/nextjs-cache-handler/functions';
import axios from 'axios';

export const config = {
runtime: 'nodejs',
};

async function getViaAxios(url) {
try {
return (await axios.get(url.href)).data;
} catch (_error) {
return null;
}
}

const cachedAxios = neshClassicCache(getViaAxios);

export default async function handler(request, response) {
if (request.method !== 'GET') {
return response.status(405).send(null);
}

const revalidate = 5;

const url = new URL('https://api.example.com/data.json');

// Add tags to be able to revalidate the cache
const data = await cachedAxios(
{ revalidate, tags: [url.pathname], responseContext: response },
url,
);

if (!data) {
response.status(404).send('Not found');

return;
}

response.json(data);
}
```

---

## Contributing

This project uses [Turborepo](https://turbo.build/repo) to manage the monorepo structure with the main package and examples.

### Prerequisites

- Node.js >= 22.0.0
- pnpm >= 9.0.0

### Development Workflow

- **Start dev server**: `pnpm dev` (runs all dev servers in parallel)
- **Run all tests**: `pnpm test`

---

## License
Expand Down
3 changes: 0 additions & 3 deletions examples/redis-minimal/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import type { NextConfig } from "next";
const nextConfig: NextConfig = {
cacheHandler: require.resolve("./cache-handler.mjs"),
cacheMaxMemorySize: 0, // disable default in-memory caching
experimental: {
//ppr: "incremental",
},
};

export default nextConfig;
Loading