{"id":3084,"date":"2021-09-01T09:29:59","date_gmt":"2021-09-01T09:29:59","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3084"},"modified":"2023-02-28T00:50:41","modified_gmt":"2023-02-28T00:50:41","slug":"js-tutorial-netlify-next-js","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/js-tutorial-netlify-next-js\/","title":{"rendered":"JS Tutorial &#8211; Netlify &#038; Next.js"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; Cassidy Williams has been doing a Blogvent (blogging every day for a month) over\u00a0<a href=\"https:\/\/www.netlify.com\/authors\/cassidy-williams\/\" rel=\"noopener\">on the Netlify Blog<\/a>. A lot of the blog posts are about\u00a0<a href=\"https:\/\/nextjs.org\/\" rel=\"noopener\">Next.js<\/a>. There is a lot to like about Next.js. I just pulled one of Cassidy\u2019s\u00a0<a href=\"https:\/\/github.com\/cassidoo\/next-netlify-starter\" rel=\"noopener\">starters<\/a>\u00a0for fun. It\u2019s very nice that it has\u00a0<a href=\"https:\/\/www.netlify.com\/blog\/2020\/12\/03\/what-is-react-fast-refresh\/\" rel=\"noopener\">React Fast-Refresh built-in<\/a>. I like how on any given \u201cPage\u201d you can import and use a\u00a0<code>&lt;Head&gt;<\/code>\u00a0to control stuff that would be in a\u00a0<code>&lt;head&gt;<\/code>. This was my first tiny little play with Next so, excuse my basicness.<\/p>\n<p>But the most compelling thing about Next.js, to me, is how easily it supports the\u00a0entire rendering spectrum. It encourages you to do\u00a0<strong>static-file rendering<\/strong>\u00a0by default (smart), then if you need to do\u00a0<strong>server-side rendering<\/strong>\u00a0(SSR), you just update any given Page component to have this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">export async function getServerSideProps() {\r\n  \/\/ Fetch data from external API\r\n  const res = await fetch(`https:\/\/...\/data`)\r\n  const data = await res.json()\r\n\r\n  \/\/ Pass data to the page via props\r\n  return { props: { data } }\r\n}<\/pre>\n<p>The assumption is that you\u2019re doing SSR because you need to hit a server for data in order to render the page, but would prefer to do that server-side so that the page can render quickly and without JavaScript if needed (great for SEO). That assumes a Node server is sitting there ready to do that work. On Netlify, that means a function (Node Lambda), but you barely even have to think about it, because you just\u00a0<a href=\"https:\/\/www.netlify.com\/blog\/2020\/12\/07\/announcing-one-click-install-next.js-build-plugin-on-netlify\/\" rel=\"noopener\">put this<\/a>\u00a0in your\u00a0<code>netlify.toml<\/code>\u00a0file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">[[plugins]]\r\n  package = \"@netlify\/plugin-nextjs\"<\/pre>\n<p>Now you\u2019ve got static where you need it, server-rendered where you need it, but you aren\u2019t giving up on client-side rendering either, which is nice and fast after the site is all booted up. I think it shoots some JSON around or something, framework magic.<\/p>\n<p>I set up a quick SSR route off my homepage to have a play, and I can clearly see that both my homepage (static) and\u00a0<code>\/cool<\/code>\u00a0route (SSR) both return static HTML on load.<\/p>\n<figure id=\"attachment_3085\" aria-describedby=\"caption-attachment-3085\" style=\"width: 1000px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3085 size-full\" src=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2020-12-14-at-2.18.49-PM.webp\" alt=\"I even had to prettify this source, as you HTML minification out of the box\" width=\"1000\" height=\"903\" srcset=\"https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2020-12-14-at-2.18.49-PM.webp 1000w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2020-12-14-at-2.18.49-PM-300x271.webp 300w, https:\/\/rengga.dev\/blog\/wp-content\/uploads\/2022\/08\/Screen-Shot-2020-12-14-at-2.18.49-PM-768x694.webp 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption id=\"caption-attachment-3085\" class=\"wp-caption-text\">I even had to prettify this source, as you HTML minification out of the box<\/figcaption><\/figure>\n<p>I admit I like working in React, and Next.js is a darn nice framework to do it with because of the balance of simplicity and power. It\u2019s great it runs on Netlify so easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; Cassidy Williams has been doing a Blogvent (blogging every <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/js-tutorial-netlify-next-js\/\" title=\"JS Tutorial &#8211; Netlify &#038; Next.js\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3802,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[264,263,285,286,103,227],"newstopic":[],"class_list":{"0":"post-3084","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"tag-javascript-tutorial","9":"tag-js-tutorial","10":"tag-netlify","11":"tag-next-js","12":"tag-web-design","13":"tag-web-designer"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3084"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3084\/revisions"}],"predecessor-version":[{"id":3087,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3084\/revisions\/3087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3802"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3084"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}