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

SPFX extension navigation events bug on SharePoint sites with latest features

Open SinghGurd opened this issue 7 years ago • 7 comments

We use SPFX extension ApplicationCustomizer to display breadcrumbs in top header and this is working very well. However, we have noticed navigatedEvent doesn't fire on SharePoint sites with latest features e.g. where a search box appears in middle of top header.

This issue is having serious impact on our users as breadcrumbs displayed in top placeholder aren't updating when users browse from one library to another. Please fix this otherwise clients are going to start losing trust in these solutions.

Below is a screenshot of top header taken from a SharePoint modern group connected site where this issue is occurring.

SPO-New-Header

SinghGurd avatar Jun 24 '19 03:06 SinghGurd

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

msft-github-bot avatar Jun 24 '19 03:06 msft-github-bot

Can you share some code that demonstrates how this works and isn't working to help with reproducing the issue?

andrewconnell avatar Jun 24 '19 11:06 andrewconnell

Please find the code below...Again this works fine for some modern sites and problem only manifests itself for a site where 'search box' appears in the middle of header as per previous screenshot.

import * as React from "react";
import * as ReactDom from "react-dom";
import { override } from "@microsoft/decorators";
import { Log } from "@microsoft/sp-core-library";
import {
  BaseApplicationCustomizer,
  PlaceholderContent,
  PlaceholderName
} from "@microsoft/sp-application-base";
import { Dialog } from "@microsoft/sp-dialog";

import * as strings from "SiteNavApplicationCustomizerStrings";
const LOG_SOURCE: string = "SiteNavApplicationCustomizer";

/**
 * If your command set uses the ClientSideComponentProperties JSON input,
 * it will be deserialized into the BaseExtension.properties object.
 * You can define an interface to describe it.
 */
export interface ISiteNavApplicationCustomizerProperties {
  message: string;
}

//https://www.eliostruyf.com/things-to-check-in-your-spfx-application-customizer-after-page-transitions/
interface NavigationEventDetails extends Window {
  isNavigatedEventSubscribed: boolean;
}
declare const window: NavigationEventDetails;


/** A Custom Action which can be run during execution of a Client Side Application */
export default class SiteNavApplicationCustomizer
  extends BaseApplicationCustomizer<ISiteNavApplicationCustomizerProperties> {
  private topPlaceholder: PlaceholderContent | undefined;

  @override
  public onInit(): Promise<void> {
    Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);

    //Subscribe to navigatedEvent only once
    if (!window.isNavigatedEventSubscribed) {
      this.context.application.navigatedEvent.add(this, () => {
        this.startReactRender("navigatedEvent");
      });
      window.isNavigatedEventSubscribed = true;
    }

    this.renderPlaceHolders();
    return Promise.resolve();
  }


  private renderPlaceHolders(): void {
    console.log("SiteNavApplicationCustomizer.renderPlaceHolders");

    // Handling the top placeholder
    if (!this.topPlaceholder) {
      //Create a new topPlaceholder element
      this.topPlaceholder = this.context.placeholderProvider.tryCreateContent(
        PlaceholderName.Top, { onDispose: this._onDispose }
      );

      // The extension should not assume that the expected placeholder is available.
      if (!this.topPlaceholder) {
        console.error("The expected placeholder (Top) was not found.");
        return;
      }

      //Render React component
      this.startReactRender("renderPlaceHolders");
    }
  }

  private startReactRender(message) {
    console.log("startReactRender", message);

    //Exit if topPlaceholder element does not exist
    if (!this.topPlaceholder || !this.topPlaceholder.domElement) {
      return;
    }

    //Delay rendering of react component      
    let self = this;
    setTimeout(() => {
      self.createReactElement();
    }, 200);
  }

  //ListId and ItemId properties are there to detect navigation change and refresh the mounted component. 
  private createReactElement() {
    console.log("createReactElement");
    const element: React.ReactElement<ISiteNavContainerProps> = React.createElement(
      SiteNavContainer,
      {
        context: this.context,
        listId: this.context.pageContext.list && this.context.pageContext.list.id ? this.context.pageContext.list.id : null,
        itemId: this.context.pageContext.listItem && this.context.pageContext.listItem.id ? this.context.pageContext.listItem.id : null
      }
    );
    ReactDom.render(element, this.topPlaceholder.domElement);
  }

  private _onDispose(): void {
    console.log('[SiteNavApplicationCustomizer._onDispose] Disposed custom top and bottom placeholders.');
    this.context.application.navigatedEvent.remove(this, this.startReactRender);
    window.isNavigatedEventSubscribed = false;
  }
}

SinghGurd avatar Jun 24 '19 21:06 SinghGurd

Further to above...below is the console log when user clicks links on quick launch.

/**
 *	Start of log when 'Documents' link is clicked in quick launch
 *	Entire page is loaded
 */
Initialized ClientSideExtensionHelper
spofiles-mini-340e7fde.js:1 Defined clientSidePlaceholders: DialogContainer,Top,Bottom
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 startReactRender navigatedEvent
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 SiteNavApplicationCustomizer.renderPlaceHolders
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 startReactRender renderPlaceHolders
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 createReactElement

**
 *	Log when 'Pages' link is clicked in quick launch
 *	Partial page load where pages library is loaded
 */
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 startReactRender navigatedEvent
site-nav-application-customizer_2ca9895902343e0452404c0d5304c620.js:15 createReactElement

**
 *	No further events are raised when 'Documents' link is clicked again
 *	Partial page loads where Documents library is loaded.
 */

This error appears in log every time user clicks from Pages to Documents and vice-versa. spo-error

SinghGurd avatar Jun 25 '19 05:06 SinghGurd

same as #4054 which is being fixed soon

ahackettms avatar Oct 08 '19 22:10 ahackettms

According to MS eng, it's a dupe of another issue so flagging as such. Monitor #4054...

andrewconnell avatar Dec 06 '19 17:12 andrewconnell

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 24 '20 06:01 msft-github-bot