𝗛𝗼𝘄 𝗪𝗲 𝗖𝘂𝘁 𝗦𝗹𝗼𝘄 𝗥𝗲𝘀𝗽𝗼𝗻𝘀𝗲𝘀 𝗯𝘆 𝟴𝟬%

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.

𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀

Next.js App Router-এ মাইগ্রেট করে আমরা কীভাবে স্লো রেসপন্স ৮০% কমিয়ে এনেছি

আমাদের অ্যাপ্লিকেশনটি স্লো রেসপন্সের কারণে ব্যবহারকারীদের একটি খারাপ অভিজ্ঞতা দিচ্ছিল। আমরা সিদ্ধান্ত নিলাম Next.js-এর নতুন App Router-এ মাইগ্রেট করার। এই মাইগ্রেশনের ফলে আমরা আমাদের স্লো রেসপন্সের হার ৮০% কমিয়ে আনতে সক্ষম হয়েছি।

সমস্যা: Pages Router-এর সীমাবদ্ধতা

আমাদের আগের আর্কিটেকচারটি Next.js-এর Pages Router ব্যবহার করছিল। এর ফলে আমাদের কিছু বড় সমস্যা ছিল:

সমাধান: App Router এবং React Server Components (RSC)

App Router-এ মাইগ্রেট করার মাধ্যমে আমরা React Server Components (RSC) ব্যবহারের সুযোগ পেয়েছি। এটি আমাদের কাজের পদ্ধতিতে আমূল পরিবর্তন এনেছে।

১. Server Components-এর ব্যবহার

RSC ব্যবহারের ফলে আমরা আমাদের ডেটা ফেচিং লজিক এবং কম্পোনেন্টগুলোকে সরাসরি সার্ভারে চালাতে পারছি। এর ফলে ক্লায়েন্টে পাঠানো JavaScript-এর পরিমাণ নাটকীয়ভাবে কমে গেছে। এখন শুধুমাত্র ইন্টারঅ্যাক্টিভ অংশগুলোর জন্য ক্লায়েন্ট কম্পোনেন্ট ব্যবহার করা হচ্ছে।

Pages Router (আগে):

// pages/product/[id].js
export async function getServerSideProps(context) {
  const res = await fetch(`https://api.example.com/products/${context.params.id}`);
  const product = await res.json();

  return { props: { product } };
}

export default function Product({ product }) {
  return <div>{product.name}</div>;
}

App Router (এখন):

// app/product/[id]/page.js
async function getProduct(id) {
  const res = await fetch(`https://api.example.com/products/${id}`);
  return res.json();
}

export default async function Page({ params }) {
  const product = await getProduct(params.id);

  return <div>{product.name}</div>;
}

২. উন্নত ডেটা ফেচিং (Data Fetching)

App Router-এ আমরা fetch API-কে আরও কার্যকরভাবে ব্যবহার করতে পারছি। সার্ভার কম্পোনেন্টের ভেতরে সরাসরি async/await ব্যবহার করে ডেটা ফেচ করা এখন অনেক সহজ এবং দ্রুত। এতে ক্লায়েন্ট এবং সার্ভারের মধ্যে অতিরিক্ত রিকোয়েস্টের প্রয়োজন কমে গেছে।

৩. Streaming এবং Suspense

আমরা loading.js এবং React Suspense-এর মাধ্যমে স্ট্রিমিং ফিচারটি ব্যবহার করছি। এর ফলে পুরো পেজটি লোড হওয়ার জন্য অপেক্ষা না করে, পেজের যে অংশগুলো তৈরি হয়ে গেছে সেগুলো ব্যবহারকারী দ্রুত দেখতে পাচ্ছেন। এটি আমাদের Perceived Performance বা ব্যবহারকারীর কাছে অ্যাপ্লিকেশনের গতি বাড়ানোর ক্ষেত্রে বড় ভূমিকা রেখেছে।

ফলাফল

এই মাইগ্রেশনের পর আমরা নিচের পরিবর্তনগুলো লক্ষ্য করেছি:

মেট্রিক আগে (Pages Router) পরে (App Router)
স্লো রেসপন্স (Slow Responses) উচ্চ ৮০% হ্রাস
ক্লায়েন্ট-সাইড JS বান্ডেল বড় অনেক ছোট
ডেটা ফেচিং স্পিড ধীর দ্রুত

উপসংহার

Next.js App Router-এ মাইগ্রেট করা আমাদের জন্য একটি অত্যন্ত সফল সিদ্ধান্ত ছিল। এটি কেবল আমাদের পারফরম্যান্স উন্নত করেনি, বরং ডেভেলপার হিসেবে আমাদের কোড লেখার পদ্ধতিকেও আরও সহজ ও আধুনিক করেছে।