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

Serving multiple frontends with the same server leads to only one being served #1806

Closed
sbihel opened this issue Sep 29, 2023 · 4 comments
Closed
Labels

Comments

@sbihel
Copy link
Contributor

sbihel commented Sep 29, 2023

Describe the bug
I am trying to create a monolith that serves two web apps (app1 and app2). When I spin up the server, the first web app that's opened (be it either app1 or app2) is rendered correctly, but then when the other app is opened, it is rendered as the other app.

This is using hydration on the client and SSR on the server. If hydration is replaced with CSR on the client and SSR disabled on the server -- then the render happens twice for each app (because it seems hydration is still enabled), with one of the renders being correct and other not.

Leptos Dependencies
I am using v0.5.0-rc3.

To Reproduce
Here is a minimal reproduction example, with commands used in the README: https://github.com/sbihel/leptos-multiple-apps

Expected behavior
Each app renders as itself.

Additional context
An example use case for this could be to have a monolith with a user dashboard and an admin dashboard, where the data may be sensitive enough that you want to separate them whilst making it easier to share code. This may not be supported, and it may not even be a good idea. But considering the end result is confusing, and the fact cargo-leptos mentions that "several projects can be defined and one package can be used in several projects", I thought this might be of interest.

@gbj
Copy link
Collaborator

gbj commented Sep 29, 2023

In the example, you're constructing two different Axum routers that both serve a single route /, and then chaining them together. As far as I can tell neither of these ever defines any other route. What behavior are you expecting to happen here? I don't understand what "Each app renders as itself" could mean in this context. When I make a request to /, what should the response be?

Or am I misunderstanding something about the setup?

@gbj gbj added the question Further information is requested label Sep 29, 2023
@sbihel
Copy link
Contributor Author

sbihel commented Sep 29, 2023

Ah yes I forgot to mention that, they're listening to different ports. And the reason why they have a router each is that I couldn't make it happen without it. As in, it's working properly, localhost:3000 displays "app1" and localhost:3001 displays "app2"

@gbj gbj removed the question Further information is requested label Sep 29, 2023
@gbj
Copy link
Collaborator

gbj commented Sep 29, 2023

Oh okay, I understand. Yes, leptos_router caches the set of possible route branches in a thread-local to avoid recalculating them on every single request.

thread_local! {
static BRANCHES: RefCell<HashMap<String, Vec<Branch>>> = RefCell::new(HashMap::new());
}
impl Branches {
pub fn initialize(base: &str, children: Fragment) {
BRANCHES.with(|branches| {
#[cfg(debug_assertions)]
{
if cfg!(any(feature = "csr", feature = "hydrate"))
&& !branches.borrow().is_empty()
{
leptos::logging::warn!(
"You should only render the <Routes/> component once \
in your app. Please see the docs at https://docs.rs/leptos_router/latest/leptos_router/fn.Routes.html."
);
}
}
let mut current = branches.borrow_mut();
if !current.contains_key(base) {

So I guess if you have two separate apps using leptos_router being served on two separate ports but from the same server (and therefore being served from the same thread), then yes, this would be a problem.

I'm open to suggestions for a good solution.

@sbihel
Copy link
Contributor Author

sbihel commented Oct 1, 2023

I'm not sure if that's the issue, I tried the wrap the branches hashmap with another hashmap using a router ID but the result is the same. Or maybe it is only part of the issue -- I imagine the runtimes management in thread_local can cause issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants