Micro Frontends Architecture with Module Federation · บทเรียน

การโหลดโมดูลแบบไดนามิก

สร้างการโหลดโมดูลรีโมตแบบไดนามิก เพื่อปรับปรุงเวลาโหลดเริ่มต้นและเพิ่มประสิทธิภาพการใช้ทรัพยากร

บทเรียน 3 จาก 412 ขั้นตอน

การโหลดโมดูลแบบไดนามิก เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน

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

What is Dynamic Loading?

When building large web applications, especially with Micro Frontends, loading everything at once can make your app slow to start. This is where dynamic module loading comes in!

It's a technique that allows you to load parts of your application only when they are actually needed, rather than upfront.

Why Dynamic Loading for MFEs?

In a Micro Frontend architecture, different teams own different parts of the UI. Often, a user might only interact with one or two Micro Frontends at a time.

  • Improved Performance: Only download the code for the Micro Frontends currently in view.
  • Faster Initial Load: Reduce the initial bundle size, making your application feel snappier.
  • Optimized Resource Usage: Save bandwidth and memory by not loading unused modules.

The Dynamic `import()` Function

The core of dynamic loading in JavaScript is the import() function. It's a special syntax that allows you to import modules asynchronously.

When you use import(), Webpack (and other bundlers) automatically create a separate 'chunk' for that module. This chunk is then loaded only when the import() call is executed.

How `import()` Works

The import() function returns a Promise. This means you can use .then() and .catch() to handle the loaded module or any potential errors.

Alternatively, you can use async/await for a cleaner syntax when dealing with asynchronous operations.

Webpack's Role in Dynamic Imports

Webpack is smart! When it sees an import() call, it knows to treat the imported module as a split point.

This means it will create a separate JavaScript file (a 'chunk') for that module and its dependencies. This chunk is then fetched from the server only when the import() call is triggered during runtime.

Dynamic Remote Module Loading

In Module Federation, dynamic loading extends to remote modules. Instead of declaring all remotes to be loaded at startup, you can configure them to be loaded only when explicitly requested via import().

This is extremely powerful for large federated applications, allowing you to build truly on-demand Micro Frontends.

Host App: Dynamic Load Example

Try running this simple example. Notice how the 'feature' message appears after a slight delay, simulating an asynchronous load. In a real MFE, this would fetch a remote component.

console.log("App starts.");

async function loadFeature() {
  console.log("Loading feature dynamically...");
  try {
    // In a real MFE, this would be: 
    // const { renderFeature } = await import('remoteApp/Feature');
    
    // Simulating a module that resolves immediately
    const simulatedModule = {
      default: () => console.log("Feature 'A' loaded and activated!")
    };
    
    // Simulate the async delay of a real network request
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    simulatedModule.default();
    console.log("Feature loading complete.");

  } catch (error) {
    console.error("Failed to load feature:", error);
  }
}

// Trigger dynamic loading after initial app setup
setTimeout(loadFeature, 500);
console.log("Initial app setup done.");

Handling Loading States

Since dynamic loading is asynchronous, there will be a brief period while the module is being fetched over the network.

It's crucial to provide a good user experience by showing a loading indicator (e.g., a spinner or skeleton screen) during this time. This prevents the UI from appearing unresponsive.

Error Handling for Dynamic Imports

What if a dynamically loaded module fails to load? Perhaps due to a network error, a broken path, or the remote server being down?

Always wrap your dynamic import() calls in a try...catch block or use the .catch() method of the Promise to gracefully handle these errors. You can display an error message or a fallback UI.

Key Performance Benefits

To recap, dynamic module loading significantly boosts your application's performance:

  • Reduced Initial Bundle Size: Only essential code is loaded upfront.
  • Faster Time to Interactive (TTI): Users can interact with the main parts of your app sooner.
  • Better Resource Utilization: Less network traffic and memory usage for features not currently in use.
  • Improved User Experience: A snappier, more responsive application.

Dynamic Loading Check

Which of the following are primary benefits of implementing dynamic module loading in a Micro Frontend application?

Dynamic Loading Recap

Great job! In this lesson, we explored dynamic module loading as a crucial technique for optimizing Micro Frontends. We learned about the import() function, how Webpack handles it, and its benefits for performance and user experience.

By loading modules only when needed, you can build more efficient and responsive federated applications. Remember to handle loading states and potential errors for a robust user experience!

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การโหลดโมดูลแบบไดนามิก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การโหลดโมดูลแบบไดนามิก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Micro Frontends Architecture with Module Federation ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน

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

สร้างการโหลดโมดูลรีโมตแบบไดนามิก เพื่อปรับปรุงเวลาโหลดเริ่มต้นและเพิ่มประสิทธิภาพการใช้ทรัพยากร คุณปฏิบัติ Micro Frontends Architecture with Module Federation ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การโหลดโมดูลแบบไดนามิก” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม

ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การเรียกใช้สิ่งที่ต้องพึ่งพาร่วมกัน
  2. โมดูลซิงเกิลตันและการจัดการเวอร์ชัน
  3. การโหลดโมดูลแบบไดนามิก
  4. การแชร์สถานะและยูทิลิตีระหว่างแอประยะไกล
← กลับไปที่ Micro Frontends Architecture with Module Federation