Fireship · @fireship

Next.js in 100 Seconds // Plus Full Beginner's Tutorial

Uploaded January 5, 2021 · Length 11:52

Next.js in 100 Seconds // Plus Full Beginner's Tutorial

Summary

Learn Next.js fundamentals, server-side rendering, and data fetching in this beginner's tutorial.

  • nextjs tutorial
  • server-side rendering
  • data fetching
  • react framework
  • web development

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:00

    next js create fast search engine

  3. 0:02

    optimize react apps with zero

  4. 0:04

    configuration

  5. 0:05

    a traditional react app is rendered

  6. 0:07

    client side where the browser starts

  7. 0:09

    with a shell of an html page

  8. 0:11

    lacking any rendered content from there

  9. 0:13

    the browser fetches the javascript file

  10. 0:14

    containing the react code to render

  11. 0:16

    content to the page

  12. 0:17

    and make it interactive but there are

  13. 0:19

    two major drawbacks with client-side

  14. 0:21

    rendering

  15. 0:22

    one the content is not reliably indexed

  16. 0:24

    by all search engines

  17. 0:25

    or read by social media link bots and

  18. 0:27

    two it can take longer to reach the

  19. 0:29

    first contentful paint when a user first

  20. 0:31

    lands on the web page

  21. 0:32

    next is a framework that allows you to

  22. 0:34

    build a react app but render the content

  23. 0:36

    in advance on the server

  24. 0:38

    so the first thing a user or search bot

  25. 0:40

    sees is the fully rendered html

  26. 0:42

    after receiving this initial page

  27. 0:44

    client-side rendering takes over

  28. 0:45

    and it works just like a traditional

  29. 0:47

    react app it's the best of both worlds

  30. 0:49

    fully rendered content for bots highly

  31. 0:51

    interactive content for users

  32. 0:53

    inside of a next project you have a

  33. 0:54

    pages directory each javascript file

  34. 0:56

    defined here

  35. 0:57

    exports a react component that

  36. 0:59

    represents a route in the application

  37. 1:01

    in other words the file structure here

  38. 1:03

    mirrors the actual urls that the user

  39. 1:05

    will navigate to

  40. 1:06

    and next provides its own router to make

  41. 1:08

    navigation seamless

  42. 1:09

    but the real magic comes into play when

  43. 1:11

    we talk about data fetching

  44. 1:12

    because next can perform multiple server

  45. 1:14

    rendering strategies from a single

  46. 1:16

    project

  47. 1:17

    static generation or pre-rendering

  48. 1:19

    allows you to render your pages

  49. 1:21

    at build time each page or component can

  50. 1:23

    implement a function called

  51. 1:25

    get static props it might fetch data

  52. 1:27

    from a cloud database then pass the data

  53. 1:29

    as props to the component

  54. 1:30

    you can then build your app to render

  55. 1:32

    out all the html locally

  56. 1:33

    and upload it to a storage bucket where

  57. 1:35

    it can be easily cached by a cdn

  58. 1:37

    that works great for a blog or any kind

  59. 1:39

    of app where the data doesn't change

  60. 1:41

    often

  61. 1:41

    but if the data does change often you

  62. 1:43

    can implement server side rendering

  63. 1:45

    which builds the html page

  64. 1:46

    each time it's requested by the user in

  65. 1:49

    the component we implement data fetching

  66. 1:51

    with the get server side props function

  67. 1:53

    instead of running at build time this

  68. 1:55

    function runs at request time

  69. 1:56

    that means the page will fetch the

  70. 1:58

    latest data on the server each time a