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

Considerations for cross-framework compatibility #14

Closed
hmil opened this issue Sep 9, 2021 · 2 comments
Closed

Considerations for cross-framework compatibility #14

hmil opened this issue Sep 9, 2021 · 2 comments

Comments

@hmil
Copy link
Contributor

hmil commented Sep 9, 2021

I'm trying to use this module in the context of micro-frontends (ie. a single page which composes independent widgets written in different frameworks), and I have run across compatibility issues.

In my research, I tried combining a svelte app using svelte-router with a react app using react-router. There are two main ways to do navigation in JS, which I'm analyzing separately:

The history API (pushState)

Because the history API doesn't provide a way to listen for changes, a workaround is always necessary. svelte-router and react-router use different and incompatible approaches to solve the problem:

  • svelte-router hooks the browser API so it can be notified when functions are called
  • react-router uses a standalone replacement for the history api

You can't really argue which is best. Both are necessary evils. The problem is: they don't work together!

Because svelte hooks the browser API, it is able to receive state changes initiated in react.
However, when svelte makes changes to the URL, react doesn't see it because it doesn't use its shim.

The location API

If path navigation is not requirement and you can use hash navigation, then the location API is a suitable candidate.

The main advantage of the location API is that it has native support for listening.

Here, react-router is able to respond to hash changes initiated from outside React, presumably because it listens to the hashchange event.

But svelte-router does not use the location API at all. Even in hash navigation, svelte-router keeps using the troublesome history API. So this time communication from react to svelte doesn't work!


The table below summarizes the compatibility issues:

routing mode React -> Svelte Svelte -> React
path (history) 🚫
hash (mixed) 🚫
hash (location)

The last line is the suggested solution: For hash-based navigation, use only the location API. Because its support for listening is built-in, it will work out of the box!

@bluwy
Copy link
Owner

bluwy commented Sep 9, 2021

This is now fixed in v0.5.0. Thanks!

@bluwy bluwy closed this as completed Sep 9, 2021
@hmil
Copy link
Contributor Author

hmil commented Sep 9, 2021

Amazing. Thanks so much!

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