Skip to content

Commit e3b8e94

Browse files
committed
Use cache
1 parent c44c385 commit e3b8e94

File tree

21 files changed

+652
-327
lines changed

21 files changed

+652
-327
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
import { CacheHandler } from '@neshca/cache-handler';
4+
5+
CacheHandler.onCreation(() => {
6+
return {
7+
handlers: [
8+
{
9+
name: 'handler-none',
10+
get: () => Promise.resolve(undefined),
11+
set: () => Promise.resolve(undefined),
12+
revalidateTag: () => Promise.resolve(undefined),
13+
delete: () => Promise.resolve(undefined),
14+
},
15+
],
16+
};
17+
});
18+
19+
export default CacheHandler;

apps/cache-testing/next.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const nextConfig: NextConfig = {
1515
// PPR should only be configured via the PPR_ENABLED env variable due to conditional logic in tests.
1616
ppr: process.env.PPR_ENABLED === 'true',
1717
largePageDataBytes: 1024 * 1024, // 1MB
18+
useCache: true,
19+
cacheHandlers: {
20+
default: './redis.js',
21+
},
1822
},
1923
};
2024

apps/cache-testing/redis.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import redis from '@neshca/cache-handler/use-cache/redis';
2+
3+
export default redis;

apps/cache-testing/src/app/app/no-params/dynamic-false/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { createGetData } from 'cache-testing/utils/create-get-data';
55

66
export const dynamicParams = false;
77

8-
export const revalidate = 5;
8+
const revalidate = 5;
99

1010
type PageParams = { params: Promise<{ slug: string }> };
1111

12-
const getData = createGetData('app/no-params/dynamic-false');
12+
const getData = createGetData('app/no-params/dynamic-false', revalidate);
1313

1414
export default async function Index({
1515
params,

apps/cache-testing/src/app/app/no-params/dynamic-true/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { createGetData } from 'cache-testing/utils/create-get-data';
55

66
export const dynamicParams = true;
77

8-
export const revalidate = 5;
8+
const revalidate = 5;
99

1010
type PageParams = { params: Promise<{ slug: string }> };
1111

12-
const getData = createGetData('app/no-params/dynamic-true');
12+
const getData = createGetData('app/no-params/dynamic-true', revalidate);
1313

1414
export default async function Index({
1515
params,

apps/cache-testing/src/app/app/no-params/ssr/200/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { notFound } from 'next/navigation';
33
import { CommonAppPage } from 'cache-testing/utils/common-app-page';
44
import { createGetData } from 'cache-testing/utils/create-get-data';
55

6-
const getData = createGetData('app/no-params/ssr', undefined, 'no-store');
6+
const getData = createGetData('app/no-params/ssr', 0);
77

88
export default async function Index(): Promise<React.ReactNode> {
99
const data = await getData('200');

apps/cache-testing/src/app/app/randomHex/[length]/page.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { notFound } from 'next/navigation';
2-
import { Suspense } from 'react';
3-
41
import { CacheStateWatcher } from 'cache-testing/components/cache-state-watcher';
52
import { PreRenderedAt } from 'cache-testing/components/pre-rendered-at';
63
import type { RandomHexPageProps } from 'cache-testing/utils/types';
4+
import { unstable_cacheTag as cacheTag } from 'next/cache';
5+
import { notFound } from 'next/navigation';
6+
import { Suspense } from 'react';
77

88
const lengthSteps = new Array(5).fill(0).map((_, i) => 10 ** (i + 1));
99

@@ -20,17 +20,17 @@ export async function generateStaticParams(): Promise<
2020
export default async function Page({
2121
params,
2222
}: PageParams): Promise<React.ReactNode> {
23+
'use cache';
24+
2325
const resolvedParams = await params;
2426
const { length } = resolvedParams;
2527
const path = `/randomHex/app/${length}`;
2628

29+
cacheTag(`/app/randomHex/${length}`);
30+
2731
const url = new URL(path, 'http://localhost:8081');
2832

29-
const result = await fetch(url, {
30-
next: {
31-
tags: [`/app/randomHex/${length}`],
32-
},
33-
});
33+
const result = await fetch(url);
3434

3535
if (!result.ok) {
3636
notFound();

apps/cache-testing/src/app/app/with-params/dynamic-false/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ type PageParams = { params: Promise<{ slug: string }> };
77

88
export const dynamicParams = false;
99

10-
export const revalidate = 5;
10+
const revalidate = 5;
1111

12-
const getData = createGetData('app/with-params/dynamic-false');
12+
const getData = createGetData('app/with-params/dynamic-false', revalidate);
1313

1414
export function generateStaticParams(): Promise<
1515
{

apps/cache-testing/src/app/app/with-params/dynamic-true/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ type PageParams = { params: Promise<{ slug: string }> };
77

88
export const dynamicParams = true;
99

10-
export const revalidate = 5;
10+
const revalidate = 5;
1111

12-
const getData = createGetData('app/with-params/dynamic-true');
12+
const getData = createGetData('app/with-params/dynamic-true', revalidate);
1313

1414
export function generateStaticParams(): Promise<
1515
{

apps/cache-testing/src/utils/create-get-data.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
import {
2+
unstable_cacheLife as cacheLife,
3+
unstable_cacheTag as cacheTag,
4+
} from 'next/cache';
15
import { normalizeSlug } from './normalize-slug';
26
import type { CountBackendApiResponseJson, PageProps } from './types';
37

4-
export function createGetData(
5-
path: string,
6-
revalidate?: number,
7-
cache?: RequestCache,
8-
) {
8+
export function createGetData(path: string, revalidate?: number) {
99
return async function getData(
1010
slug: string,
1111
): Promise<Omit<PageProps, 'revalidateAfter'> | null> {
12+
'use cache';
13+
1214
const pathAndTag = `/${path}/${normalizeSlug(slug)}`;
1315

14-
const url = new URL(`/count${pathAndTag}`, 'http://localhost:8081');
16+
cacheLife({
17+
revalidate,
18+
});
1519

16-
const tags = [pathAndTag, 'whole-app-route'];
20+
cacheTag(pathAndTag, 'whole-app-route');
1721

18-
const result = await fetch(url, {
19-
cache,
20-
next: { revalidate, tags },
21-
});
22+
const url = new URL(`/count${pathAndTag}`, 'http://localhost:8081');
23+
24+
const result = await fetch(url);
2225

2326
if (!result.ok) {
2427
return null;

0 commit comments

Comments
 (0)