Skip to content

Commit 1d61f4a

Browse files
committed
feat: fetch & cache data from 2 remote APIs test
1 parent c91ba68 commit 1d61f4a

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/app/api/test/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextResponse } from 'next/server';
22

3+
// Test local NextJs API /api/test method GET with parameters
34
export const GET = async (req: Request) => {
45
const { searchParams } = new URL(req.url);
56
const reqData = Object.fromEntries(searchParams);
@@ -10,6 +11,7 @@ export const GET = async (req: Request) => {
1011
});
1112
};
1213

14+
// Test local NextJs API /api/test method POST with variables
1315
export const POST = async (req: Request) => {
1416
const reqData = await req.json();
1517
return NextResponse.json({

src/app/page.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ const loadDataFromApi = async (slug?: string) => {
66
throw new Error('This is a ssr 500 test error');
77
}
88

9-
return await getApiResponse<{ version: string }>({
10-
apiEndpoint: 'https://registry.npmjs.org/react/latest',
11-
});
9+
// Fetch & cache data from 2 remote APIs test
10+
const [reactNpmData, nextJsNpmData] = await Promise.all([
11+
getApiResponse<{ version: string }>({
12+
apiEndpoint: 'https://registry.npmjs.org/react/latest',
13+
revalidate: 60 * 60 * 24, // 24 hours cache
14+
}),
15+
getApiResponse<{ version: string }>({
16+
apiEndpoint: 'https://registry.npmjs.org/next/latest',
17+
revalidate: 0, // no cache
18+
}),
19+
]);
20+
21+
return {
22+
reactVersion: reactNpmData?.version,
23+
nextJsVersion: nextJsNpmData?.version,
24+
};
1225
};
1326

1427
interface AppHomeProps {
@@ -18,5 +31,10 @@ interface AppHomeProps {
1831
export default async function AppHome({ searchParams }: AppHomeProps) {
1932
const apiResult = await loadDataFromApi(searchParams['slug']);
2033

21-
return <Homepage reactVersion={apiResult?.version} />;
34+
return (
35+
<Homepage
36+
reactVersion={apiResult?.reactVersion}
37+
nextJsVersion={apiResult?.nextJsVersion}
38+
/>
39+
);
2240
}

src/component/Homepage.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import PageFooter from '@/component/shared/PageFooter';
66
import ReactHookForm from '@/component/shared/ReactHookForm';
77
import { SITE_CONFIG } from '@/constant';
88

9-
export default function Homepage({ reactVersion = 'unknown' }) {
9+
export default function Homepage({
10+
reactVersion = 'unknown',
11+
nextJsVersion = 'unknown',
12+
}) {
1013
return (
1114
<main>
1215
<section>
@@ -33,7 +36,9 @@ export default function Homepage({ reactVersion = 'unknown' }) {
3336
gutterBottom
3437
sx={{ color: 'green', mt: 3 }}
3538
>
36-
Get data from api test: The latest React version is {reactVersion}
39+
Fetch & cache data from 2 remote APIs test: <br />
40+
The latest React version is {reactVersion}, and the latest NextJs
41+
version is {nextJsVersion}
3742
</Typography>
3843

3944
<Box sx={{ m: 5 }}>
@@ -57,12 +62,14 @@ export default function Homepage({ reactVersion = 'unknown' }) {
5762
href='/api/test?from=github&nextjs=yes&mui=yes&tailwind=no'
5863
target='_blank'
5964
>
60-
Test NextJs API method GET with parameters
65+
Test local NextJs API /api/test method GET with parameters
6166
</Link>
6267
</Box>
6368

6469
<Box sx={{ m: 5 }}>
65-
<h4>Test NextJs API method POST with parameters</h4>
70+
<h4>
71+
Test local NextJs API /api/test method POST with form variables
72+
</h4>
6673
<ReactHookForm />
6774
</Box>
6875

0 commit comments

Comments
 (0)