Next.js Static Site Generation (SSG) Tutorial

Поделиться
HTML-код
  • Опубликовано: 18 ноя 2024

Комментарии • 143

  • @bawad
    @bawad  4 года назад +57

    I was using incorrectly in the index.js page
    when navigating to dynamic pages you need to use href and as prop: nextjs.org/docs/api-reference/next/link#dynamic-routes
    should be:
    I updated the code on GitHub

    • @jeromesnail
      @jeromesnail 4 года назад +4

      Great video!
      One thing I would add is that when you build your static site, not alone the static HTML is rendered for pages using getStaticProps, but also next.js create a "virtual" API that serves JSON so that when your app is running, once you've loaded your first static page, the app will act like a normal client side react app. Hence the importance of the "as" prop in Link. If not use correctly it doesn't act like a SPA link but rather like a normal anchor tag. It's even worse when you use SSR as each page will be rendered on the server with browser refresh instead of just behaving like a SPA.

    • @JakelineSofiaCamposCabello
      @JakelineSofiaCamposCabello 4 года назад

      cherto!! Great video!

    • @JeremyBlackYoda
      @JeremyBlackYoda 4 года назад

      Hi Ben, thanks for catching that! I don't see the change on github index page - or is it somewhere else?

    • @illialukianov2803
      @illialukianov2803 4 года назад

      @@jeromesnail yep, but this is avaialable only with NextJS server right?
      Like they way Ben showed here - it is static site, as done in PHP or Python.
      The thing u r talking require NextJS to handle thos requests and can't be deployed to CDN easily without Vercel right?
      At least I am trying to find way to do this now, and didn't succeed yet. Do u have any ideas how to serve content of .next build folder from cdn without actual Next or Node server? Or at least with Nginx, not cdn?

    • @illialukianov2803
      @illialukianov2803 4 года назад

      @@jeromesnail because as far as I tested locally - Next gives u very low RPS like 10 (at least for our project, not very hard IMO) which is not enough for prod.
      From what I read - Vercel deploy each page as Lambda function, so it can scale it easily but what if I don't wanna use Vercel(Ziet)?

  • @chubbyshady
    @chubbyshady 4 года назад +44

    Add 'utf8' as a 2nd argument to readFileSync and will return a string

  • @violet-trash
    @violet-trash 4 года назад +24

    This is so much easier then Gatsby. No plugins, no configs, no bloat. It just works. ✋😌👌

    • @luismunoz9126
      @luismunoz9126 4 года назад +3

      No plugins, no Image optimization, no offline support, etc.

    • @rokonzamann
      @rokonzamann 4 года назад +1

      @@luismunoz9126 Yeah. but I believe those are not impossible to do. like offline support with "next-pwa/cache" or "next-offline". There should be for image optimization too.
      check out official examples...
      github.com/vercel/next.js/tree/canary/examples

    • @luismunoz9126
      @luismunoz9126 4 года назад +1

      @@rokonzamann nice one, thanks i will take a look, it seems some things has changed for good in next js.

    • @luismunoz9126
      @luismunoz9126 4 года назад +1

      @@HarryManchanda i love the future

  • @shilangyu
    @shilangyu 4 года назад +37

    You can do `console.log({ files })` and `console.log({ paths })` to retain the variable name :)

    • @jeromesnail
      @jeromesnail 4 года назад +6

      I was about to say the same thing. I even use console.table({ myVariable }) to have a nicer output :p

  • @callumn-d846
    @callumn-d846 4 года назад +33

    Great overview, would love to hear more about how we can use SSR and Static in a hybrid app

    • @jjuuiiccyyjj
      @jjuuiiccyyjj 4 года назад

      ^ this! great video btw :)

    • @temirzhanyussupov6997
      @temirzhanyussupov6997 4 года назад

      how would you explain what is a hybrid app?

    • @naynyamish270
      @naynyamish270 4 года назад +1

      @@temirzhanyussupov6997 a mix between static sites and webapp.

    • @jeromesnail
      @jeromesnail 4 года назад +6

      Basically you have nothing to do. Just use getStaticProps on your static pages, getInitialProps or getServerSideProps on your SSR pages, Next.js will do everything for you when you build you app, giving a summary of what pages are static, what pages are server side rendered. You can even export just the static site with next export, and put the output on a CDN, the app will work with static pages rendered at build and the rest of the app acting like a normal react app (without SSR).
      It's pretty amazing how easy Next.js is.

  • @andredealbuquerque7699
    @andredealbuquerque7699 4 года назад +16

    Great video!
    The SGG and SSR part of next is something to figure out. Please upload more on Next.js ! Keep up the great work

  • @RobertTodar
    @RobertTodar 4 года назад +3

    This is exactly what I was trying to figure out on my own. Thank you!!!! A nice bonus video would to be setting it up with netlifycms! 🤞

    • @divinenature6220
      @divinenature6220 4 года назад

      Yeah. I had to do mine another way. GitHub.com/DNature/divinehycenth.com

  • @yokiijay933
    @yokiijay933 4 года назад +2

    Thanks, it helped me a lot to understand the whole process. One detail should noticed, replace(/\.md$/, '') instead of replace('.md', '')

  • @danieldosen5260
    @danieldosen5260 2 года назад

    never had a need for next, but now I do - this vid is helpful... next up - to see what changed in two years :)

  • @ClaudioBernasconi
    @ClaudioBernasconi 3 года назад

    Thanks for this great introduction to static site generation using Next.js. I'm an experienced React app developer and I think that Next.js might be a great tool to build simple websites in the future for me.

  • @CoryTheSimmons
    @CoryTheSimmons 4 года назад +1

    So glad Next added dynamic routes. It's actually so good now.

  • @amandeepsbajwa
    @amandeepsbajwa 4 года назад +1

    Thanks Ben. That helps a lot. Please create more videos on NextJS

  • @maxiequa567
    @maxiequa567 4 года назад +20

    +1 For more Next.js and Headless CMS etc.

  • @kuscamara
    @kuscamara 4 года назад +2

    More Next tutorials please! 🙏
    Thank you for this work.

  • @SLP1177
    @SLP1177 4 года назад +1

    Great video Ben! I'd apreciate a video on upload files using next.js. Great job.

  • @happysloth91
    @happysloth91 4 года назад +20

    I don't think you need to import React, Next js does that for you. Unless ofc you need sth like useEffect, ...

    • @atsourno
      @atsourno 3 года назад

      why would you need useEffect on Next.js?

    • @thefakequake
      @thefakequake 3 года назад

      @@atsourno if you want to fetch a resource/send a request client side.

  • @JeremyBlackYoda
    @JeremyBlackYoda 4 года назад

    Thanks Ben for an excellent and succinct overview of the newest features of nextjs static site creation. Love to see more on Nextjs - working with some css to make it look great. (Try Tailwind or Chakra!) and remote data fetching using an open source CMS or Database like Hasura or Strapi or NetlifyCMS (community version) . You are a spot on teacher...
    .

  • @jeromesnail
    @jeromesnail 4 года назад

    I love the way Next.js handle SPA, SSG and SSR seamlessly. To me it's much more simple than Gatsby...

  • @sophiasminion7279
    @sophiasminion7279 4 года назад

    I definitely want more nextjs content. I have a cra that I want to convert to a ssr app.

  • @sundaybackpacker1482
    @sundaybackpacker1482 4 года назад

    Great job! I look forward to the next tutorials about Next.js !

  • @myke_roly
    @myke_roly 4 года назад

    Awesome!!! Im starting ssd with next.js and your video helpe me much!!

  • @adamkenton2585
    @adamkenton2585 4 года назад +4

    Can you show how to implement a dark mode in a next js app from scratch ?

  • @landonwjohnson
    @landonwjohnson 4 года назад

    This is going to be a game changer! I have been waiting for this.

  • @AlejandroVivas
    @AlejandroVivas 4 года назад

    Amazing video, very clear and easy to follow. Thank you!

  • @HaNdTriXo
    @HaNdTriXo 4 года назад

    Thanks for the awesome video!
    Since getStaticProps & getStaticPaths are async function you can actually use await fs.promises.readFile(...) this won't block the event loop.

  • @adrienblanc4662
    @adrienblanc4662 4 года назад

    Great video, would love more about Next.js, have a nice day

  • @AdrianTwarog
    @AdrianTwarog 4 года назад

    Very nice, I was planning to check out NextJS. Currently just spitting out a CRA app and that's probably not a good idea.

  • @jantomotulo3754
    @jantomotulo3754 4 года назад +1

    Thank you, i have better understanding what ssg is. Salut!

  • @zain5962
    @zain5962 4 года назад

    Please make a complete course on next.js

  • @YouWatchGames
    @YouWatchGames 4 года назад

    Really strange my html does not render correctly. If for example I put an inside the dangerouslysetinnerhtml, it renders it as plain text but when I click on inspect on chrome it shows the button tag.

  • @sky_kryst
    @sky_kryst 4 года назад +1

    Just started learning TypeScript today in the post quarantine world. What do you use TS or ES lint for typescript? Does ESlint have good enough linting for TS?

    • @bawad
      @bawad  4 года назад +1

      use eslint, tslint is deprecated

  • @kamilzielinski504
    @kamilzielinski504 4 года назад

    Great vid, thanks.
    How would it look like for hybrid static + client (no ssr) REST API call app ? Should I simply call the REST API dynamically and add the content? Is it a good practice or there is some more Next.js magic?

  • @AnamolMaharjan-e7g
    @AnamolMaharjan-e7g Год назад

    thank you for the video, clear

  • @SowedCastelli
    @SowedCastelli 4 года назад

    As always, thank you Ben

  • @MeziIroaga
    @MeziIroaga 2 года назад

    Hi Ben, thank you for the video! I am trying to export the static HTML page so I can use it elsewhere, but for some reason my out folder doesn't contain the html file itself, only the other files. Is there a way to fix this problem?

  • @KarahannAe
    @KarahannAe 4 года назад

    When I open the exported html files on the browser they dont work properly. I don't understand why do we have to serve them with "npx serve out" if they are static files?

  • @madukomablessed4712
    @madukomablessed4712 4 года назад

    Hey Ben, great content. Could you please do a new tutorial series for react native? I'd love that. Thanks

  • @sprioleau
    @sprioleau 4 года назад

    More Next.js content, please.

  • @irhamputra3666
    @irhamputra3666 4 года назад

    I don't know if Gatsby user are tempting with this cool feature from Next.js :D but imo they will stick to Gatsby because Gatsby has a lot of cool plugins

    • @jeresalem
      @jeresalem 4 года назад +1

      The only reason why I've stayed away from Gatsby - too much bundled in.

  • @mdjahidulislam9205
    @mdjahidulislam9205 3 года назад

    Ok its static site i understood thanks. But after deployment. What if i added a new post on my server.
    Then should i need to rebuild & deploy it again?

  • @martinvang2153
    @martinvang2153 4 года назад

    Hi Ben - thanks for another awesome video. I'm building a portal that has both public content and content behind user login. I was thinking if I use next.js SSR for the public content and then next.js SSG to serve a react CSR SPA (normal react app served using SSG). Is this a stupid approach and/or is there a better way to have use SSR and CSR on the same portal? PS: I'm completely new to next.js. My motivation is to have better SEO for the public pages by using SSR and then minimize the complexity of the private pages by using standard react (CSR) for those.

  • @fev4
    @fev4 4 года назад +1

    f'ing perfect timing man, thanks again

  • @nix6030
    @nix6030 3 года назад

    How if i want to .map the entire .md content for the sake of listing the entire blog so user can read the title, description before go to the blog ???

  • @dilsmatchanov
    @dilsmatchanov 4 года назад +1

    but what if I have lots of posts and when I wanna add/edit/delete a post, should I rebuild the whole project again?

    • @bawad
      @bawad  4 года назад +1

      yes
      if that gets to the point where it is too slow, that's where you might want to generate the pages at runtime when requested instead of at build time

  • @jzmmm
    @jzmmm 4 года назад

    Hey Ben, good video. Just a quick question, what are the manifest files for in the export static html files? Is there any way to remove them?

  • @eduludi
    @eduludi 4 года назад +1

    Great video, Ben! Thanks for it. Do you know if is possible to do incremental builds/exports? I mean, in case you have new or updated posts, only these will be generated, leaving untouched the rest. I think that will be really helpful when you have a lot of posts.

    • @bawad
      @bawad  4 года назад +1

      I don't think so

  • @gabbrissimo
    @gabbrissimo 3 года назад

    Can i ask you guys which hosting are you using for a nextjs app?

  • @DBAaron
    @DBAaron 3 года назад

    If I build a SSG website that allows users to upload blogposts via a form and then create markdown files, how will the website update? Do I need to deploy every minute?

  • @dhruvmaniya7149
    @dhruvmaniya7149 2 года назад

    is SSG only works on dynamic paths?
    And if it is not only for dynamic paths then
    Is this getStaticPaths and getStaticProps functions renders when component render or Next renders it when any pages of app renders if it is in different components too, suppose I want to generate all components of pages folder and my SSG functions are in Pages > movies.js component then will it work? will it generate all of my pages? or it will generate only when movies.js component renders?

  • @rajuahammad7554
    @rajuahammad7554 4 года назад +1

    Hi, I face this error prnt.sc/vwbto1. how can I solve it?

  • @randalvc
    @randalvc 4 года назад

    Once I have a million markdown files, and I added a new markdown file, does the compilation process include the million other markdown files again?

  • @andreaskarz
    @andreaskarz Год назад

    Very cool - thank you

  • @CrazyFanaticMan
    @CrazyFanaticMan 4 года назад

    which extension is he using to get his terminal to look like that? if anyone knows the answer, I'd really appreciate it

  • @myhendry
    @myhendry 4 года назад

    Hi Ben, now that Nextjs has static site generation, wouldn't that be the same as Gatsby? Nextjs does have an extra implementation in the form of dynamic server side rendering though. Personally, i find Gatsby easier to implement static sites with its in-built GraphQL and plugins

  • @gurkaransingh449
    @gurkaransingh449 4 года назад

    Ohk so my website (webapp) view is going to be somewhat different for everyuser (site can be modified and have plugins) so should i use SSR or CSR?

  • @apayrus
    @apayrus 4 года назад

    Thank you. So clear and helpful. I like and describe :)

  • @jpslr-86
    @jpslr-86 4 года назад

    Ben great content as always. ty! . Now, If a client comes to you for a website would you say next Ssg is read for production sites?

    • @bawad
      @bawad  4 года назад

      yeah

  • @zomakaja
    @zomakaja 4 года назад

    Excellent video, keep it up

  • @theuniverse685
    @theuniverse685 4 года назад

    Excellent! I was reading the Next.js docs, but I didn't understand a thing. What if I have an e-commerce, and this e-commerce has thousands of products, but I need to generate those products pages for SEO, which approach is good for it?

    • @bawad
      @bawad  4 года назад

      this approach would work, I think thousands is doable at build time

    • @theuniverse685
      @theuniverse685 4 года назад

      @@bawad So will you iterate into thousands products fetched from an api? This seems weird hahaha

  • @jacobmedure7038
    @jacobmedure7038 3 года назад

    import marked from "marked" not working for me

  • @Passion_Life
    @Passion_Life 4 года назад

    Great video! Could you talk more about how to deploy server-side-render Next JS app to Netlify? Thanks

    • @bawad
      @bawad  4 года назад +1

      you can't

  • @vladsvyrydonov1793
    @vladsvyrydonov1793 4 года назад

    like your Videos, next.js is very interesting, please more

  • @dev__adi
    @dev__adi 4 года назад

    great video, please make a part 2 of it showing how to use SSG with MDX

    • @bawad
      @bawad  4 года назад

      twitter.com/benawad/status/1242475931173883904

  • @murillomfs
    @murillomfs 4 года назад

    Nice content. I’m working on a blog with next.js right now and I’m having some problems to deploy it in other folders than the root folder of my FTP.
    You something that can help me?

  • @Sankaritarina89
    @Sankaritarina89 3 года назад

    quick question: if you would have like a hundred blog posts in .md form, and you would want to display the latest 5 posts on your /home... how would you know which ones are the latest? My idea would to add a "published" parameter to each .md and then when you map through the files in home's getStaticProps, just check that parameter. Is that a good idea? or how would you do it?

    • @erfer
      @erfer 3 года назад +1

      Maybe sort the items by created and just grab the first 5?

    • @Sankaritarina89
      @Sankaritarina89 3 года назад

      @@erfer yes, that was my idea too. It works as intended

    • @erfer
      @erfer 3 года назад +1

      @@Sankaritarina89 another way could be to write each file name to a separate text file and then just read the last/first 5 lines of the file - this way you don’t need any sorting

    • @Sankaritarina89
      @Sankaritarina89 3 года назад

      @@erfer that sounds good too. My situation is that I grabbed all the Blogposts from my wordpress and saved them as individual JSON files. They already have the publish date, so that makes it easy. I already got a working prototype for my blog with static next js last night. All good. Next js is amazing. I had not used the static feature before but it is amazing and so easy!

  • @yimingli224
    @yimingli224 4 года назад

    Why I can not see node_modules in your folder structure ?

    • @bawad
      @bawad  4 года назад +1

      because I hide it

  • @centereddev
    @centereddev 4 года назад

    Awesome! How would you go about deploying a site that has both SSR and SSG?

    • @bawad
      @bawad  4 года назад

      I would use Zeit Now also known as Vercel

    • @centereddev
      @centereddev 4 года назад

      @@bawad Thanks for the reply! Unfortunately, I'm not sure that's an option for me. We have to use AWS and/or Kubernetes at my company.

  • @Skia_
    @Skia_ 4 года назад

    my Lebanese homie is back at it again

  • @89lakaganth
    @89lakaganth 4 года назад

    Can we get more of Next.js please...

  • @fryxo
    @fryxo 4 года назад

    How to do it with a variable folder as [slug] then the folder containing [slug].js

    • @fryxo
      @fryxo 4 года назад

      Because I am not able to import fs in the [slug].js file

  • @unev
    @unev 4 года назад

    Hi, Ben. Any thoughts why react devtools are unable to parse my next.js project?

    • @bawad
      @bawad  4 года назад

      no idea

  • @kademmohammed6836
    @kademmohammed6836 3 года назад

    Thanks so much

  • @joseandkris
    @joseandkris 4 года назад

    Awesome video, just when I wondered how to do this... (RUclips knows to read minds I guess now)

  • @OBLIVIOUSKARI
    @OBLIVIOUSKARI 4 года назад

    Thanks! Do you know how to use Apollo client in this?

    • @bawad
      @bawad  4 года назад

      github.com/zeit/next.js/tree/canary/examples/with-apollo

  • @blasttrash
    @blasttrash 4 года назад

    what happened to vim or neovim? r u not using those? I see that u have vscode vim. I am new to vim and wanted to learn vim, but did u stop using it altogether? was there some problem with it? I am not sure if I should learn vim, hence the question

    • @bawad
      @bawad  4 года назад +1

      is vim worth learning?
      yes
      do I still like VSCode?
      yes, which is why I mix them

  • @maxiequa567
    @maxiequa567 4 года назад

    How can you use the front matter title to make cleaner links? using the filenames makes the-slugs-look-ugly. :)

    • @bawad
      @bawad  4 года назад +1

      slugs usually look like that, but you could make the slug whatever you like by parsing the front matter info in getStaticPaths

    • @maxiequa567
      @maxiequa567 4 года назад

      @@bawad Decided on this solution, might be of interest to others:
      // Function to convert string to Title Case
      const titleCase = (str) => str.replace(/\b\S/g, t => t.toUpperCase())
      // Wrap the slug in the function and remove the filename hyphens
      // JSX slugs.map change {slug} to:
      {titleCase(slug.replace('-', ' '))}
      Result: clean titles from dirty slugs. lol
      Feedback/criticism welcome :)

  • @georgewilkins5003
    @georgewilkins5003 4 года назад

    Ben goes full Texan 11:33

  • @kenwanjohi5128
    @kenwanjohi5128 4 года назад

    this is great

  • @marialopez1062
    @marialopez1062 4 года назад

    I have a couple questions about nextjs and if anyone with experience can answer them I would appreciated it.
    I know next js can be used for front end and back-end but what I don't understand is what's the benefits of using it for front end only with a django or php backend.

  • @hardikmodi8234
    @hardikmodi8234 4 года назад

    great video

  • @crazier192
    @crazier192 4 года назад

    Thanks for the introduction.
    Could you help answer why next.js has so many labor work? I mean you have to do all kind of stuff. In my perspective, next.js should provide all the things you've done in this video. Users just need to add more md files.

    • @bawad
      @bawad  4 года назад

      give gatsby a try if you want that

    • @crazier192
      @crazier192 4 года назад

      @@bawad I think you misunderstood my question. I don't say next.js is not good. I just want to know why next.js choose this way (labor work in my opinion), what is the benefit of doing it?

  • @jonmichalak7346
    @jonmichalak7346 4 года назад

    does this play nice with ZEIT's now/have you ever used now.sh??

    • @bawad
      @bawad  4 года назад +1

      yes and yes

    • @JonathanWilke
      @JonathanWilke 4 года назад +1

      Next.js and Zeit now always work perfectly together, because they are both built by the same team.

    • @jonmichalak7346
      @jonmichalak7346 4 года назад

      @@bawad thanks for the reply (many 'tubers don't). love the channel

  • @dilsmatchanov
    @dilsmatchanov 4 года назад

    Is it considered to be JAM stack?

    • @fr3fou
      @fr3fou 4 года назад +1

      yes

  • @jakespeakz
    @jakespeakz 4 года назад +1

    please do a firebase fetching data with firebase.database()

  • @bmasseybrannen
    @bmasseybrannen 4 года назад

    I am doing well today

  • @KozaKrisz
    @KozaKrisz 4 года назад

    What about MDX? :-)

    • @bawad
      @bawad  4 года назад +1

      gist.github.com/benawad/20c6c8f21dced3a31401c8eb750f9f62

  • @INKWEON7269
    @INKWEON7269 4 года назад +1

    Thanks~!! How to add image in .md file~?

  • @tenminutetokyo2643
    @tenminutetokyo2643 4 года назад

    Can you do one on Yarn and now to use it.

  • @CodewithFemi
    @CodewithFemi 4 года назад

    Here's how you deploy Next.js (React framework) on Azure Static Web App Service ruclips.net/video/LbUP8VZFfuU/видео.html

  • @b1chler
    @b1chler 4 года назад +1

    Did u try it with TypeScript and GraphQL? They added Generics in next version 9.3.1... github.com/zeit/next.js/issues/11033

    • @bawad
      @bawad  4 года назад

      I haven't yet

  • @angshu7589
    @angshu7589 4 года назад

    You writing JS is hurting my eyes. How are there no ".ts" files?

  • @dailymeow3283
    @dailymeow3283 2 года назад

    Anyone keep having this lame ad with boobie girl "So you wanna be a frontend engineer at google " 🤦‍♂️