sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Event for this.context.application.navigatedEvent fires twice, event args are empty

Open westleyMS opened this issue 8 years ago • 26 comments

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

  1. 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

  2. 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(): Promise { Log.info(LOG_SOURCE, Initialized ${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.

westleyMS avatar May 17 '18 16:05 westleyMS

I have the same problem!

sortegamartin avatar Jun 29 '18 11:06 sortegamartin

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...

BraunJonathan avatar Jul 09 '18 14:07 BraunJonathan

Hello,

I have the same problem, did you find a solution ?

maximreynaud avatar Jul 23 '18 12:07 maximreynaud

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.

westleyMS avatar Jul 24 '18 00:07 westleyMS

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.

jonthenerd avatar Aug 02 '18 14:08 jonthenerd

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

orty avatar Aug 31 '18 07:08 orty

Hi,

The issue is still present in SPFx 1.6. Have you guys found a solution/workaround?

ThomasGranheim avatar Sep 12 '18 13:09 ThomasGranheim

@patmill any ideas?

wobba avatar Sep 12 '18 15:09 wobba

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)

@patmillhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fpatmill&data=02%7C01%7C%7C16e5e2ae95734234edd908d618c4503d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636723628764804486&sdata=pusEN46Mbflbl2hN0SrSobYGwmAVcVsS6TvS76IrbOI%3D&reserved=0 any ideas?

— 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.

jonthenerd avatar Sep 12 '18 16:09 jonthenerd

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
  }
}

jonthenerd avatar Sep 18 '18 13:09 jonthenerd

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.

nse7ens avatar Sep 27 '18 10:09 nse7ens

Same problem. SPEventArgs contains no info about the type of event.

dsimoes avatar Dec 13 '18 14:12 dsimoes

I'm seeing this problem as well, any update from the product team on this issue? @VesaJuvonen?

patrickabel avatar Jan 08 '19 22:01 patrickabel

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 avatar Jan 08 '19 22:01 patmill

@patmill can we expect this to be fixed "behind the scenes", or will it be part of a new SPFx release?

joelfmrodrigues avatar Jan 09 '19 13:01 joelfmrodrigues

The fix is rolling out, and should be turned on soon.

westleyMS avatar Jan 19 '19 02:01 westleyMS

Is there anywhere to track this fix, and see when it's likely to arrive? We're getting desperate for this one! Thanks

dg2000r avatar Feb 15 '19 14:02 dg2000r

This is active now on my developer tenant

mirkori avatar Feb 27 '19 10:02 mirkori

The fix hit my tenant today, it appears to be working as expected now.

westleyMS avatar Feb 27 '19 16:02 westleyMS

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.

dg2000r avatar Mar 01 '19 16:03 dg2000r