Migrating Autowini's listing detail page from JSP to Next.js, and the CDN problem I hit while adding SSR
I recently migrated the Autowini Web listing detail page, which was still on JSP, to Next.js.
I'll go over why we migrated, how it went, and the CDN problem I ran into while adding SSR.
Background of the Next.js migration
Autowini is a project with many legacy pages built with JSP in its early days.
So we had been migrating pages to React one at a time, including filter search, unified search, My Wini, PartWini filter search and unified search, plus other static pages, and the React build output was uploaded to the existing IDC servers and served without a frontend server of its own.
To improve this, we gave the frontend its own server and are now migrating to Next.js step by step.
Here are the benefits we expected from moving to Next.js.
- Faster responses
- Prefetching with the
<Link />component - SEO through SSR
- Static pages through SSG
- Image optimization with the
<Image />component - Automatic per-page code splitting
We decided to migrate to get these and the other benefits Next.js offers.
First, a senior engineer on our team moved the existing React pages to Next.js.
After that, I went through the whole migrated project, set up a standard folder structure for the App Router, and refactored it by designing and building shared functions and components.
Next, I took on migrating the listing detail page, a high-priority page among those still on JSP.
How the migration went
I kept the existing public URLs and had the proxy in front of the servers send only requests for detail URLs to Next.js. JSP still handles the other pages as before.
I started with the car detail page and extended it to trucks, buses and heavy equipment, with all four types sharing one detail component.
What I cared about most in this migration was the SSR approach, rendering the content on the server and sending it down with SEO in mind.
However, under an internal policy, what the detail page shows depends on the visitor's country, so to use SSR, the server also had to know where the visitor was connecting from.
The API server determines the country from the IP on the request. At first I thought that if the Next.js server called the API, the country would be judged from the server's IP, so I had the browser fetch the content.
But that left only a skeleton in the first HTML, which was actually worse for SEO than the old JSP page that sent the content inside the HTML.
To fix this, I looked again and found that the IP the API uses to determine the country is read from the request headers.
So I changed the Next.js server to pass the visitor's IP and language headers along when it calls the API. That way, the API judges the visitor's country even when the server is the one calling.
import { headers } from "next/headers";
export async function getProduct(id: string) {
const incoming = await headers(); // Headers of the visitor's request
const res = await fetch(`https://example.com/api/products/${id}`, {
// Pass the IP and language headers used to determine the country to the API as is
headers: {
"x-forwarded-for": incoming.get("x-forwarded-for") ?? "",
"accept-language": incoming.get("accept-language") ?? "",
},
// The result differs per visitor, so don't cache it
cache: "no-store",
});
return res.json();
}The problem I ran into
In the dev environment, forwarding the headers gave the right country, but in production, every visitor was judged as connecting from Korea.
When I actually sent requests with different IPs, dev judged 8.8.8.8 as the US and 1.1.1.1 as Australia, while production judged both as Korea.
What made it harder to find was that the problem didn't show on screen.
When the country is judged wrong, the server can't render the content and sends HTML with only a skeleton, but the browser then fetches the content again and fills it in, so the page looked normal.
In the end, the page looked the same, but the first HTML Googlebot received had no content, which defeated the purpose of adding SSR.
Why only production judged everyone as Korean
The production API goes through a CDN, and the CDN refills the header that carries the visitor IP with the IP of whoever connected to it.
On the existing pages, the browser called the API directly, so whoever connected to the CDN was the visitor, and JSP determined the country directly from the request its server received.
But with SSR, the Next.js server started calling the API on the visitor's behalf for the first time. In that case, the one connecting to the CDN is the Next.js server, not the visitor.
So even though the Next.js server put the visitor IP in the header, the CDN overwrote it with the Next.js server's IP. Since the server is in Korea, every request was judged as connecting from Korea. CDN behavior that had never caused trouble before ended up erasing the visitor IP under SSR.
The dev API address doesn't go through the CDN, so the problem never showed up in dev.
Fixing it with a Docker setting
The first idea was to change the CDN settings. But considering our internal policy, I left the CDN settings alone and added a Docker compose setting so that only requests from the Next.js server skip the CDN.
# compose.yaml
services:
web:
image: example/web:latest
extra_hosts:
# Send the API domain to the host, only inside this container
- "api.example.com:host-gateway"extra_hosts adds a line to /etc/hosts inside the container. With host-gateway, when this container calls the API domain, the request goes straight to the server running the container instead of the CDN.
The proxy on that server passes requests for the same domain on to the API, so I could keep the existing address and certificate without changing any code.
API calls from the browser still go through the CDN as before, so other pages are not affected.
Since applying this, the visitor's country is judged correctly, and the first HTML that Googlebot receives includes the listing photos and specs as well.
Looking back
While fixing this CDN problem, I learned for the first time how to make only the SSR calls skip the CDN with a Docker extra_hosts setting, and it was interesting to find out it could be handled this way.
Moving the listing detail page to Next.js added more to watch on the server side, like this.
But we can now use rendering strategies such as SSR and SSG that React alone couldn't fully cover.
I'm glad that we can keep optimizing performance with a rendering strategy that fits each page and take the service in a better direction.
- #Autowini
- #Next.js
- #JSP
- #SSR
- #CDN
- #Docker