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

Modern Web Part browse back JavaScript fails to trigger onInit

Open AltruisticNerd opened this issue 7 years ago • 10 comments

Note: Related Item #3095 was prematurely closed

Category

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

Expected or Desired Behavior

When browsing Back to a page, the JavaScript should run again when clicking back from an item in a Modern Web Part.
JavaScript should run on page load and again when navigating back from an Announcement or Calendar item.

Observed Behavior

JavaScript does not run when the page is returned to using the Back Browse feature (Backspace Button or Click to Go Back browsing arrow). This occurs on a Modern Communications Site when navigating back from a Calendar or Announcement item. I have tried mapping to the unload event with no luck.

The Standard apps provided in the spfx-40-fantastics (Rich Text Editor) and Script Editor web part solutions also exhibit this bug.

The result is that JavaScript solution developed using the onInit promise of the SPFx framework will fail under these circumstances.

Steps to Reproduce

This is reproducible using a new Communications Site and the Build your first SharePoint Framework tutorial documentation. https://docs.microsoft.com/en-us/sharepoint/dev/spfx/extensions/get-started/build-a-hello-world-extension

Create an announcement item or calendar item first. Now from the page where the SPFx Extension is running, click on the new Announcement or calendar item. Use the Back button (or hit backspace) to go back to the page. JavaScript will not trigger.

AltruisticNerd avatar Dec 18 '18 20:12 AltruisticNerd

So again - I don't think there is anything incorrect here. OnInit fires when the extension is initialized. It isn't initialized during partial-page navigates.

If you want to trigger something on a navigate, you should hook onto the OnNavigate event.

patmill avatar Dec 18 '18 22:12 patmill

Does SPFx offer any mechanism for running code in these scenarios?

I have tried binding to nearly every event in the DOM with zero success, but I haven't tried OnNavigate as this seems like a bad workaround (but I will test it tomorrow). Unfortunately all the spfx web parts currently built that use JS are failing because the documentation recommends using onInit which should almost NEVER be used on a modern site if this is the behavior. I'm actually quite surprised that you would consider this even remotely acceptable.

You either need to update the documentation so that it supports these modern web parts (which would be embarrassing if you are recommending onNavigate) or we need to ask MS to modify the web parts to use full page loads.

RRFreemanArchitect avatar Dec 19 '18 05:12 RRFreemanArchitect

It's unclear exactly what you are experiencing here from your text. Webparts and extensions are different components with different life cycles. A webpart should go through a full life cycle (init -> render -> etc.) on a partial navigate, an extension will likely not. OnInit is a lifecycle event on an SPFx component, and it will get fired at different times on different components (it get's fired when the component is initialized).

For an extension, hooking onto the SPFX onNavigate event is the way to track navigate events. The lifecycle of an extension is longer than that of a webpart (which only lasts for the duration of a single view).

It might help the discussion if you could explain what exactly you are trying to achieve. For example, in your first post on this bug, you state "JavaScript will not trigger". Which javascript won't trigger? What exactly are you expecting vs. what you are seeing.

patmill avatar Dec 19 '18 06:12 patmill

Neither web parts or the customizer works with the modern web parts. I've seen a lot of related issues (Partial Page loads) in this forum of people running into the same issues with no real solution.

I did update the test code to bind to a page placeholder (bottom) as in the Part 2 of the tutorial here: https://docs.microsoft.com/en-us/sharepoint/dev/spfx/extensions/get-started/using-page-placeholder-with-extensions

Then I run into another problem that the script runs more than once in partial page loads or not at all in some instances of partial page loads. I found that the Modern same-site Announcement pages on the Government cloud will not trigger events on the extension, and other Modern page back-browses trigger more than once. This is quite ridiculous.

Here is an example of the tutorial 2 code after clicking back and forth from a Calendar item. Note that on the second browse-back, it actually renders 2 additional bottom placeholders: multiple loading

AltruisticNerd avatar Dec 20 '18 02:12 AltruisticNerd

Quick question on this last point (extra placeholders). Do you by any chance have a site collection app catalog that the extension is registered from?

For your other situation, using the back/forward buttons I am unable to repro your problem (onInit not firing on a webpart). If the two pages are running the same extensions / placeholders, then the onInit of the extension won't fire, because the extension is not destroyed / created - the same instance continues to exist.

patmill avatar Jan 04 '19 22:01 patmill

This issue has been automatically marked as stale because it has marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within next 7 days of this comment. Thank you for your contributions to SharePoint Developer activities.

msft-github-bot avatar Jan 22 '19 19:01 msft-github-bot

I don't know if it helps but you can try to override de onDispose method. As far I remember, this method is triggered on the webPart on partial-page navigation

Barba76 avatar Jan 22 '19 19:01 Barba76

This issue has been automatically marked as stale because it has marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within next 7 days of this comment. Thank you for your contributions to SharePoint Developer activities.

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

Closing issue due no response from original author. If this issue is still occurring, please open a new issue with additional details. Notice that if you have included another related issue as additional comment on this, please open that also as separate issue, so that we can track it independently.

msft-github-bot avatar Feb 05 '19 21:02 msft-github-bot

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 25 '20 18:01 msft-github-bot