Skip to content

Commit 37af226

Browse files
committed
update 06-client-side-rendering
1 parent 1fa7734 commit 37af226

File tree

49 files changed

+116
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+116
-65
lines changed

04-frameworks/08-nextjs/00-boilerplate/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

04-frameworks/08-nextjs/01-config/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

04-frameworks/08-nextjs/02-navigation/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

04-frameworks/08-nextjs/03-boilerplate/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './components';
12
export * from './car-list.component';
23
export * as api from './api';
34
export * from './car-list.mappers';

04-frameworks/08-nextjs/04-static-site-generation/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './components';
12
export * from './car-list.component';
23
export * as api from './api';
34
export * from './car-list.mappers';

04-frameworks/08-nextjs/05-server-side-rendering/api-server/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from 'hono';
22
import { logger } from 'hono/logger';
3+
import { cors } from 'hono/cors';
34
import { serve } from '@hono/node-server';
45
import { serveStatic } from '@hono/node-server/serve-static';
56
import { cars } from './mock-data';
@@ -12,6 +13,8 @@ const app = new Hono();
1213
app.use(logger());
1314
app.use('/*', serveStatic({ root: './public' }));
1415

16+
app.use('/api/*', cors());
17+
1518
app.get('/api/cars', (context) => {
1619
return context.json(db.cars);
1720
});
@@ -23,7 +26,9 @@ app.get('/api/cars/:id', (context) => {
2326
app.put('/api/cars/:id', async (context) => {
2427
const id = context.req.param('id');
2528
const car = await context.req.json();
26-
db.cars = db.cars.map((c) => (c.id === id ? car : c));
29+
db.cars = db.cars.map((c) =>
30+
c.id === id ? { ...c, isBooked: car.isBooked } : c
31+
);
2732
return context.body(null, 204);
2833
});
2934

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './components';
12
export * from './car-list.component';
23
export * as api from './api';
34
export * from './car-list.mappers';

04-frameworks/08-nextjs/06-client-side-rendering/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ If we don't need to pre-render the data and frequently updating data.
1919
- Enalbe you to add client-side interactivity.
2020
- Need to use `'use client'` directive.
2121
- You cannot use Server Components inside a client-side component but you can use Server Components as children of a client-side component.
22-
- [More info about Client Components](https://nextjs.org/docs/getting-started/react-essentials#client-components)
22+
- [More info about Client Components](https://nextjs.org/docs/app/building-your-application/rendering/client-components)
2323

2424
Let's look up the api method to book a car:
2525

26-
_./app/cars/\[carId\]/\_api/car.api.ts_
26+
_./src/pods/car/api/car.api.ts_
2727

2828
```javascript
2929
...
3030

3131
export const bookCar = async (car: Car): Promise<boolean> => {
3232
await fetch(`${url}/${car.id}`, {
33-
method: 'PATCH',
33+
method: 'PUT',
3434
headers: { 'Content-Type': 'application/json' },
3535
body: JSON.stringify(car),
3636
});
@@ -41,7 +41,7 @@ export const bookCar = async (car: Car): Promise<boolean> => {
4141

4242
And how are we using it:
4343

44-
_./app/cars/\[carId\]/\_components/car.component.tsx_
44+
_./src/pods/car/car.component.tsx_
4545

4646
```javascript
4747
'use client';
@@ -85,7 +85,7 @@ IMAGES_DOMAIN=localhost
8585

8686
```
8787

88-
_./app/\_core/constants/env.constants.ts_
88+
_./src/core/constants/env.constants.ts_
8989

9090
```diff
9191
export const envConstants = {
@@ -106,7 +106,7 @@ npm run start:prod
106106

107107
On the other hand, we cannot use client-side interactivity in a Server Component but how can we implement a global `React context`?:
108108

109-
_./app/layout.tsx_
109+
_./src/app/layout.tsx_
110110

111111
```diff
112112
import 'normalize.css';
@@ -167,7 +167,7 @@ export default RootLayout;
167167

168168
It throws the error: `TypeError: createContext only works in Client Components. Add the "use client" directive`. But we can use like:
169169

170-
_./app/theme.context.tsx_
170+
_./src/core/theme.context.tsx_
171171

172172
```jsx
173173
'use client';
@@ -211,14 +211,14 @@ export const ThemeProvider = ({ children }) => {
211211

212212
> You cannot use a ServerComponent inside Client Component but [you can pass a Server Component as prop](https://nextjs.org/docs/getting-started/react-essentials#nesting-server-components-inside-client-components).
213213
214-
_./app/layout.tsx_
214+
_./src/app/layout.tsx_
215215

216216
```diff
217217
import 'normalize.css';
218218
import './material-icons.css';
219219
import React from 'react';
220220
import { Inter } from 'next/font/google';
221-
+ import { ThemeProvider } from './theme.context';
221+
+ import { ThemeProvider } from '#core/theme.context';
222222

223223
- const ThemeContext = React.createContext({});
224224

@@ -259,11 +259,11 @@ npm run start:prod
259259

260260
Using theme:
261261

262-
_./app/cars/\_components/nav.component.tsx_
262+
_./src/pods/car-list/components/nav.component.tsx_
263263

264264
```jsx
265265
'use client';
266-
import { ThemeContext } from '@/theme.context';
266+
import { ThemeContext } from '#core/theme.context';
267267
import React from 'react';
268268

269269
interface Props {
@@ -289,21 +289,21 @@ export const Nav: React.FC<Props> = (props) => {
289289

290290
```
291291

292-
_./app/cars/\_components/index.ts_
292+
_./src/pods/car-list/components/index.ts_
293293

294294
```diff
295295
export * from './car-list.component';
296296
+ export * from './nav.component';
297297

298298
```
299299

300-
_./app/cars/layout.tsx_
300+
_./src/app/cars/layout.tsx_
301301

302302
```diff
303303
import React from 'react';
304304
import Link from 'next/link';
305305
import Image from 'next/image';
306-
+ import { Nav } from './_components';
306+
+ import { Nav } from '#pods/car-list';
307307
import classes from './layout.module.css';
308308

309309
interface Props {

0 commit comments

Comments
 (0)