𝗛𝗼𝘄 𝗪𝗲 𝗖𝘂𝘁 𝗦𝗹𝗼𝘄 𝗥𝗲𝘀𝗽𝗼𝗻𝘀𝗲𝘀 𝗯𝘆 𝟴𝟬%
Subito is a top marketplace in Italy. We handle thousands of requests every minute. Our ad detail page is vital for SEO and revenue.
We moved this page from Next.js Pages Router to App Router. We did not stop product work to do this. We moved in small steps.
Here is how we did it.
𝗧𝗵𝗲 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵
We used an incremental strategy. We added the App Router tree alongside the old Pages Router. This let us ship new features while we migrated.
We used Server Components to own data fetching. This removed the need to pass data through many layers. We also created a Data Access Layer. This made sure we only fetch data once per request.
We used Suspense and the use() hook to enable HTML streaming. This shows a skeleton screen while data loads. The user sees the page faster.
𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲𝘀
Problem 1: HTTP 410 Gone Search engines need a 410 status when a page is permanently removed. The App Router does not have a built-in way to send this. We solved this using our Express server. The server intercepts the response and changes the status code.
Problem 2: HTML Streaming We found that our loading skeletons did not appear. The page stayed blank for seconds.
We discovered that Nginx and Akamai were buffering our responses. If the CDN buffers the HTML, streaming fails. We had to turn off buffering in Nginx and apply custom settings in Akamai. Once we fixed this, content streamed to the user perfectly.
𝗧𝗵𝗲 𝗥𝗼𝗹𝗹𝗼𝘂𝘁
We rolled out in two phases to protect our SEO.
Phase 1: We moved traffic to the App Router but kept streaming off. We did this one category at a time.
Phase 2: We enabled HTML streaming. We tested this on small categories first. Our SEO team monitored rankings for two weeks before we moved to the next category.
𝗧𝗵𝗲 𝗥𝗲𝘀𝘂𝗹𝘁𝘀
The results were huge. Before the migration, up to 40% of our responses were slow. After the migration, slow responses dropped by 80%.
Most pages now load in under 500ms.
𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀
- Use parallel routing to migrate without stopping feature work.
- A custom server layer helps you control traffic and fix framework gaps.
- Streaming requires configuration at every layer. Check Nginx and your CDN.
- Test your SEO metrics during the rollout.
Jinsi tulivyopunguza majibu ya polepole kwa 80% kwa kuhama kwenda Next.js App Router
Katika makala hii, tutazungumzia jinsi tulivyohamia programu yetu kutoka Next.js Pages Router kwenda App Router na jinsi ilivyotusaidia kupunguza majibu ya polepole kwa 80%.
Tatizo
Programu yetu ilikuwa ikikabiliwa na muda mrefu wa kutoa majibu. Upataji mwingi wa data ulikuwa ukifanyika upande wa mteja (client-side), jambo ambalo lilisababisha changamoto kadhaa:
- Waterfalls: Maombi mengi ya data yanayofanywa mfululizo (sequentially), ambapo ombi moja linasubiri lingine liishe kabla ya kuanza.
- Ukubwa wa Bundle (Bundle Sizes): JavaScript nyingi sana zinazotumwa kwa mteja, jambo linalochelewesha upakiaji.
- Layout Shifts: Maudhui yanayobadilika nafasi ghafla wakati data inapakia, jambo linalochafua uzoefu wa mtumiaji.
Suluhisho: Next.js App Router
App Router inaleta React Server Components (RSC), ambazo zinaturuhusu kupata data moja kwa moja upande wa seva.
1. Server Components
Kutumia Server Components, tunaweza kupata data moja kwa moja kwenye component bila kuhitaji useEffect au useState kwa ajili ya upakiaji wa awali wa data. Hii inamaanisha kuwa upataji wa data unajumuishwa katika mchakato wa kutengeneza ukurasa kwenye seva.
async function Page() {
const data = await fetchData();
return <div>{data.name}</div>;
}
2. Streaming na Suspense
Badala ya kusubiri ukurasa mzima ukamilike, tulitumia loading.js na Suspense ili kutoa muonekano (UI) wa haraka (kama vile skeleton screens) wakati data muhimu inapakuliwa. Hii inaboresha muda wa kwanza wa kuona maudhui (First Contentful Paint).
Matokeo
Baada ya uhamisho huu, tuliona maboresho makubwa katika utendaji wetu:
| Kipimo | Kabla ya Uhamisho | Baada ya Uhamisho |
|---|---|---|
| Majibu ya Polepole (Slow Responses) | Juu | -80% |
| Ukubwa wa Bundle (Client-side) | Mkubwa | Mdogo sana |
| TTFB (Time to First Byte) | Polepole | Haraka |
Baada ya kuhama kwenda App Router, tuliona kupungua kwa majibu ya polepole kwa 80%, kupunguza muda wa kutoa byte ya kwanza (TTFB), na kupunguza ukubwa wa bundle za upande wa mteja kwa kiasi kikubwa.