Nextjs Server Side
Nextjs Server Side
com/pdf/nextjs-2/next-2-1
Step 1 - Backends in
Next.js
Next.js is a full stack framework
Why?
1 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
Code - https://github.com/100xdevs-cohort-2/week-14-2.1
Website - https://week-14-2-1.vercel.app/
2 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
UserCard component
3 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
You can do the same thing as the last slide in Next.js, but then you
lose the benefits of server side rendering
You should fetch the user details on the server side and pre-render the
page before returning it to the user.
4 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
npx create-next-app@latest
1. Install axios
npm i axios
return (
<div>
{userData.email}
{userData.name}
</div>
);
}
5 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
1. Prettify the UI
6 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
return (
<div className="flex flex-col justify-center h-screen">
<div className="flex justify-center">
<div className="border p-8 rounded">
<div>
Name: {userData?.name}
</div>
{userData?.email}
</div>
</div>
</div>
);
}
What if the getUserDetails call takes 5s to finish (lets say the backend is
slow). You should show the user a loader during this time
loading.tsx file
Just like page.tsx and layout.tsx , you can define a skeleton.tsx file that will
render until all the async operations finish
8 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
9 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
10 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
This isn’t the best way to fetch data from the backend. We’ll make
this better as time goes by
11 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
1. Create components/Signup.tsx
▾ Code
12 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
interface LabelledInputType {
label: string;
placeholder: string;
type?: string;
onChange: ChangeEventHandler<HTMLInputElement>
}
"use client"
13 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
▾ Final signup.tsx
14 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
interface LabelledInputType {
label: string;
placeholder: string;
type?: string;
onChange: ChangeEventHandler<HTMLInputElement>
}
1. Navigate to app/api/user/route.ts
15 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
Ref - https://nextjs.org/docs/app/api-reference/functions/next-
response
Step 9 - Databases!
We have a bunch of dummy routes, we need to add a database layer to
persist data.
16 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
1. Install prisma
model User {
id Int @id @default(autoincrement())
username String @unique
password String
}
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schem
17 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
console.log(user.id);
We’re not doing any authentication yet. Which is why we’re not
returning a jwt (or setting a cookie) here
18 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
For the root page, we are fetching the details of the user by hitting an
HTTP endpoint in getUserDetails
Current solution
import axios from "axios";
return (
<div className="flex flex-col justify-center h-screen">
<div className="flex justify-center">
<div className="border p-8 rounded">
<div>
Name: {userData?.name}
</div>
{userData?.email}
</div>
</div>
</div>
);
}
19 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
Better solution
import { PrismaClient } from "@prisma/client";
return (
<div className="flex flex-col justify-center h-screen">
<div className="flex justify-center">
<div className="border p-8 rounded">
<div>
Name: {userData?.name}
20 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
</div>
{userData?.email}
</div>
</div>
</div>
);
}
1. Create db/index.ts
declare global {
var prisma: undefined | ReturnType<typeof prismaClientSingleton>
}
21 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
Right now, we wrote an API endpoint that let’s the user sign up
console.log(user.id);
22 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
Under the hood, still an HTTP request would go out. But you would
feel like you’re making a function call
Steps to follow
1. Create actions/user.ts file (you can create it in a different folder)
"use server"
console.log(user.id);
...
23 of 24 10/9/24, 18:27
Projects | 100xDevs https://projects.100xdevs.com/pdf/nextjs-2/next-2-1
localStorage.setItem("token", response);
router.push("/")
}} type="button" className="mt-8 w-full text-white bg-gray-800 focus:ring-4 focus:rin
2. Gives you types of the function response on the frontend (very similar
to trpc)
24 of 24 10/9/24, 18:27