-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Add microfrontends guide for Workers #27364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: production
Are you sure you want to change the base?
Conversation
Document microfrontend architecture using router workers and service bindings, including routing logic, path rewriting, asset handling, preloading, and deployment workflows.
|
This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:
|
src/content/docs/workers/framework-guides/web-apps/microfrontends.mdx
Outdated
Show resolved
Hide resolved
Co-authored-by: Brayden Wilmoth <brayden.wilmoth@gmail.com>
| ## Preloading | ||
|
|
||
| The router can generate a preload script that prefetches other microfrontends in the background, improving navigation performance between different parts of your application. | ||
|
|
||
| When you set `"preload": true` on a route, the router: | ||
|
|
||
| 1. Generates a `/__mf-preload.js` script | ||
| 2. Injects it into HTML responses | ||
| 3. Prefetches configured microfrontends after page load | ||
|
|
||
| Example preload script (automatically generated): | ||
|
|
||
| ```javascript | ||
| // Injected by router worker | ||
| const routes = ["/app-a", "/app-b"]; | ||
| const run = () => { | ||
| for (const p of routes) { | ||
| fetch(p, { | ||
| method: "GET", | ||
| credentials: "same-origin", | ||
| cache: "default", | ||
| }).catch(() => {}); | ||
| } | ||
| }; | ||
|
|
||
| if (document.readyState === "loading") { | ||
| document.addEventListener("DOMContentLoaded", run, { once: true }); | ||
| } else { | ||
| run(); | ||
| } | ||
| ``` | ||
|
|
||
| This allows browsers to fetch and [cache](/cache/) resources from other microfrontends before users navigate to them, resulting in faster transitions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## Preloading | |
| The router can generate a preload script that prefetches other microfrontends in the background, improving navigation performance between different parts of your application. | |
| When you set `"preload": true` on a route, the router: | |
| 1. Generates a `/__mf-preload.js` script | |
| 2. Injects it into HTML responses | |
| 3. Prefetches configured microfrontends after page load | |
| Example preload script (automatically generated): | |
| ```javascript | |
| // Injected by router worker | |
| const routes = ["/app-a", "/app-b"]; | |
| const run = () => { | |
| for (const p of routes) { | |
| fetch(p, { | |
| method: "GET", | |
| credentials: "same-origin", | |
| cache: "default", | |
| }).catch(() => {}); | |
| } | |
| }; | |
| if (document.readyState === "loading") { | |
| document.addEventListener("DOMContentLoaded", run, { once: true }); | |
| } else { | |
| run(); | |
| } | |
| ``` | |
| This allows browsers to fetch and [cache](/cache/) resources from other microfrontends before users navigate to them, resulting in faster transitions. | |
| ## Route Preloading | |
| When `preload: true` is set on a static mount route, the router automatically preloads those routes to enable faster navigation. The router uses **browser-specific optimization** to provide the best performance for each browser: | |
| ### Chromium Browsers (Chrome, Edge, Opera, Brave) | |
| For Chromium-based browsers, the router uses the **Speculation Rules API** - a modern, browser-native prefetching mechanism: | |
| - Injects `<script type="speculationrules">` into the `<head>` element | |
| - Browser handles prefetching automatically with optimal priority management | |
| - Respects user preferences (battery saver, data saver modes) | |
| - Uses per-document in-memory cache for faster access | |
| - Not blocked by Cache-Control headers | |
| - More efficient than JavaScript-based fetching | |
| **Example injected speculation rules:** | |
| ```json | |
| { | |
| "prefetch": [ | |
| { | |
| "urls": ["/app1", "/app2", "/dashboard"] | |
| } | |
| ] | |
| } |
Other Browsers (Firefox, Safari)
For browsers that don't yet support the Speculation Rules API, the router falls back to a JavaScript-based preload script:
- Injects
<script src="/mount/__mf-preload.js" defer></script>into the<body>element - Script fetches preload routes after DOM loads
- Uses external script (CSP-friendly) instead of inline JavaScript
- Uses
GETrequests withcredentials: "same-origin"andcache: "default"
Browser Detection:
The router automatically detects the browser from the User-Agent header and injects the appropriate preload mechanism. No configuration needed!
Limitations:
- Only works for static mounts (no dynamic parameters)
- Only preloads routes that are not the current route
- Static mounts must have
preload: truein their route configuration
Example Configuration:
{
"routes": [
{ "binding": "APP1", "path": "/app1", "preload": true },
{ "binding": "APP2", "path": "/app2", "preload": true },
{ "binding": "APP3", "path": "/:tenant/dashboard" } // Cannot preload (dynamic parameter)
]
}When a user visits /app1, the router will automatically preload /app2 (but not /app1 since that's the current route).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that didn't take the full suggestion because of backticks I imagine...
|
Template added to our templates repo here: cloudflare/templates#877 |
Document microfrontend architecture using router workers and service bindings, including routing logic, path rewriting, asset handling, preloading, and deployment workflows.