Skip to content

Commit 9d723e1

Browse files
committed
Merge branch 'master' into next16
2 parents e117ae9 + 06be1a0 commit 9d723e1

File tree

19 files changed

+8394
-15030
lines changed

19 files changed

+8394
-15030
lines changed

.github/workflows/node.js.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19+
- uses: pnpm/action-setup@v4
1920
- name: Use Node.js
2021
uses: actions/setup-node@v4
2122
with:
2223
node-version: 22.x
23-
cache: 'npm'
24-
cache-dependency-path: ./packages/nextjs-cache-handler/package-lock.json
25-
- run: npm ci
24+
cache: 'pnpm'
25+
- run: pnpm install --frozen-lockfile
2626
- run: npm run build --if-present
2727
- run: npm test

.github/workflows/npm-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ jobs:
2323
- name: Copy README file to package directory
2424
run: |
2525
cp ../../README.md .
26+
- uses: pnpm/action-setup@v4
2627
- uses: actions/setup-node@v4
2728
with:
28-
cache: npm
29+
cache: pnpm
2930
node-version: lts/*
30-
cache-dependency-path: ${{ env.rootDir }}/package-lock.json
3131
registry-url: "https://registry.npmjs.org"
32-
- run: npm ci
32+
- run: pnpm install --frozen-lockfile
3333
- name: Publish to npm
3434
run: |
3535
if [ "${{ github.event.inputs.prerelease }}" == "true" ]; then

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Turborepo
2+
.turbo
3+
4+
# Dependencies
5+
node_modules
6+
7+
8+
# Build outputs
9+
dist
10+
.next
11+
12+
# Logs
13+
*.log
14+
15+
# OS
16+
.DS_Store
17+
18+
# IDE
19+
.vscode
20+
.idea
21+
*.swp
22+
*.swo
23+

.npmrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# pnpm configuration
2+
shamefully-hoist=true
3+
strict-peer-dependencies=false
4+

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,104 @@ This project was originally based on [`@neshca/cache-handler`](https://www.npmjs
473473
474474
For context or historical documentation, you may still reference the [original project](https://caching-tools.github.io/next-shared-cache).
475475
476+
## neshClassicCache
477+
478+
⚠️ Deprecated: This function was migrated from @neshca for compatibility purposes only. Use with caution - no further development or support is planned.
479+
480+
`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.
481+
482+
483+
> [!NOTE]
484+
>
485+
> Cache entries created with `neshClassicCache` can be revalidated only by the [`revalidateTag` ↗](https://nextjs.org/docs/app/api-reference/functions/revalidateTag) method.
486+
487+
### Parameters
488+
489+
#### `fetchData`
490+
491+
This is an asynchronous function that fetches the data you want to cache. It must be a function that returns a `Promise`.
492+
493+
#### `commonOptions`
494+
495+
This is an object that controls how the cache behaves. It can contain the following properties:
496+
497+
- `tags` - An array of tags to associate with the cached result. Tags are used to revalidate the cache using the `revalidateTag` and `revalidatePath` functions.
498+
499+
- `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.
500+
501+
- `argumentsSerializer` - A function that serializes the arguments passed to the callback function. Use it to create a cache key. Defaults to `JSON.stringify(args)`.
502+
503+
- `resultSerializer` - A function that serializes the result of the callback function.Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
504+
505+
- `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'))`.
506+
507+
- `responseContext` - The response context object. If provided, it is used to set the cache headers acoording to the `revalidate` option. Defaults to `undefined`.
508+
509+
### Returns
510+
511+
`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.
512+
513+
### Example
514+
515+
```jsx filename="src/pages/api/api-example.js" copy
516+
import { neshClassicCache } from '@fortedigital/nextjs-cache-handler/functions';
517+
import axios from 'axios';
518+
519+
export const config = {
520+
runtime: 'nodejs',
521+
};
522+
523+
async function getViaAxios(url) {
524+
try {
525+
return (await axios.get(url.href)).data;
526+
} catch (_error) {
527+
return null;
528+
}
529+
}
530+
531+
const cachedAxios = neshClassicCache(getViaAxios);
532+
533+
export default async function handler(request, response) {
534+
if (request.method !== 'GET') {
535+
return response.status(405).send(null);
536+
}
537+
538+
const revalidate = 5;
539+
540+
const url = new URL('https://api.example.com/data.json');
541+
542+
// Add tags to be able to revalidate the cache
543+
const data = await cachedAxios(
544+
{ revalidate, tags: [url.pathname], responseContext: response },
545+
url,
546+
);
547+
548+
if (!data) {
549+
response.status(404).send('Not found');
550+
551+
return;
552+
}
553+
554+
response.json(data);
555+
}
556+
```
557+
558+
---
559+
560+
## Contributing
561+
562+
This project uses [Turborepo](https://turbo.build/repo) to manage the monorepo structure with the main package and examples.
563+
564+
### Prerequisites
565+
566+
- Node.js >= 22.0.0
567+
- pnpm >= 9.0.0
568+
569+
### Development Workflow
570+
571+
- **Start dev server**: `pnpm dev` (runs all dev servers in parallel)
572+
- **Run all tests**: `pnpm test`
573+
476574
---
477575
478576
## License

0 commit comments

Comments
 (0)