Intro to Next.js + Server Side Rendering
Ketan Mayer-Patel
UNC Department of Computer Science
COMP 426: Modern Web Programming (Fall 2025)
Module 3: Next.js
Lecture 12
Announcements
Intro to Next.js + Server Side Rendering
COMP 426: Modern Web Programming (Fall '26)
M3: Next.js
L12
!
!
Lecture 11 Video
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
MPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
React Functionality
These JS scripts get loaded, starting up React and all other packages / code running.
Now, we are looking at how an MPA loads initially.
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
about�html
Notice that MPAs have separate html files for each page!
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
React Functionality
React renders, updating the DOM.
DOM change updates the browser view.
Hello, world!
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
about�html
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
React Functionality
Hello, world!
At this point, it appears that the site has loaded successfully.
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
about�html
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
React Functionality
Hello, world!
At this point, it appears that the site has loaded successfully.
Now, let’s see what happens when the user tries to navigate to the about page.
about�html
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
about�html
index�html
React Functionality
Hello, world!
Client requests the website’s about page.
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
about�html
index�html
React Functionality
Hello, world!
Client requests the website’s about page.
The server responds with about.html .
Existing React setup on the client and all files here get overwritten. DOM erased.
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
about�html
about�html
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
about�html
about�html
React Functionality
All CSS and JS gets loaded again. ��A new React instance spins up again (all local state gone)��The user sees a flash of an empty page before the page reloads again, which takes time.
MPA Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
about�html
about�html
React Functionality
All CSS and JS gets loaded again. ��A new React instance spins up again (all local state gone)��The user sees a flash of an empty page before the page reloads again, which takes time.
About�page!
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
The client can request the website’s home page.
localhost:3000/
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
The client can request the website’s home page.
The server responds with index.html .
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
The index.html file, usually mostly empty in a React app, populates the DOM with a root element.
DOM change updates the browser view.
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
The index.html file now requests the CSS and JS files from its <head> metadata.
The index.html file, usually mostly empty in a React app, populates the DOM with a root element.
DOM change updates the browser view.
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
The index.html file now requests the CSS and JS files from its <head> metadata.
The index.html file, usually mostly empty in a React app, populates the DOM with a root element.
DOM change updates the browser view.
The server responds with css/js .
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
React
These JS scripts get loaded, starting up React and all other packages / code running.
Router
localhost:3000/
But this time, there is a router.
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
React
Router
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
React renders, updating the DOM.
DOM change updates the browser view.
Hello, world!
localhost:3000/
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
Hello, world!
At this point, it appears that the site has loaded successfully.
React
Router
localhost:3000/
But, what if we try to navigate to the about page?
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
Hello, world!
React
Router
localhost:3000/
User presses a link to navigate them to the about page
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
Hello, world!
React
Router
localhost:3000/
The router instructs React to render the about page.
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
React
Router
localhost:3000/
React clears the page…
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
About page
React
Router
localhost:3000/
… and replaces it with the about page.
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
About page
React
Router
localhost:3000/about
The router then updates the route to look like a new page loaded.
SPA Example
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Client
Dev Server
Browser Screen
DOM
index�html
main�css
main�css
main�css
main�js
index�html
index�html
main�css
main�css
main�css
main�js
About page
React
Router
localhost:3000/about
The router then updates the route to look like a new page loaded.
In this model, the client never asked the server for the new page.��The same React instance is running.��State is preserved.��The router handles page changes, and renders using the JS loaded initially because all components are part of the JS bundle.
App Architecture
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Tradeoffs Review
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Intro to Next.js
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Intro to Next.js
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Building a Next Project
Client
Dev Server
Browser Screen
DOM
Step 1: We develop our Next project in VSCode. This includes HTML, TSX, etc.
M3: Next.js
L12
Our Next Project
</>
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Building a Next Project
Client
Dev Server
Browser Screen
DOM
Our Next Project
Step 1: We develop our Next project in VSCode. This includes HTML, TSX, etc.
Step 2: We either deploy to Vercel or run locally, which causes our project to build.
</>
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Building a Next Project
Client
Dev Server
Browser Screen
DOM
Step 1: We develop our Next project in VSCode. This includes HTML, TSX, etc.
Step 2: We either deploy to Vercel or run locally, which causes our project to build.
</>
.next
This build step creates a folder called .next containing both our original bundle and server code.
M3: Next.js
L12
Our Next Project
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Building a Next Project
Client
Dev Server
Browser Screen
DOM
Our Next Project
Step 1: We develop our Next project in VSCode. This includes HTML, TSX, etc.
Step 2: We either deploy to Vercel or run locally, which causes our project to build.
</>
.next
This build step creates a folder called .next containing both our original bundle and server code.
.next
...
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Building a Next Project
Client
Dev Server
Browser Screen
DOM
Our Next Project
Step 1: We develop our Next project in VSCode. This includes HTML, TSX, etc.
Step 2: We either deploy to Vercel or run locally, which causes our project to build.
</>
.next
This build step creates a folder called .next containing both our original bundle and server code.
.next
...
Next Server Functionality
Next.js server functionality starts running on the server.
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
Now, what if the client requests the home page?
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
The client requests the website’s home page.
localhost:3000/
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
The client requests the website’s home page.
localhost:3000/
(! IMPORTANT !)��Next.js tries to generate the HTML file for this page (using React) on the server first.
html
This HTML file should look like the page - it is not just a blank shell!
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
The client requests the website’s home page.
localhost:3000/
(! IMPORTANT !)��Next.js tries to generate the HTML file for this page (using React) on the server first.
html
This HTML file should look like the page - it is not just a blank shell!
The client can request the website’s home page.
The server responds with this html .
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
The client requests the website’s home page.
localhost:3000/
(! IMPORTANT !)��Next.js tries to generate the HTML file for this page (using React) on the server first.
html
This HTML file should look like the page - it is not just a blank shell!
The client can request the website’s home page.
The server responds with this html .
html
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
localhost:3000/
html
This HTML file should look like the page - it is not just a blank shell!
html
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
This leaves us with…
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
localhost:3000/
html
html
Home�Page!
Since this HTML file should look like the page - the DOM gets updated and immediately shows the right page.
With this approach, the initial HTML is generated on the server, not the client.��This is called Server-Side Rendering (SSR)
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
localhost:3000/
html
html
Home�Page!
Attendance Check!
!
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
localhost:3000/
html
html
Home�Page!
Attendance Check!
!
Answer: No.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Adding Interactivity
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
html
Home�Page!
The index.html file now requests JS files from its <head> metadata.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Adding Interactivity
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
html
Home�Page!
The index.html file now requests JS files from its <head> metadata.
The server responds with css/js .
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Adding Interactivity
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
html
Home�Page!
These JS scripts get loaded, starting up React / Next and all other packages / code running.
Router
React, etc
Next App
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Adding Interactivity
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
html
Home�Page!
Router
React, etc
Next App
Next then will reconcile the existing page and sprinkle in functionality / JS.
Page becomes interactive!
This process is known as hydration.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Adding Interactivity
Client
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
html
Home�Page!
Router
React, etc
Next App
From this point on, the Next app is running on the client.��So, from here on, we can treat the app like an SPA.�(Use the Router to switch pages!)
SSR primarily just affects the first load (when we enter the app). *
Standard SPA vs. Next (SSR) for SEO
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
In this example, assume the crawler is trying to�“index” (read) the about page.
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
Crawler requests the about page
/about
Crawler requests the about page
/about
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
Crawler requests the about page
/about
Crawler requests the about page
/about
html
The server generates non-empty HTML.
The server immediately with index.html .
Mostly empty b/c it’s the client’s job to render the page in later!
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
html
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
html
The index.html file, usually empty in a React app, populates the DOM with a root element.
Page remains blank
About�Page!
Since this HTML file should look like the page - the DOM gets updated and immediately shows the right page.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
About�Page!
html
The html files now request JS files from its <head> metadata.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
About�Page!
html
The server responds with JS files.
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
About�Page!
html
js
js
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
But, as we said last lecture, crawlers typically won’t run this JavaScript!
So, the loading ends and the crawler works with what it sees.
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
About�Page!
html
js
js
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
The loading ends and the crawler works with what it sees.
Blank page with an SPA
�Pre-rendered page with Next.js
Yes, the page from Next is not interactive, but it usually contains enough content for the crawler to index this page well.
Traditional SPA
Next.js (SSR)
Search Engine Crawler
Dev Server
Browser Screen
DOM
.next
...
Next Server Functionality
M3: Next.js
L12
Search Engine Crawler
Browser Screen
DOM
Dev Server
index�html
main�css
main�css
main�css
main�js
index�html
html
About�Page!
html
js
js
Big Idea
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Setting Up a Next.js App
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
App Router
COMP 426: Modern Web Programming (Fall '26)
Intro to Next.js + Server Side Rendering
M3: Next.js
L12
Routing Files
Layouts and Nested Routes
Client-side Components
Dynamic Routes