การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ
วิเคราะห์ขนาดบันเดิลของแอปพลิเคชัน และตรวจสอบเพื่อค้นหาและแก้ไขคอขวดด้านประสิทธิภาพ
การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ เป็นบทเรียน Next.js 15 Fullstack Web Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack Web Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Boost App Speed with Audits
Welcome to Bundle Analysis and Performance Audits! In modern web development, speed is key. Users expect fast loading times, and search engines reward performant sites.
This lesson will equip you with tools and techniques to understand what makes your Next.js application tick, identify bottlenecks, and ultimately make it faster.
What's a Bundle? Why Care?
When you build a Next.js application for production, all your code (JavaScript, CSS, assets) is processed and combined into one or more files called bundles.
- Faster Loading: Smaller bundles download quicker.
- Better UX: A fast site means happier users.
- SEO Boost: Search engines favor performant websites.
- Cost Savings: Less data transferred can mean lower hosting costs.
Meet Next.js Bundle Analyzer
The @next/bundle-analyzer is an essential tool that helps you visualize the contents of your Next.js production bundles. It creates an interactive treemap that shows exactly which modules and dependencies contribute to your app's total size.
This visual report makes it easy to spot large libraries or duplicated code that might be slowing down your application.
Set Up Bundle Analyzer: Install
First, you need to install the package as a development dependency in your Next.js project. Open your terminal in your project's root directory and run:
npm install --save-dev @next/bundle-analyzerSet Up Bundle Analyzer: Configure
Next, you'll update your next.config.js file to enable the analyzer. This tells Next.js to generate the bundle reports when you build your application.
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
openAnalyzer: false,
});
/** @type {import('next').NextConfig} */
const nextConfig = {};
module.exports = withBundleAnalyzer(nextConfig);Running the Analyzer
Now that it's configured, you can generate the bundle report. You'll typically do this by setting an environment variable before running your build command:
After the build completes, the reports (e.g., .next/analyze/client.html) will be generated. You can then open them in your browser to explore!
ANALYZE=true npm run buildInterpreting the Report
The bundle analyzer report is an interactive treemap. Here's what to look for:
- Large Rectangles: Indicate large modules or libraries.
- Red/Yellow Areas: Often highlight areas that contribute significantly to bundle size.
- Duplicates: Sometimes, different versions of the same library can be bundled.
- Unused Code: Can help you identify dependencies you might not need.
Hover over sections to see file sizes and paths.
Common Optimization Strategies
Once you've identified large parts of your bundle, here are common ways to optimize:
- Dynamic Imports (Code Splitting): Load components or libraries only when needed.
- Tree Shaking: Ensure unused code from imported modules is removed.
- Gzip/Brotli Compression: Server-side compression reduces file transfer size.
- Remove Unused Libraries: Audit your dependencies and remove those not in use.
- Optimize Images: Use
next/imageand appropriate formats.
Performance Audits with Lighthouse
Beyond bundle size, Google Lighthouse is a powerful tool built into Chrome DevTools that performs a comprehensive audit of your web page. It checks for:
- Performance: Metrics like First Contentful Paint, Largest Contentful Paint.
- Accessibility: How usable your app is for everyone.
- Best Practices: General web standards.
- SEO: Search engine optimization factors.
Run it on your production build to get actionable insights!
Check Your Knowledge
Which of the following are effective strategies for reducing your Next.js application's bundle size or improving its performance?
Recap & Next Steps
Great job! You've learned how to analyze and optimize your Next.js application's performance:
- We explored what a bundle is and why its size matters.
- You learned to set up and interpret reports from
@next/bundle-analyzer. - We covered key optimization techniques like dynamic imports and tree shaking.
- Finally, we introduced Google Lighthouse for comprehensive performance audits.
Keep these tools in your arsenal to build faster, more efficient Next.js applications!
คำถามที่พบบ่อย
บทเรียน “การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack Web Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ”
วิเคราะห์ขนาดบันเดิลของแอปพลิเคชัน และตรวจสอบเพื่อค้นหาและแก้ไขคอขวดด้านประสิทธิภาพ คุณปฏิบัติ Next.js 15 Fullstack Web Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack Web Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack Web Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack Web Apps นี้ได้ไหม
ได้ บทเรียน Next.js 15 Fullstack Web Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำไปใช้งานบน Vercel และ Netlify
- การปรับแต่งรูปภาพและแบบอักษร
- การวิเคราะห์บันเดิลและตรวจสอบประสิทธิภาพ
- ตัวชี้วัดหลักของเว็บและการตรวจติดตาม