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

Calling Navigation.newUrl from the init user function does not trigger a call to urlUpdate #66

Closed
mlaily opened this issue Oct 14, 2023 · 3 comments

Comments

@mlaily
Copy link
Contributor

mlaily commented Oct 14, 2023

Description

From what I could see, urlUpdate is automatically called whenever a navigation even occurs, like one triggered by a call to Navigation.newUrl.

This is not the case when calling Navigation.newUrl from the user init function though.

From what I could see, this seems to be due to the fact subscriptions are initialized after the init function is called, so the browser navigation event goes unnoticed (the onLocationChange handler supposed to intercept it is not setup yet).

This is inconvenient because it means trying to do a url redirection when the app starts does not work as expected (the browser url will change, but will be out of sync with the model).

It might be a known issue/shortcoming, but I could not find documentation on this so I'm opening this issue.

Workaround

A simple workaround is to create a user message (e.g. DelayedRedirectIndexPage), and use it from the init function instead of calling Navigation.newUrl.

Then in the update function, handle the DelayedRedirectIndexPage message and do the call to Navigation.newUrl there.

Doing it that way ensures the onLocationChange subscription of Elmish.Navigation is setup when the navigation event is triggered.

Related information

  • elmish version: 4.0.2 (latest)
  • fable-compiler version: 4.1.4
  • fable-core version: 4.1
@MangelMaxime
Copy link
Member

From what I could see, this seems to be due to the fact subscriptions are initialized after the init function is called, so the browser navigation event goes unnoticed (the onLocationChange handler supposed to intercept it is not setup yet).

I think that you are right that init is called before the subscriptions are attached but Navigation.newUrl is not called directly because this a command.

Looking at the code it seems like the execution order is:

  1. Call init
  2. Call subscribe
  3. Execute the cmd coming from init in your case Navigation.newUrl
  4. Compute (register) subscription ?
  5. Start the loop which process messages indefinitely

CleanShot 2023-10-15 at 17 21 59@2x

Could it be that 3 and 4 needs to swap?

I am not all that familiar with the new subscription system of Elmish so all I am saying are things to investigate.

Also, would you have a reproduction code/project that we could use and check for the bug?

@et1975
Copy link
Member

et1975 commented Oct 16, 2023

The fix is released as Elmish@4.1.0, please give it a spin.

@et1975 et1975 closed this as completed Oct 16, 2023
@mlaily
Copy link
Contributor Author

mlaily commented Oct 17, 2023

@et1975 tested and my scenario works out of the box now. Thanks a lot! ♥

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

3 participants