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

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.

𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀

كيف خفضنا الاستجابات البطيئة بنسبة 80% عبر الانتقال إلى Next.js App Router

في هذا المقال، سنشارككم كيف تمكنا من تحسين أداء تطبيقنا بشكل كبير من خلال الانتقال من Next.js Pages Router إلى App Router. لقد أدى هذا التحول إلى تقليل الاستجابات البطيئة بنسبة 80%، وسنستعرض معكم الأسباب والنتائج.

المشكلة: قيود الـ Pages Router

عندما بدأنا باستخدام Next.js، كان الـ Pages Router هو الخيار المعتاد. ومع ذلك، مع نمو تطبيقنا، واجهنا تحديات تتعلق بالأداء:

الحل: الانتقال إلى الـ App Router

قدم الـ App Router نهجاً جديداً يعتمد على الخادم أولاً (Server-first)، مما سمح لنا باستغلال ميزات React الحديثة.

1. مكونات React Server Components (RSC)

هذا هو التغيير الجذري. في الـ Pages Router، كانت معظم المكونات تُعامل كمكونات عميل (Client Components). أما في الـ App Router، أصبح بإمكاننا استخدام Server Components بشكل افتراضي.

الفوائد:

2. البث (Streaming) و Suspense

بدلاً من انتظار تحميل الصفحة بأكملها، سمح لنا الـ App Router بـ "بث" أجزاء الصفحة تدريجياً. باستخدام Suspense من React، يمكننا عرض أجزاء من الصفحة (مثل الهياكل العظمية - Skeletons) بينما يتم تحميل البيانات للأجزاء الأخرى في الخلفية.

هذا يحسن بشكل كبير من الأداء المدرك (Perceived Performance)، حيث يشعر المستخدم أن التطبيق يستجيب فوراً.

3. التخطيطات المتداخلة (Nested Layouts)

سمحت لنا الـ Layouts في الـ App Router بتقليل كمية العمل الذي يحتاجه المتصفح. عند التنقل بين الصفحات، لا يتم إعادة تحميل الأجزاء المشتركة (مثل القوائم الجانبية أو شريط التنقل)، مما يجعل التنقل سلساً للغاية.

النتائج

بعد الانتقال الكامل، شهدنا تحسناً ملحوظاً في مقاييس الأداء:

المقياس قبل الانتقال (Pages Router) بعد الانتقال (App Router) التحسن
الاستجابات البطيئة (Slow Responses) عالية منخفضة جداً 80% ↓
حجم حزمة JavaScript كبير صغير ~50% ↓
وقت التفاعل (TTI) بطيء سريع جداً ملحوظ

الخلاصة

لم يكن الانتقال إلى Next.js App Router مجرد تغيير في هيكلية الملفات، بل كان استراتيجية لتحسين الأداء من الجذور. من خلال تقليل كمية JavaScript المرسلة إلى العميل واستخدام تقنيات مثل Streaming و Server Components، تمكنا من تقديم تجربة مستخدم أسرع وأكثر كفاءة.