overridden dispose() method triggered on page edit
This is a bug I'm getting with recent SPFx updates
Category
- [ ] Question
- [ ] Typo
- [x] Bug
- [ ] Enhancement / Suggestion
- [ ] Additional article idea
Expected or Desired Behavior
Override the dispose() method to run code when a web part is deleted. For example:
protected dispose(): void {
// cleanup code when web part is deleted
}
Observed Behavior
A recent update to SPFx calls the overridden dispose() method when the edit page button is clicked on modern pages.
Thanx for the info. Will review and get back.
@manishgarg1 I can see in the source comments that the dispose() API will be deprecated in favor of an API named onDispose(). Also in the comments it is noted that the onDispose API "is expected to be called in scenarios like page navigation i.e. the host is transitioning from one page to another and disposes the page that is being transitioned out." That wasn't the behavior in Drop 5. In drop 5 dispose() was called when the web part was DELETED from a page. Perhaps there is need for an onDelete() or onRemove() API that is called when a web part is removed?
@manishgarg1 There is still no way to intercept a web part delete event. We need this to cleanup file system resources when a web part is deleted.
@jaredbuttars could you tell more what file system resources you would like to clean up on web part deletion?
@waldekmastykarz several of my web parts allow users to upload files to the site. The files themselves are for specific use by a single instance of a web part on a page. It is logical in this use case that if the web part instance was deleted from a page all of the files uploaded through it would also be deleted. At present I can't intercept that delete from page event. If I could through some sort of onDelete override then I could easily write the bits to cleanup files associated with that web part instance.
The challenging part is that cleaning up resources might take a while and it's definitely not something you should want to do in browser. Having something like a webhook would make more sense, but as you've noticed there is currently no onDelete event. Have you tried looking into the web part/web part manager API to see if there are no events that you could subscribe to?
@jaredbuttars, there are two types of delete events:
-
Page navigation: when the page tells the web part manager to cleanup all the current web part related context. This is the case we have addressed with the onDispose() API. In this case you may have DOM handles and other client side objects that you may want to clean up. This is primarily to avoid client side memory leaks.
-
Deletion of a web part from a page: we discussed a lot for an API for this scenario. Multiple potentially complicated scenarios came up. Modern pages are stored as list items. What would you do when the page is deleted directly from the list? The web part data would be left hanging. What if you delete the web part from the page data through an API? And so on... The only scenario we can cover at this point is the delete button on the web part toolbar. Would you want an API just for that one scenario and is it ok if all other scenarios are left out?
@manishgarg1
I can see how it would become complicated to cover all delete scenarios. Even still, an API just for the one scenario when a web part is deleted from a page would be extremely useful.
Added this to User Voice.
https://sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/suggestions/19261525-ondelete-method
Covered by user voice. Closing this entry.
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