Home > Article > Web Front-end > Episode The Guardian of Codex – Embracing PWAs and Micro-Frontends
Arin stood at the edge of Codex’s vast frontier, where the glow of its luminous data fields met the deep expanse of space. The hum of interconnected nodes buzzed beneath her feet, resonating with life and potential. Today was different; it wasn’t just another day in the Planetary Defense Corps (PDC). The mission was more than defending against adversaries—it was about fortifying Codex’s resilience, ensuring it could withstand disruptions while delivering seamless experiences to the Users who depended on it.
Captain Lifecycle’s voice cut through the silence, calm but stern. “Cadet Arin, remember: resilience is not just about power; it’s about adaptability. The Users are the essence of Codex, and their experience must be safeguarded at all costs.”
Arin took a deep breath, eyes scanning the shimmering horizon. The task was clear: fortify Codex with tools and techniques that would empower its defenses and maintain user trust.
Arin reached into the archives of Codex, where the ancient blueprints for Progressive Web Apps (PWAs) were stored. She knew that PWAs were not just apps—they were guardians that stood between Codex and the chaos of disconnection. These powerful constructs enabled offline capabilities, ensuring that Users would continue to access essential resources even when data pathways faltered.
What is a PWA?
A Progressive Web App (PWA) leverages service workers and manifests to offer web applications that behave like native apps, enabling offline use, faster load times, and installability.
Code Example: Service Worker Setup
Arin began crafting the service worker, the silent guardian that cached assets and provided seamless offline support:
if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/service-worker.js') .then(registration => { console.log('Service Worker registered with scope:', registration.scope); }) .catch(error => { console.error('Service Worker registration failed:', error); }); }); }
The light of the service worker's code glowed as Arin embedded it into Codex’s defenses, ensuring Users would never face the void, even in the absence of network connectivity.
Pros:
Cons:
When to Use:
When to Avoid:
Arin’s eyes scanned the vast, sprawling interface of Codex, each sector buzzing with its unique energy signature. The planet had grown complex over time, each addition making it harder to maintain. She recalled the teachings of the Builders of Scalability: “Divide and conquer. Each part must be able to stand alone yet function harmoniously.”
What are Micro-Frontends?
Micro-frontends extend the principle of microservices architecture to the frontend, enabling teams to break down a monolithic app into smaller, independently deployable units that function as one cohesive application.
This approach is especially beneficial for large-scale applications where multiple teams work on different parts of the app. Micro-frontends allow each team to maintain autonomy, update, and deploy their part without affecting the entire app.
Key Benefits of Micro-Frontends:
Potential Challenges:
The Test Case: Pokémon App:
Arin envisioned a Pokémon App where different parts, such as Poke Battle and Pokedex, were developed as separate micro-frontends. This division would ensure that updates to the Pokedex wouldn’t affect the Poke Battle and vice versa.
Setting Up the Container App:
The container app acts as the orchestrator that binds the micro-frontends together. Below is an example setup with Webpack Module Federation for integrating micro-frontends.
container-app/package.json:
if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/service-worker.js') .then(registration => { console.log('Service Worker registered with scope:', registration.scope); }) .catch(error => { console.error('Service Worker registration failed:', error); }); }); }
container-app/webpack.config.js:
{ "name": "container-app", "dependencies": { "react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^5.2.0" }, "scripts": { "start": "webpack serve --config webpack.config.js" } }
container-app/src/index.js:
const HtmlWebpackPlugin = require('html-webpack-plugin'); const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin'); module.exports = { entry: './src/index.js', mode: 'development', devServer: { port: 8080, }, output: { publicPath: 'http://localhost:8080/', }, plugins: [ new ModuleFederationPlugin({ name: 'container', remotes: { pokebattle: 'pokebattle@http://localhost:8081/remoteEntry.js', pokedex: 'pokedex@http://localhost:8082/remoteEntry.js', }, shared: ['react', 'react-dom'] }), new HtmlWebpackPlugin({ template: './public/index.html', }), ], };
Creating the Poke Battle Micro-Frontend:
The Poke Battle micro-frontend has its own codebase and Webpack configuration.
pokebattle/package.json:
if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/service-worker.js') .then(registration => { console.log('Service Worker registered with scope:', registration.scope); }) .catch(error => { console.error('Service Worker registration failed:', error); }); }); }
pokebattle/webpack.config.js:
{ "name": "container-app", "dependencies": { "react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^5.2.0" }, "scripts": { "start": "webpack serve --config webpack.config.js" } }
pokebattle/src/App.js:
const HtmlWebpackPlugin = require('html-webpack-plugin'); const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin'); module.exports = { entry: './src/index.js', mode: 'development', devServer: { port: 8080, }, output: { publicPath: 'http://localhost:8080/', }, plugins: [ new ModuleFederationPlugin({ name: 'container', remotes: { pokebattle: 'pokebattle@http://localhost:8081/remoteEntry.js', pokedex: 'pokedex@http://localhost:8082/remoteEntry.js', }, shared: ['react', 'react-dom'] }), new HtmlWebpackPlugin({ template: './public/index.html', }), ], };
Setting Up the Pokedex Micro-Frontend:
pokedex/package.json:
import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; const PokeBattle = React.lazy(() => import('pokebattle/App')); const Pokedex = React.lazy(() => import('pokedex/App')); function App() { return ( <Router> <React.Suspense fallback={<div>Loading...</div>}> <Switch> <Route path="/pokebattle" component={PokeBattle} /> <Route path="/pokedex" component={Pokedex} /> </Switch> </React.Suspense> </Router> ); } ReactDOM.render(<App />, document.getElementById('root'));
pokedex/webpack.config.js:
{ "name": "pokebattle", "dependencies": { "react": "^17.0.2", "react-dom": "^17.0.2" }, "scripts": { "start": "webpack serve --config webpack.config.js" } }
pokedex/src/App.js:
const HtmlWebpackPlugin = require('html-webpack-plugin'); const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin'); module.exports = { entry: './src/index.js', mode: 'development', devServer: { port: 8081, }, output: { publicPath: 'http://localhost:8081/', }, plugins: [ new ModuleFederationPlugin({ name: 'pokebattle', filename: 'remoteEntry.js', exposes: { './App': './src/App', }, shared: ['react', 'react-dom'] }), new HtmlWebpackPlugin({ template: './public/index.html', }), ], };
Arin’s Revelation:
Arin stood back, watching as Codex’s new micro-frontend architecture shimmered to life. Each segment was an independent yet harmonious part of a greater whole. “Codex is now stronger,” she thought. “Each part can fight, adapt, and evolve on its own.”
Pros:
Cons:
When to Use:
When to Avoid:
Arin turned to Captain Lifecycle, who nodded approvingly. “The Users must feel that Codex is always responsive, always prepared,” he said. Code splitting and lazy loading were the keys to ensuring this. By loading only what was necessary, Codex could maintain its agility and keep Users immersed in their experience.
Code Splitting Example:
import React from 'react'; function App() { return ( <div> <h1>Poke Battle Arena</h1> <p>Choose your Pokémon and battle your friends!</p> </div> ); } export default App;
Pros:
Cons:
When to Use:
When to Avoid:
|
Definition |
Pros | Cons | When to Use | When to Avoid | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Progressive Web Apps (PWAs) | Web apps with offline capabilities and native-like features. | Offline access, improved performance, user engagement. | Complex service worker management, debugging challenges. | For apps needing offline capabilities and quick load. | Apps that don’t benefit from offline or native features. | ||||||||||||||||||||||||
Micro-Frontends | Independent, deployable micro-apps forming one application. | Team autonomy, independent deployments, modular architecture. | Communication complexity, potential dependency duplication. | Large apps needing scalable, modular development. | Simple apps where the complexity isn’t justified. | ||||||||||||||||||||||||
Code Splitting | Splitting code into smaller chunks that load on demand. | Reduces initial load time, improves UX. | Requires managing loading states, can complicate debugging. | Apps with large, seldom-used components. | Lightweight apps with minimal performance concerns. |
The above is the detailed content of Episode The Guardian of Codex – Embracing PWAs and Micro-Frontends. For more information, please follow other related articles on the PHP Chinese website!