0Pricing
Web Performance Optimization & Lighthouse · บทเรียน

การแบ่งโค้ดและการโหลดแบบขี้เกียจ

แบ่งชุดโค้ด JavaScript ขนาดใหญ่เป็นส่วนย่อยที่โหลดตามต้องการด้วยการนำเข้าแบบไดนามิกและการแบ่งตามเส้นทาง เพื่อให้ผู้ใช้ดาวน์โหลดเฉพาะโค้ดที่จำเป็นจริง ๆ

การแบ่งโค้ดและการโหลดแบบขี้เกียจ เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Monolithic Bundle Problem

Shipping all JavaScript in one bundle forces users to download code for pages they may never visit. Code splitting breaks the bundle into smaller chunks loaded only when needed.

Dynamic import()

The dynamic import() expression returns a promise and tells bundlers to create a separate chunk. The module loads on demand at runtime.

button.addEventListener('click', async () => {
  const { renderChart } = await import('./chart.js');
  renderChart();
});

Route-Based Splitting

The most impactful split is per route: each page becomes its own chunk, so the initial load contains only the landing route.

const Dashboard = React.lazy(() => import('./pages/Dashboard'));

Suspense Fallbacks

While a lazy chunk loads, show a fallback so the UI stays responsive. In React this is Suspense; other frameworks have equivalents.

<Suspense fallback={<Spinner />}>
  <Dashboard />
</Suspense>

Component-Level Splitting

Heavy widgets like rich text editors, maps, or charts can be split too, loading only when the user opens that feature rather than on first paint.

Vendor Splitting

Separating third-party libraries into a vendor chunk lets it cache independently. App code changes frequently; vendor code rarely does, so users re-download less.

Prefetching Chunks

Lazy loading can add a delay when the user navigates. Prefetch the chunk during idle time, for example on link hover, so it is ready before the click.

link.addEventListener('mouseenter', () => import('./pages/Settings'));

Avoiding Over-Splitting

Too many tiny chunks create request overhead and waterfalls. Aim for meaningful boundaries (routes, big features), not splitting every small function.

Tree Shaking Synergy

Code splitting pairs with tree shaking: use ES module imports and import only what you need so dead code never enters any chunk.

import { debounce } from 'lodash-es'; // only debounce is bundled

Measuring the Result

Use a bundle analyzer to inspect chunk sizes and the Coverage tab in DevTools to find unused code shipped on initial load. Lighthouse flags large unused JS.

Strategy Summary

  • Split by route first.
  • Lazy-load heavy components.
  • Separate vendor chunks for caching.
  • Prefetch likely-next chunks during idle.

Quick Check

Which JavaScript feature tells bundlers to create a separate chunk loaded on demand?

Recap

You learned to shrink initial payload with code splitting: dynamic import(), route- and component-level lazy loading, vendor chunking, idle prefetching, and tree shaking. Split at meaningful boundaries and measure with a bundle analyzer.

คำถามที่พบบ่อย

บทเรียน “การแบ่งโค้ดและการโหลดแบบขี้เกียจ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแบ่งโค้ดและการโหลดแบบขี้เกียจ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแบ่งโค้ดและการโหลดแบบขี้เกียจ”

แบ่งชุดโค้ด JavaScript ขนาดใหญ่เป็นส่วนย่อยที่โหลดตามต้องการด้วยการนำเข้าแบบไดนามิกและการแบ่งตามเส้นทาง เพื่อให้ผู้ใช้ดาวน์โหลดเฉพาะโค้ดที่จำเป็นจริง ๆ คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแบ่งโค้ดและการโหลดแบบขี้เกียจ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม

ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การลดขนาดข้อมูล JavaScript
  2. กลยุทธ์การโหลดสคริปต์อย่างมีประสิทธิภาพ
  3. Web Workers และการทำงานนอกเธรดหลัก
  4. การแบ่งโค้ดและการโหลดแบบขี้เกียจ
← กลับไปที่ Web Performance Optimization & Lighthouse