Skip to content

Commit

Permalink
refactor: improve AppHome
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStack committed Sep 26, 2023
1 parent 1a22be5 commit 88e35a6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
Binary file modified public/images/app-page-tsx.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function Error({
<WarningIcon />
<h1>Oops, something went wrong!</h1>
<h5>change this in app/error.tsx</h5>
<h4>{error.message}</h4>
<Box sx={{ m: 5 }}>
<Button onClick={reset}>Try again</Button>
</Box>
Expand Down
19 changes: 10 additions & 9 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ import Homepage from '@/components/Homepage';

import { getApiResponse } from '@/utils/shared/get-api-response';

import { PageParams } from '@/types';
import { NpmData, PageParams } from '@/types';

const loadDataFromApi = async (slug?: string) => {
if (slug === 'testError500') {
throw new Error('This is a ssr 500 test error');
throw new Error('This is mock a ssr 500 test error');
}

// Fetch & cache data from 2 remote APIs test
const [reactNpmData, nextJsNpmData] = await Promise.all([
getApiResponse<{ version: string }>({
getApiResponse<NpmData>({
apiEndpoint: 'https://registry.npmjs.org/react/latest',
revalidate: 60 * 60 * 24, // 24 hours cache
}),
getApiResponse<{ version: string }>({
getApiResponse<NpmData>({
apiEndpoint: 'https://registry.npmjs.org/next/latest',
revalidate: 0, // no cache
}),
]);

return {
reactVersion: reactNpmData?.version,
nextJsVersion: nextJsNpmData?.version,
reactNpmData,
nextJsNpmData,
};
};

const AppHome = async ({ searchParams }: PageParams) => {
const apiResult = await loadDataFromApi(searchParams?.['slug']);
const slug = searchParams?.slug;
const { reactNpmData, nextJsNpmData } = await loadDataFromApi(slug);

return (
<Homepage
reactVersion={apiResult?.reactVersion}
nextJsVersion={apiResult?.nextJsVersion}
reactVersion={reactNpmData?.version}
nextJsVersion={nextJsNpmData?.version}
/>
);
};
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export interface PageParams {
params?: { id?: string };
searchParams?: { [key: string]: string | undefined };
}

export interface NpmData {
version: string;
}

0 comments on commit 88e35a6

Please sign in to comment.