What is Nextjs
Next.js is a full-stack SSR framework based on React, providing a series of tools for automatic configuration of React,
allowing us to focus on building applications and simplify the configuration process.
What is SSR(Server Side Render)
SPA (Single Page App)
When it comes to server-side rendering, we have to mention SPA (Single Page Applications). Examples of single page
applications include React and Vue, which operate by downloading JavaScript to the client side for execution.
They undergo compilation and building processes to create JavaScript virtual DOM objects, which are then mounted onto
index.html all at once. Since they are mounted on a single HTML file, for instance:


Other modifications are based on it, so it is a single-page application.
Moreover, SPA programs place the JavaScript files for rendering and logical processing on the client side, where the
rendering tasks are handled by the client, hence they are also known as CSR (Client-Side Rendering) programs.
On the other hand, server-side rendering involves the server rendering the HTML file and then delivering it to the
client, returning the corresponding HTML file based on different requests.
Comparison
- Performance
CSR assigns the rendering task to the client, which can lead to lag on devices with poor performance, but it
relatively reduces the server’s load. In contrast, SSR faces significant stress during peak request times due to the
need to handle a large volume of requests, but it alleviates pressure on the client side. - SEO-friendliness
Before rendering, it is difficult for CSR to obtain specific information about the webpage, making it not
SEO-friendly. However, SSR can provide the rendered files to search engine crawlers, at which point the page content
is essentially complete, facilitating analysis and recognition by the engine algorithms. - Initial load time
CSR requires downloading the JS to the client for rendering, so there might be a period of blankness on the page (an
issue with initial screen rendering) before the JS is downloaded to the client.
On the other hand, SSR renders the HTML on the server first, eliminating this step and reducing client-side burden,
thus it can display content to users more quickly.
Setup Nextjs Project
git repository
The teaching content of this article will be placed in the GitHub repository, which can be studied in conjunction.
Different branches will correspond to different chapters.
init project
1 | npx create-next-app@latest next-course |

Alternatively, you can use my open-source project to create.
Function 1: File Routing System
In version 13, Next.js introduced a new App Router built on React Server Components, which supports shared layouts,
nested routes, loading states, error handling, and more.
Next.js automatically recognizes files in the app
directory with the extension .page.jsx
or .page.tsx
to create
pages, while their parent or sibling files called layout.jsx
or layout.tsx
are used as page layouts for rendering.
try it
Assuming we want to create a blog website and the access path for the blog is site.example/blog/test/,
let’s create the directory file structure accordingly:
1 | - app |
1 | // app/blog/test/page.tsx |
Add a link to it in app/page.tsx.
1 | // app/page.tsx |
start it:
1 | npm run dev |

Modify the content in globals.css to make the background look nicer.
1 | :root { |
result:


Ok, You completed the challenge!
Route mapped to URL
From the example we just looked at, we can see that the mapping relationship from file to route is as follows:
file://site.dir/app/a/b/page.jsx -> site.example/a/bDynamic Route
You might have noticed that the current URLs are hardcoded.
If we need 10 different blog pages (such as blog/test2
, blog/abc
, blog/start
), using this method would require
manually creating 10 different xxx/page.js/jsx/ts/tsx
files, which is very time-consuming and inefficient.
Of course, Next.js provides a solution called dynamic routes.
The file route mapping for it looks like this:
file://site.dir/app/blog/[slug]/page.jsx -> site.example/blog/a, site.example/blog/b, site.example/blog/ctry it
Modify our directory structure.
1 | - app |
Create a blog content page.
1 | // app/blog/[slug]/page.tsx |
![Visiting `blog/a` reached our `[slug]/page`.](https://raw.githubusercontent.com/malred/malcode-site-photos/master/img/20240825210024.png)

layout
Next.js provides a layout file that offers layout functionality, which is UI shared among multiple routes. When
navigating, the layout retains its state, remains interactive, and does not re-render. Layouts can also be nested.
Let’s analyze the layout file:
1 | import type {Metadata} from "next"; |
Let’s add a navbar and a footbar.
1 | // components/footbar.tsx |
Have you not learned Tailwind CSS yet? Check out this article (todo).
add them to layout.tsx
1 | // app/layout.tsx |
Result:

provide blog contents
OK, you now have dynamic routes that can match different names, as well as having created a navbar and footbar. However,
we want the page to display more content.
write markdown
Many note-taking applications now support Markdown syntax, which is a method I particularly favor. Additionally, there
are numerous npm libraries available that can parse the contents of Markdown files into HTML rich text strings.
learn markdown Syntax?
let us change directory structure
1 | - app |
mountain.md
1 |  |
flower.md
1 |  |
bird.md
1 |  |
read and show
Install the dependencies required to parse Markdown.
1 | npm i marked |
Read and parse files in the code.
1 | // app/blog/[slug]/page.tsx |
Great, we have now successfully parsed and rendered the Markdown text onto the page!
Result:

Address the issue with text styling.
You might have noticed that our heading styles are the same as the regular text. This is because Tailwind CSS removes
default CSS styles, but we can use a library to address this issue.
1 | npm i -save-dev @tailwindcss/typography |
Register as a Tailwind plugin.
1 | // tailwind.config.ts |
Then use it to add the ‘prose’ class name to components.
1 | // app/blog/[slug]/page.tsx |
Restart the server and access the page again.

Function 2: Provide API interfaces.
Next.js will parse the files in the api directory as backend interfaces, receiving and processing HTTP requests.
We will move the operation of reading and parsing Markdown files to the backend (api directory), while keeping the
rendering work on the frontend (app directory).
try it
Change our directory structure.
1 | - app |
The current correspondence between the file system and URLs is as follows:
1 | app/api/blogs/[slug]/route.ts -> site.example.com/api/blogs/a, site.example.com/api/blogs/b, ... |
Write code to handle requests.
1 | // api/blogs/route.ts |
Result:


Request backend data from the front end.
1 | // app/blog/[slug]/page.tsx |
Result:


At this point, we will pause here. To prevent the article from becoming too lengthy, I have placed the remaining content
in separate sections, which you can click below to continue reading.
Next Step
Use next-themes for day and night theme switching.
Have you seen websites that can switch between light and dark themes? We can also achieve this.
Connect to the database using Prisma.
Storing data in a file system or cache is one option, but using a database to store data is a more common choice.
Use Next-Auth for authentication.
Authentication is a common feature in applications. Using Next-Auth eliminates the hassle of creating your own login and
registration pages, allowing you to focus on implementing authentication logic.
Use TipTap as a rich text editor.
TipTap is a modern headless rich text editor that allows us to easily create attractive page content.
How to customize TipTap through code? Check out my latest article (todo).