question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can this be used in a single spa application?

See original GitHub issue

I’m having issues using tss-react in a single spa environment. In my root app, I am able to get the UMD version of MUI and emotion (not been able to find an umd/systemjs version of this package or create one successfully):

        "@mui/material": "https://unpkg.com/@mui/material@5.4.3/umd/material-ui.production.min.js",
        "@emotion/react": "https://cdn.jsdelivr.net/npm/@emotion/react@11.8.2/dist/emotion-react.umd.min.js",
        "@emotion/styled": "https://cdn.jsdelivr.net/npm/@emotion/styled@11.8.1/dist/emotion-styled.umd.min.js",

And they are being externalised by webpack in the sub app I am trying to use tss-react, In this app I have created a cache as the docs showed:

export const muiCache = createCache({
    'key': 'mui',
    'prepend': true,
  });

// region Component

const Theme: FunctionComponent<IThemeProps> = ({
  children,

  style
}) => {
  // handlers

  const theme = React.useMemo(() => {
    switch (style) {
      case Themes.Default:
      default:
        return DefaultTheme;
    }
  }, [style]);

  // render

  return (
    <CacheProvider value={muiCache}>
      <MuiThemeProvider theme={theme}>
        {children}
      </MuiThemeProvider>
    </CacheProvider>
  );
};

But MUI css always takes precedence over tss-react created styles and cannot seem to get it to work. Is there a way to get this to work?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
tristan-cunninghamcommented, Apr 29, 2022

@liztownd @garronej Documentation may need to change if you have made it

Your solution causes single spa inspector dev tool to not work, infinite loading. This seems like it will happen if you use single spa layout engine.

To fix this, don’t create a new array with custom props added (applicationsWithProps in your example). Add the props to the route constructor like so:

const routes = constructRoutes(microfrontendLayout, {
  props: {
    createCache: createCache,
    CacheProvider: CacheProvider
  },
  loaders: {
  }
});

And then in your layout html file add the props to the application like this:

<application name="@your-app" props="createCache,CacheProvider"></application>
3reactions
liztowndcommented, Apr 30, 2022

@garronej & @kennyist – I WIN! Lol.

In the root-config file when registering the sub applications I passed an instance of the createCache & CacheProvider from @emotion, then picked them up from the props rather than importing them in each sub-app (I had several apps all importing their own theme object and assigning classNames). Voila, I now have one muiCache, and the styles are appearing in order, pre-prending the tss styles, each with their own prefix depending on the sub-app they originated from.

in root-config:

import { registerApplication, start } from "single-spa";
import {
  constructApplications,
  constructRoutes,
  constructLayoutEngine,
} from "single-spa-layout";
import microfrontendLayout from "./microfrontend-layout.html";
import { CacheProvider } from "@emotion/react";
import createCache from "@emotion/cache";

const routes = constructRoutes(microfrontendLayout);
const applications = constructApplications({
  routes,
  loadApp({ name }) {
    return System.import(name);
  },
});
const layoutEngine = constructLayoutEngine({ routes, applications });

const applicationsWithProps = applications.map((application) => {
  return {
    ...application,
    customProps: { createCache: createCache, CacheProvider: CacheProvider },
  };
});

applicationsWithProps.forEach(registerApplication);

layoutEngine.activate();
start();

inside cache provider in every sub app:

import { ReactChild } from "react";
import type { CacheProvider } from "@emotion/react";
import type createCache from "@emotion/cache";

export type Props = {
  CacheProvider: typeof CacheProvider;
  createCache: typeof createCache;
  children?: ReactChild;
};

export function Theme(props: Props) {

  const { CacheProvider, createCache, children } = props;

  const muiCache = createCache({
    "key": "mui", // to test in each sub app I assigned a different "key", they should all show up in one cache
    "prepend": false
  });

  return <CacheProvider value={muiCache}>{children}</CacheProvider>;

};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Frequently Asked Questions
No. We strongly encourage that your single-spa-config or root application does not use any JavaScript UI frameworks (React, Angular, Angularjs, Vue, etc) ...
Read more >
Building Micro Frontends Using Single-SPA Framework
Single-page applications are packaged up into modules and combine with the container app. These applications can be built on top of different ...
Read more >
What is single-spa framework? - Adservio
Single-spa frameworks differ from traditional web apps because they're a client-side application that works with the speed and performance of a ...
Read more >
Deploy single application individually · Issue #123
Let's say I have a single-spa with 10 applications. ... I found out that I can use the predefined scripts to build the...
Read more >
Connect Micro frontends with the Single-Spa framework. Step ...
This app will be responsible for connecting all the frameworks that we will use in the current article. Create a folder for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found