การสร้างเส้นทางแบบสหพันธ์
สร้างการกำหนดเส้นทางที่ไมโครฟรอนต์เอนด์แต่ละตัวจัดการเส้นทางของตนเอง และผสานรวมเข้ากับเราเตอร์ส่วนกลาง
การสร้างเส้นทางแบบสหพันธ์ เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Micro Frontends Architecture with Module Federation และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Micro Frontends Architecture with Module Federation มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Federated Routing?
In a Micro Frontend (MFE) architecture, federated routing means each individual MFE is responsible for managing its own specific routes. Instead of one central application dictating all routes, each MFE declares its own pathways.
This approach gives teams more autonomy, allowing them to develop and deploy routing logic independently without tightly coupling to a main host application's routing system.
Benefits of Federated Routes
Federated routing offers several key advantages:
- Autonomy: MFE teams control their own navigation.
- Independent Deployment: Changes to an MFE's routes don't require redeploying the entire host app.
- Scalability: Easier to manage routing logic as the number of MFEs grows.
- Technology Agnostic: Different MFEs can use different routing libraries.
Core Principle: Route Ownership
The fundamental idea behind federated routing is that each Micro Frontend publishes its own set of routes. Think of it like a mini-application within the larger system, responsible for its internal navigation structure.
The host application then acts as an aggregator, discovering and integrating these MFE-owned routes into a single, cohesive routing experience for the user.
MFE Defining Local Routes
Here's how a Micro Frontend (let's call it "MFE A") might define its internal routes. These are specific paths that MFE A is responsible for rendering.
We're using a simple array structure to represent routes, where each route has a path and a component or view it should render.
// mfe-a-routes.js
// This MFE owns routes starting with '/app-a'
export const mfeARoutes = [
{
path: '/app-a',
component: 'MFE_A_Dashboard'
},
{
path: '/app-a/profile',
component: 'MFE_A_UserProfile'
}
];Another MFE's Routes
Similarly, another Micro Frontend ("MFE B") will define its own set of routes. Notice how its paths are distinct, often prefixed to avoid conflicts, ensuring clear ownership.
This separation allows MFE A and MFE B to evolve their navigation independently.
// mfe-b-routes.js
// This MFE owns routes starting with '/app-b'
export const mfeBRoutes = [
{
path: '/app-b',
component: 'MFE_B_HomePage'
},
{
path: '/app-b/settings',
component: 'MFE_B_SettingsPage'
}
];Host Aggregates MFE Routes
The host application's role is to act as the central orchestrator. It needs to discover and import the route definitions provided by each Micro Frontend.
Using Module Federation, these route definitions can be exposed by remote MFEs and consumed by the host, effectively building a global route map from distributed sources.
Integrating into a Global Router
The host application then takes all the aggregated routes and integrates them into its own global router instance. This example simulates how a host app might load and process routes from different MFEs to handle navigation.
Try changing the currentPath variable in the code and run it!
// mfe-a-routes.js (Simulated Remote MFE)
export const mfeARoutes = [
{ path: '/app-a', component: 'MFE_A_Dashboard' },
{ path: '/app-a/profile', component: 'MFE_A_UserProfile' }
];
// mfe-b-routes.js (Simulated Remote MFE)
export const mfeBRoutes = [
{ path: '/app-b', component: 'MFE_B_HomePage' },
{ path: '/app-b/settings', component: 'MFE_B_SettingsPage' }
];
// host-app.js (Main Application Entry Point)
// In a real app, these would be dynamically imported
// via Module Federation.
const allFederatedRoutes = [
...mfeARoutes,
...mfeBRoutes
];
function globalRouter(currentPath) {
console.log(`Attempting to navigate to: ${currentPath}`);
const matchedRoute = allFederatedRoutes.find(
route => route.path === currentPath
);
if (matchedRoute) {
console.log(`Match found! Rendering: ${matchedRoute.component}`);
} else {
console.log(`No route found for: ${currentPath}. Showing 404.`);
}
}
// Simulate user navigation
console.log("--- Global Routing Simulation ---");
globalRouter('/app-a');
globalRouter('/app-b/settings');
globalRouter('/app-a/profile');
globalRouter('/unknown-path');
globalRouter('/app-b');Seamless Cross-MFE Navigation
Once the host's global router has integrated all federated routes, navigation between different Micro Frontends becomes seamless. From the user's perspective, it feels like a single, unified application.
When a user clicks a link to /app-b/settings, the global router identifies that this path belongs to MFE B and delegates the rendering or activation of MFE B accordingly.
Avoiding Route Conflicts
With multiple MFEs defining routes, conflicts can arise if two MFEs define the same path (e.g., both have a /dashboard route).
Common strategies to prevent this include:
- Namespacing: Prefixing MFE routes (e.g.,
/mfea/dashboard,/mfeb/dashboard). - Unique Paths: Ensuring each MFE's routes are inherently unique.
- Host Override: Allowing the host to prioritize or remap conflicting routes.
Check Your Understanding
Imagine you have a host application integrating routes from two Micro Frontends, MFE-A and MFE-B. MFE-A defines /products and /products/:id. MFE-B defines /cart and /checkout.
Which of the following best describes the host application's role in this federated routing setup?
Recap: Federated Routing
You've learned about federated routing, where each Micro Frontend owns and defines its own routes. We saw how a host application aggregates these routes to create a unified navigation experience.
- Each MFE declares its own routes.
- Host app collects and integrates MFE routes.
- Ensures MFE autonomy and independent deployment.
- Namespacing helps prevent conflicts.
This approach is crucial for large-scale MFE systems, balancing independence with a consistent user experience.
คำถามที่พบบ่อย
บทเรียน “การสร้างเส้นทางแบบสหพันธ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างเส้นทางแบบสหพันธ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างเส้นทางแบบสหพันธ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Micro Frontends Architecture with Module Federation นี้ได้ไหม
ได้ บทเรียน Micro Frontends Architecture with Module Federation ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กลยุทธ์การกำหนดเส้นทางแบบรวมศูนย์
- การสร้างเส้นทางแบบสหพันธ์
- การลิงก์เชิงลึกและการจัดการ URL
- การจัดการการนำทางแบบซ้อนและข้ามแอป