NEXT.JS COMPLETE GUIDE
Make a Next.js Application
$ npx create-next-app color-pages
$ cd color-pages
$ npm run dev
mkdir data
touch data/colors.json
/ data/colors.json
[
{ "name": "Illuminating", "hex": "#F5DF4D" },
{ "name": "Classic Blue", "hex": "#0f4c81" },
{ "name": "Living Coral", "hex": "#FA7268" },
{ "name": "Ultra Violet", "hex": "#5f4b8b" },
{ "name": "Greenery", "hex": "#88b04b" },
{ "name": "Rose Quartz", "hex": "#F7CAC9" },
{ "name": "Marsala", "hex": "#B57170" },
{ "name": "Radiant Orchid", "hex": "#b067a1" }
]
Directing
$ touch pages/[color].js
getStaticPaths
// [color].js
// import the colors array
import colors from '../data/colors.json'
export async function getStaticPaths() {
// loop through the colors array
const paths = colors.map(color => ({
// return an object with params.color set to the color's name
params: { color: color.name }
}))
// Paths will look like this:
// [
// { params: { color: 'Marsala' } },
// { params: { color: 'Illuminating'} }
// ...
// ]
return { paths, fallback: false }
}
getStaticProps
// [color].js
export async function getStaticProps({ params }) {
// find the info for just one color
const color = colors.find(color => color.name === params.color)
// return it in the necessary format.
return { props: { color } }
}
Format the Page
// [color.js]
export default function Color({ color }) {
return <div className='color-page' style={{ backgroundColor: color.hex }}>
<h1>{color.name}</h1>
</div>
}
/* global.css */
html,
body, #__next, .color-page {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
top: 0px;
position: absolute;
display: block;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
.color-page {
display: flex;
justify-content: center;
align-items: center;
}
Interface Part
Presently, we should simply utilize the Connection part to connection to each variety's page from the landing page. We'll refresh index.js with the rundown of varieties.We'll involve two Next.js explicit parts in this Home part - - Connection and Head. <Link> permits you to do client-side course advances, which will make page changes smoother for clients. We'll involve it instead of and generally very much like a tag.
The <Head> part permits us to embed information into the html head tag from inside the part. We'll refresh the page title and meta labels from that point!
// index.js
import Head from 'next/head'
import Link from 'next/link'
import colors from '../data/colors.json'
export default function Home() {
return (
<div>
<Head>
<title>Colors!</title>
<meta name="description" content="App that displays pretty colors to learn Next!" />
</Head>
{colors.map(color => (
<Link href={`/${color.name}`}>
<h2>{color.name}</h2>
</Link>
))}
</div>
)
}
SSR versus SSG
We just constructed a statically created Next.js application - - this means information is just brought at fabricate time. In the event that our varieties were coming from a Programming interface and we had our site constructed and sent, our application wouldn't refresh with any Programming interface changes (say the 2022 shade of the year was added). For some applications this is absolutely fine! A blog doesn't have to refresh again and again with content.
SSG (static site age) permits Next.js to create HTML for each page when the site is being fabricated. Those pages can then be reserved by a CDN and lead to a super performant site.
That being said, some of the time you really want a site that refreshes powerfully, and that is where server-side delivering comes in. SSR (server-side-delivering) permits you to in any case deliver HTML on the server-side yet do that for each solicitation made by a client to the page rather than at construct time.
To utilize SSR rather than SSG, we would supplant our getStaticProps and getStaticPaths with just getServerSideProps. Note that the beneath model won't work since we didn't really make a Programming interface!
export async function getServerSideProps({ params }) {
// Make a request to get data about the color via our API
const res = await fetch(`http://www.color-api.com/${params.color}`)
const color = await fetch.json()
// return the data as props that will be passed to the Color component
return { props: { color } }
}
To peruse more about SSR versus SSG, I have a full blog entry about the distinction!
Sending
Since you have a Next.js application composed, you really want to get it live on the web. AWS Intensify upholds sending both SSR and SSG Next.js applications with next to no extra arrangement on your end.
In the event that you're making a statically created Next.js application, go to your package.json document and change your fabricate content to next form && next trade. In the event that you're rather making a server-side delivered application, you don't have to change a thing! The contents Next.js created for you will be what you really want.
"scripts": {
"dev": "next dev",
+ "build": "next build && next export",
"start": "next start"
},
Then, make a store on your git provider of choice, and push your code to it.
Make an AWS account if you don't at this point have one.
Investigate to the Heighten Control focus
Click on the orange partner application button.
Pick GitHub in the From your ongoing code menu, and snap continue
Amazing Sir...
ReplyDelete