Part of Demo Microfrontends - A comprehensive Single-SPA microfrontend architecture demonstration
A Svelte microfrontend for Single-SPA demonstrating compile-time optimization, reactive programming, and minimal runtime overhead.
This application is one of 12 microfrontends in the demo-microfrontends project:
Microfrontend | Framework | Port | Route | Repository |
---|---|---|---|---|
š Auth App | Vue.js | 4201 | /login | single-spa-auth-app |
šØ Layout App | Vue.js | 4202 | All routes | single-spa-layout-app |
š Home App | AngularJS | 4203 | / | single-spa-home-app |
š °ļø Angular App | Angular 8 | 4204 | /angular/* | single-spa-angular-app |
š Vue App | Vue.js 2 | 4205 | /vue/* | single-spa-vue-app |
āļø React App | React 16 | 4206 | /react/* | single-spa-react-app |
š¦ Vanilla App | ES2020+ | 4207 | /vanilla/* | single-spa-vanilla-app |
š§© Web Components | Lit | 4208 | /webcomponents/* | single-spa-webcomponents-app |
š TypeScript App | TypeScript | 4209 | /typescript/* | single-spa-typescript-app |
š jQuery App | jQuery 3.6 | 4210 | /jquery/* | single-spa-jquery-app |
š„ Svelte App | Svelte 3 | 4211 | /svelte/* | This repo |
šÆ Root App | Single-SPA | 8080 | Orchestrator | single-spa-root |
Main Repository: demo-microfrontends
npm install
npm start
# Runs on http://localhost:4211
npm run build
# Outputs to dist/single-spa-svelte-app.js
// Automatic recalculation when dependencies change
$: doubleCount = count * 2;
$: countMessage = count === 0 ? 'Zero' : count === 1 ? 'One' : `${count} items`;
let count = 0;
let items = [];
let loading = false;
// Reactive updates automatically trigger re-renders
<button on:click={increment}>+</button>
<input bind:value={name} on:keypress={(e) => e.key === 'Enter' && addItem()}>
{#if loading}
<div class="loading">Loading...</div>
{:else if items.length === 0}
<div class="empty">No items yet!</div>
{:else}
{#each items as item (item.id)}
<div class="item">{item.name}</div>
{/each}
{/if}
This microfrontend exports the required Single-SPA lifecycle functions:
export const bootstrap = () => Promise.resolve();
export const mount = (props) => svelteApp.mount(props);
export const unmount = () => svelteApp.unmount();
The application mounts to the DOM element with ID svelte-app
:
<div id="svelte-app"></div>
Configured to activate on routes starting with /svelte
:
singleSpa.registerApplication(
'svelte',
() => loadApp('single-spa-svelte-app'),
showWhenPrefix(['/svelte'])
);
<script>
// Component logic
let state = 'reactive';
// Reactive statements
$: derivedValue = computeFromState(state);
// Event handlers
function handleClick() {
state = 'updated';
}
</script>
<!-- Template -->
<div class="component">
<button on:click={handleClick}>
{state}
</button>
</div>
<style>
/* Scoped styles */
.component {
/* Component-specific styles */
}
</style>
single-spa-svelte-app/
āāā src/
ā āāā App.svelte # Main Svelte component
ā āāā single-spa-svelte-app.js # Single-SPA integration
āāā dist/ # Build output directory
āāā package.json # Dependencies and scripts
āāā webpack.config.js # Build configuration
āāā .gitignore # Git ignore rules
āāā README.md # This file
MIT License - see LICENSE file for details.
Run the complete microfrontend system:
# Clone main repository
git clone https://github.com/cesarchamal/demo-microfrontends.git
cd demo-microfrontends
# Start all microfrontends
./run.sh local dev
Run this microfrontend individually:
npm install
npm start
# Visit http://localhost:4211
Cesar Francisco Chavez Maldonado - Svelte Microfrontend Example