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

Probable bug inside navigatedEvent listener of SPFx Application Customizer

Open devangbhavsar89 opened this issue 7 years ago • 25 comments

Hello guys!

I'm implementing a certain customization via. SPFx extension (i.e., an application customizer) which involves moving of some components (say, quick search box) from OOTB site header area to top placeholder area, as a part of custom header.

I do understand that the OOTB search box component loads asynchronously on the page. This has been taken care of already while performing the component transition. So that part is pretty much achieved quite easily. Now as we know, the OOTB quick search box is not shown on all pages (say, document library, for example), so then in order to comply to OOTB semantics/user experience, it should also not be shown in the custom header as well on such pages. And as I've figured out, there is some MDS kind of partial loading taking place when we navigate across different pages. This means that the full application customizer code is not executed each time and thus the transited component still remains in its place - even though it is not intended any more.

So, of course, some additional logic is needed to hide it again when user navigates to such pages. And in order to also handle the MDS behavior, I started using this.context.application.navigatedEvent.add(this, this._navigatedEventHandler); event handler and build some code to perform cleanup/re-render inside it, so that it triggers each time when user navigates across pages. This seems to do the trick (quite a bit), as it provided me with some kind of listener that can then perform cleanup/re-render when user navigates across different pages.

However, as I've noticed there is some problem with this event handler. It actually fires 2 times instead of 1, for every single page navigation. Apparently, it fires 2 times - one before page navigation starts and one after page navigation is completed (it's just like item adding and item added events). However, there is no way to identify the "navigating" and "navigated" events distinctly. I even tried checking the SPEventArgs parameter which comes as the method argument, but it is always empty. There is no useful information in it.

Any clue if I'm missing out on something here or most likely this is some native issue with SPFx extension, as I understand?

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

I'm expecting either of the following as a behavior:

  • Ideally there should be separate triggers for page navigating or page navigated events, so that appropriate one can be leveraged based on the requirement.
  • Or at least, the SPEventArgs parameter should provide some information about the type of event it is handling (whether it is page navigating or page navigated event).

Observed Behavior

The this.context.application.navigatedEvent() event triggers multiple times (just before page navigation starts and after the page navigation is completed). And on top of it, there is no way to distinguish both these events, because the method parameter/argument (SPEventArgs) is always empty in both cases.

Steps to Reproduce

Just place following code stub inside onInit() method of an application customizer:

this.context.application.navigatedEvent.add(this, (eventArgs: SPEventArgs) => {
    // Issue-1: Custom code to perform cleanup/re-render fires two times...
    this._navigatedEventHandler(eventArgs);
		
    // Issue-2: The eventArgs parameter is always empty!
    console.log(eventArgs);
});

Quick Workaround to the Issue

I tried a few tweaks in code and was eventually able to distinguish both events with the help of window.location parameter and some additional logic. This way, I got my code working in expected manner even with this 2 times execution of this.context.application.navigatedEvent() event handler. However, I'm not sure, if this is an optimal solution to the given problem definition. Please suggest?

Also it would be really nice, if we can have either both events available separately or at least its exact type (navigating or navigated) is known via. SPEventArgs parameter somehow.

devangbhavsar89 avatar Jan 05 '19 20:01 devangbhavsar89

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

msft-github-bot avatar Jan 05 '19 20:01 msft-github-bot

Thanks @devangbhavsar89 . I've got a repro and we're looking into it.

patmill avatar Jan 07 '19 19:01 patmill

Hi @patmill, do we have any update on this bug-fix?

Also I'm quite eager to know, in what manner will this issue be fixed. Meaning, (a) will we have separate handlers for navigating and navigated events or (b) have information about event type available inside SPEventArgs parameter? I believe, based on the type of fix that will be pushed to customer environments, I'll probably have to make corresponding adjustments to the earlier workaround which I'd made to get it working, to ensure that it continues to work seamlessly once the fix is pushed.

So just wanted to check how is it going to be fixed and when is it expected to roll-out?

devangbhavsar89 avatar Jan 22 '19 07:01 devangbhavsar89

The code is rolling out, so you should have it soon. The current fix is to not fire multiple events. There isn't currently an -ing/ed pair.

patmill avatar Jan 24 '19 18:01 patmill

That's great to know, thanks for the info!

devangbhavsar89 avatar Jan 25 '19 05:01 devangbhavsar89

@patmill can you please provide guidance on how to update my existing project so that the fix gets applied? Thank you.

navarrorc avatar Feb 14 '19 17:02 navarrorc

You shouldn't have to do anything - as long as you didn't make temporary workarounds in your code. Are you still seeing issues?

patmill avatar Feb 14 '19 17:02 patmill

Yes I am. The code inside the this.context.navigatedEvent.add(...) Is firing exactly twice when navigating to a different page.

navarrorc avatar Feb 14 '19 17:02 navarrorc

OK, my bad. Apparently the fix isn't rolling until next week. But once it rolls, you shouldn't need to change anything. Sorry about the incorrect dates.

patmill avatar Feb 14 '19 17:02 patmill

No worries. Thank you for the information.

navarrorc avatar Feb 14 '19 18:02 navarrorc

Does anyone know if this has been released yet?

rganser avatar Mar 11 '19 16:03 rganser

@rganser It is active on my Personal Development Tenant which is set with the Targeted release for everyone (Get updates early for your entire organization).

On a Production tenant you would normally have it set to Standard release (Get update when we release them broadly). I haven't tested it in Production, but maybe someone else from Microsoft can confirm that it has been released broadly across all tenants.

navarrorc avatar Mar 11 '19 17:03 navarrorc

I just tested this again in my developer tenant (configured for targeted release) and I'm still seeing the issue. When navigating from one site-page to another the navigatedEvent handler fires exactly twice.

this.context.application.navigatedEvent.add(...)

@patmill the changes shouldn't require any SPFx updates right?

patrickabel avatar Mar 21 '19 21:03 patrickabel

Correct - they should not. Let me follow up with the owning group. Sorry about this.

patmill avatar Mar 21 '19 21:03 patmill

So, I spent a chunk of time trying to repro this, as did the owner, but we can't seem to see the issue. Can you double check that a) you aren't registering for the event twice and b) you aren't registering the extension twice?

patmill avatar Mar 22 '19 16:03 patmill

@patmill care to share your code? I'm not registering for the event twice nor am I registering the extension twice. I can re-check in the morning, but am still seeing the event trigger twice.

patrickabel avatar Mar 25 '19 02:03 patrickabel

Here's what I'm using to test @patmill. My console.log() is triggering twice each page change. Screenshot_37

export default class SpoAppInsightsApplicationCustomizer
  extends BaseApplicationCustomizer<ISpoAppInsightsApplicationCustomizerProperties> {

  @override
  public onInit(): Promise<void> {

    this.context.application.navigatedEvent.add(this, this.showAlert);

    return Promise.resolve<void>();
  }

  public showAlert(): void {
    console.log('navigated change...' + this.context.pageContext.web.serverRelativeUrl + '[' + Date.now() + ']');
  }
}

patrickabel avatar Mar 25 '19 19:03 patrickabel

OK, my bad. I got it happening again. Looking at the callstack, it appears to actually be worse than navigated getting fired twice. It looks like the entire app customizer is getting reinitialized on a navigate (which is kicking off the initial 'hey, you register for a navigated' event) - at least from my debug query string test. Looking in more.

update OK - noticed one thing, wondering if you can confirm. If I run my customizer w/ just the debug query string (testing it out), I'm getting double events. If I actually install and register it, I only get one event. Is that what you are seeing?

patmill avatar Mar 25 '19 20:03 patmill

@patmill good catch, it looks like the event duplication occurs while debugging but not once the SPFx extension is deployed and registered on the site.

@patmill I do have a follow-on question though regarding context. Is there a way I can 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.

patrickabel avatar Mar 26 '19 20:03 patrickabel

OK, good to hear that it is only while debugging. We'll sort that out as well, but it seems less of an emergency. There isn't a way to delay it, but a better way to find out what is rendering is to use the current context. That will be updated.

patmill avatar Mar 26 '19 20:03 patmill