Skip to content
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

Creating an exception to "common" components using single-spa-layout #99

Closed
raihle opened this issue Dec 3, 2020 · 2 comments
Closed

Comments

@raihle
Copy link

raihle commented Dec 3, 2020

Describe the bug or question
Does / could single-spa-layout support exceptions to otherwise common components?

To Reproduce
I have an app like this:

<single-spa-router>
  <application name="navigation"></application>
  <route path="app-1">
    <application name="app-1"></application>
  </route>
  <!-- ... -->
</single-spa-router>

I want navigation to be present on "all" routes, but there are specific exceptions. This is pretty easy to implement with the application's underlying activity function, but can I access that while using single-spa-layout?

Additional context
I'm doing server-side rendering, otherwise I would probably just drop the layout engine and rely on manual registration.

@joeldenning
Copy link
Member

Hi @raihle, good question. Are the exceptions route based? If so, this is possible by putting the <application> underneath <route>. The <route default> can also be helpful here.

If the exceptions are not route-based, this is possible for browser-only usage of single-spa-layout, since the application objects are returned from constructApplications and can be modified however you'd like:

import { constructRoutes, constructApplications, constructLayoutEngine } from 'single-spa-layout';
import { registerApplication, start } from 'single-spa';

const routes = constructRoutes({...})
const applications = constructApplications({...});
const layoutEngine = constructLayoutEngine({...});

const navigationApp = applications.find(app => app.name === 'navigation')
navigationApp.activeWhen.push((location) => shouldNavigationMount(location))

applications.forEach(registerApplication)
start()

However, on the server side there are no application objects, and no existing way to render applications based on anything except routes. I'm open to that being added to sendLayoutHTTPResponse somehow, but it doesn't currently exist.

@joeldenning joeldenning transferred this issue from single-spa/single-spa Dec 4, 2020
@raihle
Copy link
Author

raihle commented Dec 7, 2020

Thanks for your reply!

I see now that I can nest additional routes inside <route default>, which allows me to use a top-level default for applications that include the common components, and a lower-level one for the "404" fallback. Thanks again!

@raihle raihle closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants