Navigating from a page to another page causes application state issues
Category
- [ ] Question
- [ ] Typo
- [X] Bug
- [ ] Additional article idea
The bug described below makes SharePoint framework development pretty tedious as you cannot be sure how many instances of your webpart exists on a page. When developing webparts with shared components/data, the bug described here makes development really hard.
The bug can also be shown with out of the box modern webparts, thus reproducing steps and observed/expected behaviors are described without any custom functionalities. And this doesn't seem to only affect the top navigation bar.
If this is not a bug and expected behavior, it requires a lot hacks to create complex application.
Expected or Desired Behavior
Within a standard Communications Site, if you add any webpart to a page and navigate to another page from the top navigation, all webparts within the page you were leaving should be disposed and not saved in memory. Navigating to a new page should only create one instance of a webpart. Navigating to a new page from the top navigation bar should provide a similar application state as in browsing to the page via providing an URL.
In this example I'm just using a standard Document library webpart.
React components on a page with a single webpart, when refreshing a page or providing the URL of the page manually in the browser address bar:

Observed Behavior
When navigating to a new page from the top navigation bar, all the webparts from the navigating page are kept in memory and all webparts on a page are instantiated 3 times, though you can only see the latest instance of said webpart.
React application state when navigating to a page with a single webpart from an empty home page through a link from top navigation:

Steps to Reproduce
Prerequisites: -Have React development tools extension installed -Latest Chrome browser (tested also with latest FireFox)
- Create an empty Communications site
- Edit the landing page and add a standard Document library webpart to the page.
- Publish the page
- Create a new page
- Edit the new page and add a standard Document library webpart to the page.
- Publish the page
- Edit the top navigation bar and add the newly created page to the navigation bar.
- Navigate to the Home page from the navigation bar
- Navigate to the newly created page from the navigation bar
- Open Chrome devtools and watch the React components while continuing navigating between the pages from the top navigation bar
Worst case scenario - navigating back and forth 10 times - both pages contain a single webpart:

Potentially the same issue as in https://github.com/SharePoint/sp-dev-docs/issues/1464.
In my custom webparts, I can notice that after navigation event the onInit() function of a single webpart is run three times, thus giving me the idea of three different instances. Also the render() function is run three times, with this.renderedOnce being always false.
I've just been debugging an issue with a custom SPFX web part we have deployed to our production tenant, and was just about to log exactly the same issue.
In my case, the custom SPFX web part is on the welcome page of the site. Using @waldekmastykarz's approach of adding source maps to production packages, if I put a break point in the onInit function in my web part's WebPart.ts file, on a hard refresh of the page the break point is hit once. However, if you then click on the site logo to redirect to the same (welcome) page, the break point is triggered twice.
This is clearly an issue between the SPFX event model and the partial loading when clicking between "modern" SharePoint pages. The web part should only be initialised once, for both hard refreshes and modern page redirection.
@VesaJuvonen I've not had any luck in my attempts to find a workaround for this issue. As this is breaking our production sites right now and users are unable to use the application, can you please provide us with a code snippit that we can use in our web parts to resolve the issue until a proper fix is rolled out?
Thanks for your feedback.
In order to provide a bit of context, web parts are initialized per instance. That means if you have 2 instances of the same web part in a page, that web part will be initialized twice. Consider that if part of the initialization code depends on the properties, it would not be the same. That also applies to the navigation across pages. What the framework does in this case is dispose the old web part instances and create new ones.
There was a missing point in the general guidance on building web parts with React that caused this issue with the non-visible React elements. That is what to do when disposing web parts.
We have updated the yeoman generator (available starting 1.5.0) to include
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
as part of the generated TypeScript. This ensures that when the web part gets disposed, React stops tracking the code inside. Non-React web parts would not need this code at all.
For the built-in web parts, the different teams are working on changing this behavior, and this will roll out in the following weeks.
Thank you @mpasarin, that has resolved the issue for my custom web part!
Do you need to be using 1.5.0 to make this work? I am on 1.4.1 and I tried adding the onDispose() and it doesn't appear to ever get called.
Actually I can see that it does call the onDispose(). That doesn't seem to help though as I still see multiple copies of the webpart go through their render(). Each time the renderedOnce property is false too. This is causing a lot of issues because it causes high memory usage especially in IE11.
I have tried webpart properties like "renderedOnce","firstrender", however the issues is still prevalent. The webparts are loaded twice in case if page is navigated using Navigation and multiple times when the properties are edited.
There seems to be similar issues with SingleWebPartAppPage pagelayout.
Using a modern team site and left side navigation as links.
When navigating out of a singlewebpartapppage the onDispose() function is not called at all. If navigating to another singlewebpartapppage, rendering of the new component doesn't work.
When navigating to a singlewebpartapppage from an article page, the webpart in the page is initialized twice.
I understand as the singlewebpartapppage is still in preview.
Seems that navigation between article pages, by initializing the correct amount of webparts and correct disposal seems to work about ok. Some weird issues, but cannot yet reproduce easily to say exactly.
OK, I'm super late to the party here. Sorry about that. I can totally repro the "fast nav runs onInit 3 times" and that (to me) is wrong. I'll follow up with the fast-nav folks.
What is the proposed way of dealing with this onprem (SP2019)? We also had that bug on SPO a while ago, but there it seems to be fixed.
So first, this bug should be closed for SPO. For on-prem, you should open up a service request.
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