Event for this.context.application.navigatedEvent fires twice, event args are empty
I have code something like in the repro. This code is registered as a application extension to run on all pages. The dialog comes up twice each time, once on navigation away, and the other on navigation to I believe, but the event args don't have anything in them, so I can't distinguish in the code if I should fire the dialog. I only want it to fire when the page is navigated to, not away from.
Category
- [X] Question
- [ ] Typo
- [x] Bug
- [ ] Additional article idea
Expected or Desired Behavior
I would expect that the event args would have an identifier if the event was away from or to the page.
Observed Behavior
event fires twice and no event args
Steps to Reproduce
-
Create a application extension for running script on all pages- https://github.com/SharePoint/sp-dev-docs/blob/master/docs/spfx/extensions/guidance/migrate-from-usercustomactions-to-spfx-extensions.md
-
In the extension, place code something like this-
/** A Custom Action which can be run during execution of a Client Side Application */
export default class HelloWorldApplicationCustomizer
extends BaseApplicationCustomizer
@override
public onInit(): PromiseInitialized ${strings.Title});
let message: string = this.properties.testMessage;
if (!message) {
message = "(No properties were provided.)";
}
this.context.application.navigatedEvent.add(this, this.showAlert);
return Promise.resolve();
}
public showAlert(args1:EventArgs):void { let message: string = this.properties.testMessage; if (!message) { message = "(No properties were provided.)"; } // args1 doesn't have any data to distinguish Dialog.alert("test"); } }
Try the extension and note after a few navigations between modern pages (as=json) it starts showing the dialog twice.
I did notice if I switched to a simple js alert("test") statement, you can tell it fires once on the page leaving, and the second time on page entering. The Dialog doesn't, but I think that is due to some async code in that class.
I have the same problem!
I have the problem too !! I noticed that if i arrive on a site through the home page, i get events when leaving the page and when enering the new one and if i rrive on the site through another page, i only get an event when leaving the page... @VesaJuvonen : could you provide a reliable documentation explaining how navigatedEvent are supposed to work ? When are they triggered ? What are the eventArgs supposed to be ?
We would like to change the content of the top placeholder based on the page we're on and change it when the user scrolls into the page but right know, we can't manage to do that because depending on the situation, we do not have the same navigatedEvents fired...
Hello,
I have the same problem, did you find a solution ?
This is under investigation currently by the product team and they have some idea of what may be causing the issue. I should hear back soon.
We've got the same issue and have a workaround in place to limit what we're doing on navigated to once per navigation. If there's interest I'll post our workaround.
Hi @jonthenerd, I am really interested in your workaround to narrow navigatedEvents to only one occurrence. can you share it with us please ?
Many thanks
Hi,
The issue is still present in SPFx 1.6. Have you guys found a solution/workaround?
@patmill any ideas?
I’ll try and post my workaround by end of week. It’s working for 1.4.1 but I don’t see why it wouldn’t work for higher.
From: Mikael Svenson [email protected] Sent: Wednesday, September 12, 2018 10:27 AM To: SharePoint/sp-dev-docs Cc: Jon Badgett; Mention Subject: Re: [SharePoint/sp-dev-docs] Event for this.context.application.navigatedEvent fires twice, event args are empty (#1871)
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FSharePoint%2Fsp-dev-docs%2Fissues%2F1871%23issuecomment-420690802&data=02%7C01%7C%7C16e5e2ae95734234edd908d618c4503d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636723628764804486&sdata=Rd0WmpbqBFAvLWMlUUiMLX1fvpgvaeVFLUzVx8WQ48c%3D&reserved=0, or mute the threadhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAC-MAbodLphfTnQDw0Icf1foxUK26PHwks5uaSf5gaJpZM4UDa4x&data=02%7C01%7C%7C16e5e2ae95734234edd908d618c4503d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636723628764804486&sdata=NMsOUAOeGAAqYa0qpnnnIxrS9C0L%2BFQzEPnfzrlYcfY%3D&reserved=0.
Here's the workaround code. This is working in 1.4.1. Using this code, if you have something you need to run on initial page load you'll need to plumb it yourself. The second firing on navigating away is mitigated by just keeping track of the last known page location.
export default class YourCustomizer {
private currentPage = "";
private isInitialLoad = true;
private getFreshCurrentPage(): string {
return window.location.pathname + window.location.search;
}
private updateCurrentPage(): void {
this.currentPage = this.getFreshCurrentPage();
}
private navigatedEvent(): void {
const navigatedPage = this.getFreshCurrentPage();
if (!this.isInitialLoad && (navigatedPage !== this.currentPage)) {
this.realNavigatedEvent();
this.updateCurrentPage();
}
this.isInitialLoad = false;
}
private realNavigatedEvent(): void {
// TODO: Your work that depends upon the page changing
}
}
I noticed indeed that the navigatedEvent is being called twice when coming from an "internal" url, so basically when the partial loading kicks in.
I think a better solution would be if there was a navigatingEvent and a navigatedEvent, being fired when leaving and entering a page respectively.
Or, alternatively: a parameter that's being passed to the navigatedEvent which specifies if you're navigating away or not.
Same problem. SPEventArgs contains no info about the type of event.
I'm seeing this problem as well, any update from the product team on this issue? @VesaJuvonen?
Yes - it's a bug and a fix is being made. Take a look at https://github.com/SharePoint/sp-dev-docs/issues/3210 as well.
@patmill can we expect this to be fixed "behind the scenes", or will it be part of a new SPFx release?
The fix is rolling out, and should be turned on soon.
Is there anywhere to track this fix, and see when it's likely to arrive? We're getting desperate for this one! Thanks
This is active now on my developer tenant
The fix hit my tenant today, it appears to be working as expected now.
This in on my tenant now, which has fixed the firing twice issue. Now does anyone know how to delay the firing until after the navigated page has loaded? At the moment it seems to happen first, so document.location.pathname returns the previous page url where the link was clicked, rather than the one that has just been navigated to.