-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnextjs.html
More file actions
1489 lines (1473 loc) · 57.5 KB
/
nextjs.html
File metadata and controls
1489 lines (1473 loc) · 57.5 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Next.js — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="Next.js — Ruby on Rails Guides" />
<meta name="description" content="Next.jsNext.js is a framework for react that also includes backend functionality.After working through this guide you should know how to build server rendered pages in next.js know how to build api endpoints in next.js" />
<meta property="og:description" content="Next.jsNext.js is a framework for react that also includes backend functionality.After working through this guide you should know how to build server rendered pages in next.js know how to build api endpoints in next.js" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<h1>Next.js</h1><p>Next.js is a framework for react
that also includes backend functionality.</p><p>After working through this guide you should</p>
<ul>
<li>know how to build server rendered pages in next.js</li>
<li>know how to build api endpoints in next.js</li>
</ul>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#what-is-next-js-questionmark">What is next.js?</a></li>
<li><a href="#what-does-it-offer-questionmark">What does it offer?</a></li>
<li><a href="#the-missing-persistence-layer">The missing persistence layer</a></li>
<li><a href="#next-js-routing">next.js routing</a></li>
<li><a href="#next-js-as-a-static-site-generator">next.js as a Static Site Generator</a>
<ul>
<li><a href="#next-config-js-for-static-site-generation">next.config.js for static site generation</a></li>
<li><a href="#using-github-or-gitlab-pages">using Github or gitlab pages</a></li>
</ul></li>
<li><a href="#next-js-for-api-endpoints">next.js for API endpoints</a></li>
<li><a href="#server-side-rendering-ssr">Server Side Rendering (SSR)</a></li>
<li><a href="#react-server-components">React Server Components</a>
<ul>
<li><a href="#how-to-use-rsc">How to use RSC</a></li>
<li><a href="#server-components-can-contain-client-components">Server Components can contain client components:</a></li>
<li><a href="#client-components-cannot-contain-server-components">Client Components cannot contain server components:</a></li>
<li><a href="#you-can-pass-in-server-components-to-a-clientcomponent">You can pass in Server Components to a ClientComponent</a></li>
<li><a href="#server-comopnents-cannot-pass-functions-to-its-descendents">Server Comopnents cannot pass functions to its descendents</a></li>
<li><a href="#how-rsc-works">How RSC Works</a></li>
</ul></li>
<li><a href="#hosting-next-js">Hosting next.js</a></li>
<li><a href="#telemetry">Telemetry</a></li>
<li><a href="#see-also">See Also</a></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_nextjs.html#/0'>◻</a></p>
<h2 id="what-is-next-js-questionmark"><a class="anchorlink" href="#what-is-next-js-questionmark"><span>1</span> What is next.js?</a></h2><p>Next.js is an open source framework, but it is mainly developed by Vercel.
Vercel's business is platform a service. They published next.js in 2016.
React documentation mentions Next.js among "Recommended Toolchains"
since at least 2021.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_nextjs.html#/1'>◻</a></p>
<h2 id="what-does-it-offer-questionmark"><a class="anchorlink" href="#what-does-it-offer-questionmark"><span>2</span> What does it offer?</a></h2><p>Next.js gives you a folder structure for you application,
and several different ways to render webpages:</p>
<ul>
<li>static webpages that are rendered once, at build time.</li>
<li>api endpoints</li>
<li>server rendered pages that are served as HTML, but can be refreshed.</li>
<li>React Server Components, where you can mix client and server side components in one react tree</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_nextjs.html#/2'>◻</a></p>
<h2 id="the-missing-persistence-layer"><a class="anchorlink" href="#the-missing-persistence-layer"><span>3</span> The missing persistence layer</a></h2><p>Next.js is not a full backend framework. For example it does not offer an
ORM or another persistence layer. There is good documentation for
combining next.js with</p>
<ul>
<li><a href="https://supabase.com/docs/guides/getting-started/quickstarts/nextjs">superbase</a>, which offers Postgres as a service</li>
<li><a href="https://www.apollographql.com/blog/apollo-client/next-js/how-to-use-apollo-client-with-next-js-13/">apollo</a>, which offers GraphQL</li>
<li><a href="https://www.prisma.io/nextjs">prisma</a>, a ORM for typescript or javascript</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_nextjs.html#/3'>◻</a></p>
<h2 id="next-js-routing"><a class="anchorlink" href="#next-js-routing"><span>4</span> next.js routing</a></h2><p>From next.js 13 onwards the routes are created in the <code>app/</code> directory.
<code>page.js</code> is the default (like index.html used to be), folders can contain parameters in brackets:</p><div class="interstitial code">
<pre><code class="highlight plaintext">app/page.js --> /
app/otherpage/page.js --> /otherpage
app/users/page.js --> /users/
app/users/[:id]/page.js --> /users/42/
</code></pre>
<button class="clipboard-button" data-clipboard-text="app/page.js --> /
app/otherpage/page.js --> /otherpage
app/users/page.js --> /users/
app/users/[:id]/page.js --> /users/42/
">Copy</button>
</div>
<p>There are special files:</p>
<ul>
<li><code>layout.js</code> for <a href="https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#nesting-layouts">nested layouts</a></li>
<li><code>error.js</code> for <a href="https://nextjs.org/docs/app/building-your-application/routing/error-handling">error handling</a> with error boundaries</li>
</ul>
<p><img src="images/nested-layouts-ui.png" alt=""></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-4' href='slides_nextjs.html#/4'>◻</a></p>
<h2 id="next-js-as-a-static-site-generator"><a class="anchorlink" href="#next-js-as-a-static-site-generator"><span>5</span> next.js as a Static Site Generator</a></h2><p>You can use <code>next.js</code> like <code>jekyll</code>, <code>eleventy</code> or <code>gatsby</code> as a <a href="https://jamstack.org/generators/">static
site generator</a>: during build time, html files are generated and can
then be served by a minimal webserver without backend capabilities.</p><p>This is the classic <a href="https://jamstack.org/">Jamstack</a> (where JAM stands for JavaScript, API and Markup).</p><p>See <a href="https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation">Static Site Generation (SSG)</a></p>
<ul>
<li>create a nextjs app</li>
<li>in next.config.js add one or two values (see below)</li>
<li><code>npm run build</code></li>
<li>find the static files in folder <code>out/</code></li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-5' href='slides_nextjs.html#/5'>◻</a></p>
<h3 id="next-config-js-for-static-site-generation"><a class="anchorlink" href="#next-config-js-for-static-site-generation"><span>5.1</span> next.config.js for static site generation</a></h3><p>We must set <code>output</code> to <code>export</code> to enable static site generations.</p><p>For static pages we need to set <code>trailingSlash</code> to <code>true</code> on most webservers.</p><p>If the site will be hosted in a subfolder, for example at <a href="http://bjelline.pages.mediacube.at/statixnextjs/">http://bjelline.pages.mediacube.at/statixnextjs/</a>
we must configure this as the config value <code>basePath</code>:</p><div class="interstitial code">
<pre><code class="highlight plaintext">const nextConfig = {
output: 'export',
trailingSlash: true,
basePath: '/foldername',
}
</code></pre>
<button class="clipboard-button" data-clipboard-text="const nextConfig = {
output: 'export',
trailingSlash: true,
basePath: '/foldername',
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-6' href='slides_nextjs.html#/6'>◻</a></p>
<h3 id="using-github-or-gitlab-pages"><a class="anchorlink" href="#using-github-or-gitlab-pages"><span>5.2</span> using Github or gitlab pages</a></h3><p>You can host your static files gitlab pages with the following configuration:
gitlab CI is used to build the pages, and the resulting folder <code>out</code> is declared
as an artifact. Gitlab will pick up this artifact and serve it through its
pages webserver.</p><div class="interstitial code">
<pre><code class="highlight plaintext">image: node
before_script:
- npm install
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/
pages:
script:
- npm run build
- mv public old_republic
- mv out public
artifacts:
paths:
- public
only:
- main
</code></pre>
<button class="clipboard-button" data-clipboard-text="image: node
before_script:
- npm install
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/
pages:
script:
- npm run build
- mv public old_republic
- mv out public
artifacts:
paths:
- public
only:
- main
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-7' href='slides_nextjs.html#/7'>◻</a></p>
<h2 id="next-js-for-api-endpoints"><a class="anchorlink" href="#next-js-for-api-endpoints"><span>6</span> next.js for API endpoints</a></h2><p>Use files called <code>route.js</code> as <a href="https://nextjs.org/docs/app/building-your-application/routing/router-handlers">route handlers</a> to implement the endpoints.</p><div class="interstitial code">
<pre><code class="highlight js"><span class="c1">// file /app/greeting/route.js</span>
<span class="k">import</span> <span class="p">{</span> <span class="nx">NextResponse</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">next/server</span><span class="dl">'</span>
<span class="k">export</span> <span class="k">async</span> <span class="kd">function</span> <span class="nf">GET</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">NextResponse</span><span class="p">.</span><span class="nf">json</span><span class="p">({</span> <span class="dl">"</span><span class="s2">hello</span><span class="dl">"</span><span class="p">:</span> <span class="dl">"</span><span class="s2">world</span><span class="dl">"</span> <span class="p">})</span>
<span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="// file /app/greeting/route.js
import { NextResponse } from 'next/server'
export async function GET() {
return NextResponse.json({ "hello": "world" })
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-8' href='slides_nextjs.html#/8'>◻</a></p>
<h2 id="server-side-rendering-ssr"><a class="anchorlink" href="#server-side-rendering-ssr"><span>7</span> Server Side Rendering (SSR)</a></h2><p>With server side rendering, a HTML document is shipped to the browser.
Then the browser loads the necessary JavaScript, and
<a href="https://react.dev/reference/react-dom/hydrate#hydrating-server-rendered-html">hydrates</a> the HTML
into a client side React App.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-9' href='slides_nextjs.html#/9'>◻</a></p>
<h2 id="react-server-components"><a class="anchorlink" href="#react-server-components"><span>8</span> React Server Components</a></h2><p>The react render tree is composed from server and client components. In next.js 13 all components
are server components by default. You have to add <code>"use client";</code> on top of a component to turn
it into a client component.</p><p><img src="images/react-server-components.png" alt=""></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-10' href='slides_nextjs.html#/10'>◻</a></p>
<h3 id="how-to-use-rsc"><a class="anchorlink" href="#how-to-use-rsc"><span>8.1</span> How to use RSC</a></h3><p>Simple rules for client and server components:</p>
<ul>
<li>Use <code>.server.js</code> and <code>.client.js</code> as filename extensions.</li>
<li>Server Components can contain client components.</li>
<li>Client Components cannot contain server components.</li>
<li>Server Components can instantiate both client and server components, and pass in a Server Component as the children prop to a ClientComponent.</li>
<li>Server Components cannot pass functions as props to its descendents, only data.</li>
<li>Use modules to share data in server components, use <a href="https://nextjs.org/docs/getting-started/react-essentials#context">context</a> to share data in client components.</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-11' href='slides_nextjs.html#/11'>◻</a></p>
<h3 id="server-components-can-contain-client-components"><a class="anchorlink" href="#server-components-can-contain-client-components"><span>8.2</span> Server Components can contain client components:</a></h3><div class="interstitial code">
<pre><code class="highlight js"><span class="c1">// this is server_component.server.js</span>
<span class="k">import</span> <span class="nx">ClientComponent</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./ClientComponent.client</span><span class="dl">'</span>
<span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">ServerComponent</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return </span><span class="p">(</span>
<span class="o"><></span>
<span class="o"><</span><span class="nx">ClientComponent</span> <span class="o">/></span>
<span class="o"><</span><span class="sr">/</span><span class="err">>
</span> <span class="p">)</span>
<span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="// this is server_component.server.js
import ClientComponent from './ClientComponent.client'
export default function ServerComponent() {
return (
<>
<ClientComponent />
</>
)
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-12' href='slides_nextjs.html#/12'>◻</a></p>
<h3 id="client-components-cannot-contain-server-components"><a class="anchorlink" href="#client-components-cannot-contain-server-components"><span>8.3</span> Client Components cannot contain server components:</a></h3><div class="interstitial code">
<pre><code class="highlight js"><span class="c1">// this is client_component.client.js</span>
<span class="c1">// ERROR !!!!</span>
<span class="k">import</span> <span class="nx">ServerComponent</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./ServerComponent.server</span><span class="dl">'</span>
<span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">ClientComponent</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return </span><span class="p">(</span>
<span class="o"><</span><span class="nx">div</span><span class="o">></span>
<span class="o"><</span><span class="nx">ServerComponent</span> <span class="o">/></span>
<span class="o"><</span><span class="sr">/div</span><span class="err">>
</span> <span class="p">)</span>
<span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="// this is client_component.client.js
// ERROR !!!!
import ServerComponent from './ServerComponent.server'
export default function ClientComponent() {
return (
<div>
<ServerComponent />
</div>
)
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-13' href='slides_nextjs.html#/13'>◻</a></p>
<h3 id="you-can-pass-in-server-components-to-a-clientcomponent"><a class="anchorlink" href="#you-can-pass-in-server-components-to-a-clientcomponent"><span>8.4</span> You can pass in Server Components to a ClientComponent</a></h3><div class="interstitial code">
<pre><code class="highlight js"><span class="c1">// this is outer_server_component.server.js</span>
<span class="k">import</span> <span class="nx">ClientComponent</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./ClientComponent.client</span><span class="dl">'</span>
<span class="k">import</span> <span class="nx">ServerComponent</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">./ServerComponent.server</span><span class="dl">'</span>
<span class="k">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nf">OuterServerComponent</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return </span><span class="p">(</span>
<span class="o"><</span><span class="nx">ClientComponent</span><span class="o">></span>
<span class="o"><</span><span class="nx">ServerComponent</span> <span class="o">/></span>
<span class="o"><</span><span class="sr">/ClientComponent</span><span class="err">>
</span> <span class="p">)</span>
<span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="// this is outer_server_component.server.js
import ClientComponent from './ClientComponent.client'
import ServerComponent from './ServerComponent.server'
export default function OuterServerComponent() {
return (
<ClientComponent>
<ServerComponent />
</ClientComponent>
)
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-14' href='slides_nextjs.html#/14'>◻</a></p>
<h3 id="server-comopnents-cannot-pass-functions-to-its-descendents"><a class="anchorlink" href="#server-comopnents-cannot-pass-functions-to-its-descendents"><span>8.5</span> Server Comopnents cannot pass functions to its descendents</a></h3><p>Because props are serialized into JSON, and functions cannot be serialized into JSON,
Server Components cannot pass functions as props to its descendents.</p><div class="interstitial code">
<pre><code class="highlight js"><span class="c1">// ERROR !!!!</span>
<span class="kd">function</span> <span class="nf">SomeServerComponent</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="o"><</span><span class="nx">button</span> <span class="nx">onClick</span><span class="o">=</span><span class="p">{()</span> <span class="o">=></span> <span class="nf">alert</span><span class="p">(</span><span class="dl">'</span><span class="s1">OHHAI</span><span class="dl">'</span><span class="p">)}</span><span class="o">></span><span class="nx">Click</span> <span class="nx">me</span><span class="o">!<</span><span class="sr">/button</span><span class="err">>
</span><span class="p">}</span>
<span class="c1">// ERROR !!!!</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="// ERROR !!!!
function SomeServerComponent() {
return <button onClick={() => alert('OHHAI')}>Click me!</button>
}
// ERROR !!!!
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-15' href='slides_nextjs.html#/15'>◻</a></p>
<h3 id="how-rsc-works"><a class="anchorlink" href="#how-rsc-works"><span>8.6</span> How RSC Works</a></h3><p>its complicated</p><p><a href="https://github.com/reactwg/server-components/discussions/5">RSC Components from Scratch</a></p><p><a href="https://www.plasmic.app/blog/how-react-server-components-work">How Server Components Work</a></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-16' href='slides_nextjs.html#/16'>◻</a></p>
<h2 id="hosting-next-js"><a class="anchorlink" href="#hosting-next-js"><span>9</span> Hosting next.js</a></h2><p>Vercel, the company behind next.js, offers hosting on their platform. There is a free tier.</p><p>To host next.js you need:</p>
<ul>
<li>to run the build step to create static assets and javascript bundles</li>
<li>to run node.js on the server</li>
</ul>
<p>If you are using dokku a node.js buildpack will be chosen by default,
and do both for you. you might additionally consider:</p>
<ul>
<li>setting up permanent storage at <code>public/upload</code>, if you want to handle uploaded file and store them in the webspcae</li>
<li>setting up a database</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-17' href='slides_nextjs.html#/17'>◻</a></p>
<h2 id="telemetry"><a class="anchorlink" href="#telemetry"><span>10</span> Telemetry</a></h2><p>A next.js app sends at least 500 lines of information to Vercel.
See <a href="https://nextjs.org/telemetry">Telemetry</a>
to learn what information is sent and how to switch it off.</p><div class="interstitial code">
<pre><code class="highlight plaintext">[telemetry] {
"eventName": "NEXT_CLI_SESSION_STARTED",
"payload": {
"nextVersion": "13.4.7",
"nodeVersion": "v18.16.1",
"cliCommand": "build",
"isSrcDir": false,
"hasNowJson": false,
"isCustomServer": null,
"hasNextConfig": true,
"buildTarget": "default",
"hasWebpackConfig": false,
"hasBabelConfig": false,
"imageEnabled": true,
"imageFutureEnabled": true,
"basePathEnabled": false,
"i18nEnabled": false,
"locales": null,
"localeDomainsCount": null,
"localeDetectionEnabled": null,
"imageDomainsCount": 0,
"imageRemotePatternsCount": 0,
"imageSizes": "16,32,48,64,96,128,256,384",
"imageLoader": "default",
"imageFormats": "image/webp",
"nextConfigOutput": null,
"trailingSlashEnabled": false,
"reactStrictMode": false,
"webpackVersion": 5,
"turboFlag": false,
"appDir": true,
"pagesDir": false
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "build-lint",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "@auth/prisma-adapter",
"packageVersion": "^1.0.0"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "@prisma/client",
"packageVersion": "^4.16.1"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "autoprefixer",
"packageVersion": "10.4.14"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "date-fns",
"packageVersion": "^2.30.0"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "eslint",
"packageVersion": "8.43.0"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "eslint-config-next",
"packageVersion": "13.4.7"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "next",
"packageVersion": "13.4.7"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "next-auth",
"packageVersion": "^4.22.1"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "postcss",
"packageVersion": "8.4.24"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "prisma",
"packageVersion": "^4.16.1"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "react",
"packageVersion": "18.2.0"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "react-dom",
"packageVersion": "18.2.0"
}
}
[telemetry] {
"eventName": "NEXT_PACKAGE_DETECTED",
"payload": {
"packageName": "tailwindcss",
"packageVersion": "3.3.2"
}
}
[telemetry] {
"eventName": "NEXT_BUILD_COMPLETED",
"payload": {
"durationInSeconds": 10,
"totalAppPagesCount": 4,
"totalPageCount": 0,
"hasDunderPages": false,
"hasTestPages": false
}
}
[telemetry] {
"eventName": "NEXT_LINT_CHECK_COMPLETED",
"payload": {
"durationInSeconds": 0,
"eslintVersion": "8.43.0",
"lintedFilesCount": 14,
"lintFix": false,
"nextEslintPluginVersion": "13.4.7",
"nextEslintPluginErrorsCount": 0,
"nextEslintPluginWarningsCount": 0,
"nextRulesEnabled": {
"@next/next/no-html-link-for-pages": "error",
"@next/next/no-sync-scripts": "error",
"@next/next/google-font-display": "warn",
"@next/next/google-font-preconnect": "warn",
"@next/next/next-script-for-ga": "warn",
"@next/next/no-before-interactive-script-outside-document": "warn",
"@next/next/no-css-tags": "warn",
"@next/next/no-head-element": "warn",
"@next/next/no-img-element": "warn",
"@next/next/no-page-custom-font": "warn",
"@next/next/no-styled-jsx-in-document": "warn",
"@next/next/no-title-in-document-head": "warn",
"@next/next/no-typos": "warn",
"@next/next/no-unwanted-polyfillio": "warn",
"@next/next/inline-script-id": "error",
"@next/next/no-assign-module-variable": "error",
"@next/next/no-document-import-in-page": "error",
"@next/next/no-duplicate-head": "error",
"@next/next/no-head-import-in-document": "error",
"@next/next/no-script-component-in-head": "error"
},
"buildLint": true
}
}
[telemetry] {
"eventName": "NEXT_TYPE_CHECK_COMPLETED",
"payload": {
"durationInSeconds": 0,
"typescriptVersion": null
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "experimental/optimizeCss",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "experimental/nextScriptWorkers",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "optimizeFonts",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_BUILD_OPTIMIZED",
"payload": {
"durationInSeconds": 13,
"staticPageCount": 0,
"staticPropsPageCount": 0,
"serverPropsPageCount": 0,
"ssrPageCount": 0,
"hasStatic404": true,
"hasReportWebVitals": false,
"rewritesCount": 0,
"headersCount": 0,
"redirectsCount": 0,
"headersWithHasCount": 0,
"rewritesWithHasCount": 0,
"redirectsWithHasCount": 0,
"middlewareCount": 0,
"totalAppPagesCount": 4,
"staticAppPagesCount": 2,
"serverAppPagesCount": 2,
"edgeRuntimeAppCount": 0,
"edgeRuntimePagesCount": 0,
"totalPageCount": 0,
"hasDunderPages": false,
"hasTestPages": false
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcLoader",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcMinify",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcRelay",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcStyledComponents",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcReactRemoveProperties",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcExperimentalDecorators",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcRemoveConsole",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcImportSource",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swcEmotion",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/x86_64-apple-darwin",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/x86_64-unknown-linux-gnu",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/x86_64-pc-windows-msvc",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/i686-pc-windows-msvc",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/aarch64-unknown-linux-gnu",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/armv7-unknown-linux-gnueabihf",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/aarch64-apple-darwin",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/aarch64-linux-android",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/arm-linux-androideabi",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/x86_64-unknown-freebsd",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/x86_64-unknown-linux-musl",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/aarch64-unknown-linux-musl",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "swc/target/aarch64-pc-windows-msvc",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "turbotrace",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "transpilePackages",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "skipMiddlewareUrlNormalize",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "skipTrailingSlashRedirect",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "modularizeImports",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/image",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/future/image",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/legacy/image",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/script",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/dynamic",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "@next/font/google",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "@next/font/local",
"invocationCount": 0
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/font/google",
"invocationCount": 1
}
}
[telemetry] {
"eventName": "NEXT_BUILD_FEATURE_USAGE",
"payload": {
"featureName": "next/font/local",
"invocationCount": 0
}
}
</code></pre>
<button class="clipboard-button" data-clipboard-text="[telemetry] {
"eventName": "NEXT_CLI_SESSION_STARTED",
"payload": {
"nextVersion": "13.4.7",
"nodeVersion": "v18.16.1",
"cliCommand": "build",
"isSrcDir": false,
"hasNowJson": false,
"isCustomServer": null,
"hasNextConfig": true,
"buildTarget": "default",
"hasWebpackConfig": false,
"hasBabelConfig": false,
"imageEnabled": true,
"imageFutureEnabled": true,
"basePathEnabled": false,
"i18nEnabled": false,
"locales": null,
"localeDomainsCount": null,
"localeDetectionEnabled": null,
"imageDomainsCount": 0,
"imageRemotePatternsCount": 0,