-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
463 lines (451 loc) · 16.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>Server Components</title>
<link rel="stylesheet" href="dist/reset.css" />
<link rel="stylesheet" href="dist/reveal.css" />
<link rel="stylesheet" href="dist/theme/moon.css" id="theme" />
<!-- Theme used for syntax highlighted code -->
<link
rel="stylesheet"
href="plugin/highlight/monokai.css"
id="highlight-theme"
/>
<!-- Add icon logo -->
<link
rel="icon"
href="dist/assets/presentation/logo.png"
type="image/icon type"
/>
</head>
<body>
<div class="reveal">
<div class="slides">
<section
style="height: 100%"
data-menu-title="Server Components"
data-background-image="dist/assets/presentation/background_big.png"
>
<h2 style="bottom: 20%; right: 0; position: absolute">
REACT SERVER COMPONENTS
</h2>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>Introduction</h3>
</section>
<section>
<p>
Server and Client Components allow developers to build
applications that span the server and client, combining the
<strong>rich interactivity</strong> of client-side apps with the
<strong>improved performance</strong> of traditional server
rendering.
</p>
</section>
<section
data-background-iframe="https://nextjs.org/blog/next-13-4"
></section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>Thinking in Server Components</h3>
</section>
<section>
<p>
React Server Components introduce a new mental model for building
hybrid applications that leverage the server and the client.
</p>
</section>
<section>
<p>
Instead of React rendering your whole application client-side
(such as in the case of
<strong>Single-Page Applications</strong>), React now gives you
the flexibility to choose where to render your components based on
their purpose.
</p>
</section>
<section>
<img data-src="dist/assets/thinking-in-server-components.avif" />
<aside class="notes">
If we were to split the page into smaller components, you'll
notice that the majority of components are non-interactive and can
be rendered on the server as Server Components. For smaller pieces
of interactive UI, we can sprinkle in Client Components.
</aside>
</section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>Why Server Components?</h3>
</section>
<section>
<p>
Server Components allow developers to better
<strong>leverage server infrastructure</strong>. For example, you
can move data fetching to the server, closer to your database, and
keep large dependencies that previously would impact the client
JavaScript bundle size on the server, leading to improved
performance.
</p>
</section>
<section>
<p>
The initial page load is faster, and the client-side JavaScript
bundle size is reduced.
</p>
</section>
<section>
<p>
Additional JavaScript is only added as client-side interactivity
is used in your application through Client Components.
</p>
</section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>Client components</h3>
</section>
<section>
<p>
Client Components enable you to add client-side interactivity to
your application.
</p>
</section>
<section>
<p>
The <strong>use client</strong> directive is a convention to
declare a boundary between a Server and Client Component module
graph.
</p>
<pre><code data-line-numbers="1"><script type="text/template">'use client'
import { useState } from 'react'
export default function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={setCount(prev => prev + 1)}>
Click me
</button>
</div>
)}
</script></code></pre>
</section>
<section>
<img
class="r-stretch"
data-src="dist/assets/use-client-directive.avif"
/>
<aside class="notes">
"use client" sits between server-only and client code. It's placed
at the top of a file, above imports, to define the cut-off point
where it crosses the boundary from the server-only to the client
part. Once "use client" is defined in a file, all other modules
imported into it, including child components, are considered part
of the client bundle. Since Server Components are the default, all
components are part of the Server Component module graph unless
defined or imported in a module that starts with the "use client"
directive.
</aside>
</section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>When to use Server and Client Components?</h3>
</section>
<section>
<img data-src="dist/assets/when-to-use.png" />
<figcaption style="font-size: small">
Fig.1 -
<a
href="https://nextjs.org/docs/getting-started/react-essentials#when-to-use-server-and-client-components"
>Comparatives.</a
>
</figcaption>
</section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<section>
<h3>Patterns</h3>
</section>
<section>
<h4>Moving Client Components to the Leaves</h4>
<pre><code data-line-numbers><script type="text/template">// SearchBar is a Client Component
import SearchBar from './searchbar'
// Logo is a Server Component
import Logo from './logo'
// Layout is a Server Component by default
export default function Layout({ children }) {
return (
<>
<nav>
<Logo />
<SearchBar />
</nav>
<main>{children}</main>
</>
)}
</script></code></pre>
<aside class="notes">
To improve the performance of your application, we recommend
moving Client Components to the leaves of your component tree
where possible. For example, you may have a Layout that has static
elements (e.g. logo, links, etc) and an interactive search bar
that uses state. Instead of making the whole layout a Client
Component, move the interactive logic to a Client Component (e.g.
SearchBar) and keep your layout as a Server Component. This means
you don't have to send all the component Javascript of the layout
to the client.
</aside>
</section>
<section>
<h4>Composing Client and Server Components</h4>
<ul class="r-stack">
<li class="fragment fade-out" data-fragment-index="0">
On the <strong>server</strong>, React renders all Server
Components before sending the result to the client.
<ul>
<li>
This includes Server Components nested inside Client
Components.
</li>
<li>
Client Components encountered during this stage are skipped.
</li>
</ul>
</li>
<li class="fragment current-visible" data-fragment-index="0">
On the <strong>client</strong>, React renders Client Components
and slots in the rendered result of Server Components, merging
the work done on the server and client.
<ul>
<li>
If any Server Components are nested inside a Client
Component, their rendered content will be placed correctly
within the Client Component.
</li>
</ul>
</li>
</ul>
</section>
<section>
<h4 data-id="title">
Nesting Server Components inside Client Components
</h4>
</section>
<section data-auto-animate>
<h4 style="color: red" data-id="title">
Nesting Server Components inside Client Components
</h4>
<p>Unsupported Pattern</p>
</section>
<section>
<pre>
<code data-line-numbers="4, 12">
<script type="text/template">'use client'
// This pattern will **not** work! You cannot import a
// Server Component into a Client Component.
import ExampleServerComponent from './server-component'
export default function ExampleClientComponent({children}) {
const [count, setCount] = useState(0)
return (
<>
<button onClick={setCount(prev => prev + 1)}>
{count}
</button>
<ExampleServerComponent />
</>
)}
</script>
</code>
</pre>
</section>
<section data-auto-animate>
<h4 data-id="title">
Nesting Server Components inside Client Components
</h4>
<p>Recommended Pattern:</p>
<b>Passing Server Components to Client Components as Props</b>
<aside class="notes">
The Server Component will be rendered on the server, and when the
Client Component is rendered on the client, the "slot" will be
filled in with the rendered result of the Server Component.
</aside>
</section>
<section>
<pre><code data-line-numbers="5,13">
<script type="text/template">'use client'
import { useState } from 'react'
export default function ExampleClientComponent({children}) {
const [count, setCount] = useState(0)
return (
<>
<button onClick={setCount(prev => prev + 1)}>
{count}
</button>
{children}
</>
)}</script>
</code></pre>
<aside class="notes">
A common pattern is to use the React children prop to create the
"slot". We can refactor ExampleClientComponent to accept a generic
children prop and move the import and explicit nesting of
ExampleClientComponent up to a parent component. The only
responsibility ExampleClientComponent has is to decide where
whatever children will eventually be should be placed.
</aside>
</section>
<section>
<pre>
<code data-line-numbers="10">
<script type="text/template">// This pattern works:
// You can pass a Server Component as a child or prop of a
// Client Component.
import ExampleClientComponent from './client-component'
import ExampleServerComponent from './server-component'
export default function Page() {
return (
<ExampleClientComponent>
<ExampleServerComponent />
</ExampleClientComponent>
)
}</script>
</code>
</pre>
<aside class="notes">
In a parent Server Component, you can import both the
ExampleClientComponent and ExampleServerComponent and pass
ExampleServerComponent as a child of ExampleClientComponent. With
this approach, the rendering of ExampleClientComponent and
ExampleServerComponent are decoupled and can be rendered
independently - aligning with Server Components, which are
rendered on the server before Client Components.
</aside>
</section>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<h3>Other topics not covered</h3>
<ul>
<li>Using third-party packages.</li>
<li>Using context to share state between Client Components.</li>
<li>Sharing data between Server Components.</li>
</ul>
</section>
<section
data-background-image="dist/assets/presentation/background2.png"
>
<h3>Resources</h3>
<ul style="list-style-type: none">
<li>
<div style="display: flex; align-items: center; gap: 10px">
<img
style="padding: 0 4px"
width="32"
height="32"
data-src="dist/assets/nextjs-icon.png"
/>
<a
href="https://nextjs.org/docs/getting-started/react-essentials"
>Next.js - React Essentials</a
>
</div>
</li>
<li>
<div style="display: flex; align-items: center; gap: 10px">
<img
width="40"
height="32"
data-src="dist/assets/yt-icon.png"
/>
<a href="https://www.youtube.com/watch?v=kUs-fH1k-aM"
>Do you need SSR? by Theo - t3.gg</a
>
</div>
</li>
<li>
<div style="display: flex; align-items: center; gap: 10px">
<img
width="40"
height="32"
data-src="dist/assets/yt-icon.png"
/>
<a href="https://www.youtube.com/watch?v=TQQPAU21ZUw">
Data fetching with React Server Components
</a>
</div>
</li>
<li>
<div style="display: flex; align-items: center; gap: 10px">
<img
width="40"
height="32"
data-src="dist/assets/plasmic.svg"
/>
<a
href="https://www.plasmic.app/blog/how-react-server-components-work"
>
How React server components work
</a>
</div>
</li>
<li>
<div style="display: flex; align-items: center; gap: 10px">
<img
width="40"
height="32"
data-src="dist/assets/gitnation.png"
/>
<a
href="https://portal.gitnation.org/contents/react-server-components-under-the-hood"
>
RSC - Under the hood
</a>
</div>
</li>
</ul>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script src="plugin/reveal.js-menu/menu.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealMarkdown, RevealHighlight, RevealNotes, RevealMenu],
menu: {
themes: true,
transitions: true,
},
});
</script>
</body>
</html>