npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meta-spa-router

v1.2.0

Published

- Meta Router for creating a shell app that loads micro frontends (aka routed applications or child apps) using iframes - Because of the usage of iframes the micro frontends are isolated and cannot influence each other in an unplanned way. - Allows usi

Downloads

121

Readme

meta-spa-router: A lightweight and solid approach towards micro frontends (SPAs/ clients for micro services)

Features

  • Meta Router for creating a shell app that loads micro frontends (aka routed applications or child apps) using iframes
  • Because of the usage of iframes the micro frontends are isolated and cannot influence each other in an unplanned way.
  • Allows using different frameworks in different versions for the loaded micro frontends.
  • iframes are created at runtime
  • Allows jumping to a specific route within a child app
  • Synchronizing child app's routes with the shell's route
  • Resizes the iframes to prevent a scrollbar within the iframe
  • Tested with Hash-based routing
  • Tested w/ Chrome, Firefox, Edge and IE 11 (IE needs polyfills)
  • Allows Angular-like Aux-Routes to show several micro frontends side by side at one time

Installation

npm install meta-spa-router --save

Usage

Setting up the routes in the shell app:

var MetaRouter = require('meta-spa-router').MetaRouter;

var config = [
    {
        path: 'a',
        app: '/app-a/dist'
    },
    {
        path: 'b',
        app: '/app-b/dist'
    }
];

window.addEventListener('load', function() { 

    var router = new MetaRouter();
    router.config(config);
    router.init();
    router.preload();


    document.getElementById('link-a')
            .addEventListener('click', function() { router.go('a') });

    document.getElementById('link-b')
            .addEventListener('click', function() { router.go('b') });

    document.getElementById('link-aa')
            .addEventListener('click', function() { router.go('a', 'a') });

            document.getElementById('link-ab')
            .addEventListener('click', function() { router.go('a', 'b') });        

}); 

Setting up the routed client apps (Framework agnostic way):

    import { RoutedApp } from 'meta-spa-router';

    let routedApp = new RoutedApp();

    routedApp.config({ appId: 'a' });
    routedApp.init();

    [...]

    // When we change the route in the client app, tell the shell about it:
    routedApp.sendRoute(e.url);

    // When the shell changes the route, we have to tell our router about it:
    routedApp.registerForRouteChange(url => /* delegate url to your app's router */);

Please note that by convention the appId is the same as its path in the shell app.

The example below uses Angular and it's DI system for this task.

Example

  • see folder sample in the libs repo.
  • see index.js for setting up the meta routes
  • see app-a/src/app/app.module.ts - Even though this works with any framework, this client app uses Angular and it's Dependency Injection (DI) system. In this file, the ChildApp class is registered as a service. Instead of this you could also instanciate it directly when going with an other framework.
  • see app-a/src/app/app.component.ts for initializing child apps. Again, using Angular's DI which is not a condition.

Trying it out

  1. Install the libs for each child app and build them:
    cd app-a
    npm install
    npm run build

    cd ..

    cd app-b
    npm install
    npm run build

    cd ..
  1. Do the same for the shell app:
    npm install
    npm run build
  1. Start the shell app with a web server of your choice, e. g. http-server
    npm install -g http-server
    http-server -o

Aux Routes

You can use Angular-like Aux Routes that allows to load several micro frontends at the same time. To use this feature just define several outlets. You can use any id while the value outlet is used for the default route:

<div id="outlet"></div>
<div id="outlet2"></div>

After this, you can define per route for which outlet it is indented. If you skip the outlet property, the outlet with the id outlet is used:

var config = [
    {
        path: 'a',
        app: '/app-a/dist'
    },
    {
        path: 'b',
        app: '/app-b/dist',
        outlet: 'outlet2'
    }
];

To load a micro frontend into those outlets, just use the go method:

    router.go('a'); // loads a into (default) outlet
    router.go('b'); // loads b into outlet2 (see above)

Trying out Aux Routes

Please find an example for Aux Routes in the folder sample-aux. To install the dependencies and run it, you can use the commands described for the other sample above.

Blog

More infos about this and Angular can be found on my blog.