Skip to content

Commit 1fa7734

Browse files
committed
update 05-server-side-rendering
1 parent b003a13 commit 1fa7734

37 files changed

+65
-53
lines changed

04-frameworks/08-nextjs/05-server-side-rendering/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ _./app/cars/page.tsx_
2828
...
2929
const CarListPage = async () => {
3030
// cache: 'force-cache' is the default
31-
- const carList = await getCarList({ next: { revalidate: 10 } }); // In seconds
32-
+ const carList = await getCarList({ cache: 'no-store' });
31+
- const carList = await api.getCarList({ next: { revalidate: 10 } }); // In seconds
32+
+ const carList = await api.getCarList({ cache: 'no-store' });
3333
console.log('Car list at build time:', { carList });
3434

3535
return <CarList carList={mapCarListFromApiToVm(carList)} />;
@@ -82,7 +82,7 @@ npm run start:prod
8282
8383
We could add `options` property in the API method:
8484

85-
_./app/cars/\[carId\]/\_api/car.api.ts_
85+
_./pods/car/api/car.api.ts_
8686

8787
```diff
8888
import { envConstants } from '@/_core/constants';
@@ -104,33 +104,30 @@ export const getCar = async (
104104

105105
```
106106

107-
108107
_./app/cars/\[carId\]/page.tsx_
109108

110109
```diff
111110
import React from 'react';
112111
import { Metadata } from 'next';
113-
import { getCar } from './_api';
114-
import { Car } from './_components';
115-
import { mapCarFromApiToVm } from './car.mappers';
112+
import { Car, api, mapCarFromApiToVm } from '#pods/car';
116113

117114
interface Props {
118115
params: { carId: string };
119116
}
120117

121118
export const generateMetadata = async (props: Props): Promise<Metadata> => {
122119
const { params } = props;
123-
- const car = await getCar(params.carId);
124-
+ const car = await getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
120+
- const car = await api.getCar(params.carId);
121+
+ const car = await api.getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
125122
return {
126123
title: `Rent a car - Car ${car.name} details`,
127124
};
128125
};
129126

130127
const CarPage = async (props: Props) => {
131128
const { params } = props;
132-
- const car = await getCar(params.carId);
133-
+ const car = await getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
129+
- const car = await api.getCar(params.carId);
130+
+ const car = await api.getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
134131
console.log('Car page', car);
135132

136133
return <Car car={mapCarFromApiToVm(car)} />;
@@ -140,9 +137,13 @@ export default CarPage;
140137

141138
```
142139

143-
> `cache: 'force-cache'` and empty cache: will fetch data only once but it will re-render the component on each refresh (F5).
140+
> All options will re-render the component on each refresh (F5)
141+
142+
> `cache: 'force-cache'`: will fetch data only once.
143+
144+
>`cache: 'no-store'`: will fetch data on each refresh (F5).
144145
145-
>`cache: 'no-store'`: will fetch data and re-render the component on each refresh (F5).
146+
>`next: { revalidate: 10 },`: will fetch data on each refresh (F5) after revalidate seconds.
146147
147148
# About Basefactor + Lemoncode
148149

04-frameworks/08-nextjs/05-server-side-rendering/app/cars/[carId]/_components/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

04-frameworks/08-nextjs/05-server-side-rendering/app/cars/_components/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

04-frameworks/08-nextjs/05-server-side-rendering/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.

04-frameworks/08-nextjs/05-server-side-rendering/package.json

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33
"version": "1.0.0",
44
"description": "Nextjs examples",
55
"scripts": {
6-
"start": "run-p -l start:dev start:api-server",
6+
"start": "run-p start:dev start:api-server",
77
"start:dev": "next dev",
88
"build": "rimraf .next && next build",
99
"start:prod": "next start -p 8080",
1010
"start:api-server": "cd api-server && npm run mock-server",
1111
"postinstall": "cd ./api-server && npm install"
1212
},
13+
"imports": {
14+
"#*": [
15+
"./src/*",
16+
"./src/*.ts",
17+
"./src/*.tsx",
18+
"./src/*/index.ts",
19+
"./src/*/index.tsx"
20+
]
21+
},
1322
"author": "Lemoncode",
1423
"license": "MIT",
1524
"dependencies": {
16-
"next": "^14.1.0",
25+
"next": "^14.2.9",
1726
"normalize.css": "^8.0.1",
18-
"react": "^18.2.0",
19-
"react-dom": "^18.2.0",
20-
"sharp": "^0.33.2"
27+
"react": "^18.3.1",
28+
"react-dom": "^18.3.1",
29+
"sharp": "^0.33.5"
2130
},
2231
"devDependencies": {
23-
"@types/node": "20.11.16",
24-
"@types/react": "^18.2.55",
25-
"@types/react-dom": "^18.2.19",
32+
"@types/node": "22.5.4",
33+
"@types/react": "^18.3.5",
34+
"@types/react-dom": "^18.3.0",
2635
"npm-run-all": "^4.1.5",
27-
"rimraf": "^5.0.5",
28-
"typescript": "^5.3.3"
36+
"rimraf": "^6.0.1",
37+
"typescript": "^5.6.2"
2938
}
3039
}

04-frameworks/08-nextjs/05-server-side-rendering/app/cars/[carId]/page.tsx renamed to 04-frameworks/08-nextjs/05-server-side-rendering/src/app/cars/[carId]/page.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import React from 'react';
22
import { Metadata } from 'next';
3-
import { getCar } from './_api';
4-
import { Car } from './_components';
5-
import { mapCarFromApiToVm } from './car.mappers';
3+
import { Car, api, mapCarFromApiToVm } from '#pods/car';
64

75
interface Props {
86
params: { carId: string };
97
}
108

119
export const generateMetadata = async (props: Props): Promise<Metadata> => {
1210
const { params } = props;
13-
const car = await getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
11+
const car = await api.getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
12+
1413
return {
1514
title: `Rent a car - Car ${car.name} details`,
1615
};
1716
};
1817

1918
const CarPage = async (props: Props) => {
2019
const { params } = props;
21-
const car = await getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
20+
const car = await api.getCar(params.carId, { cache: 'no-store' }); // Check 'force-cache' too
2221
console.log('Car page', car);
2322

2423
return <Car car={mapCarFromApiToVm(car)} />;

04-frameworks/08-nextjs/05-server-side-rendering/app/cars/page.tsx renamed to 04-frameworks/08-nextjs/05-server-side-rendering/src/app/cars/page.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import React from 'react';
22
import { Metadata } from 'next';
3-
import { getCarList } from './_api';
4-
import { CarList } from './_components';
5-
import { mapCarListFromApiToVm } from './car-list.mappers';
3+
import { CarList, api, mapCarListFromApiToVm } from '#pods/car-list';
64

75
export const metadata: Metadata = {
86
title: 'Rent a car - Car list',
97
};
108

119
const CarListPage = async () => {
1210
// cache: 'force-cache' is the default value
13-
const carList = await getCarList({ cache: 'no-store' }); // In seconds
11+
const carList = await api.getCarList({ cache: 'no-store' });
1412
console.log('Car list at build time:', { carList });
1513

1614
return <CarList carList={mapCarListFromApiToVm(carList)} />;

0 commit comments

Comments
 (0)