sp-dev-docs
sp-dev-docs copied to clipboard
Form digest expiration in SPFx service
Target SharePoint environment
SharePoint Online
What SharePoint development model, framework, SDK or API is this about?
💥 SharePoint Framework
Developer environment
Windows
What browser(s) / client(s) have you tested
- [ ] 💥 Internet Explorer
- [X] 💥 Microsoft Edge
- [ ] 💥 Google Chrome
- [ ] 💥 FireFox
- [ ] 💥 Safari
- [ ] mobile (iOS/iPadOS)
- [ ] mobile (Android)
- [ ] not applicable
- [ ] other (enter in the "Additional environment details" area below)
Additional environment details
-
browser version 104.0.1293.70
-
SPFx version 1.15.2 (also noted in 1.15.0)
-
Node.js version v16.15.1
I'm using a docker dev container to build and serve the SPFx solution.
Describe the bug / error
The problem occurs when using an SPFx service in an SPFx command set extension. The SPFx service consumes the page context and retrieves the Form Digest to be able to call SharePoint endpoints using POST requests. When leaving the page open for more than an hour, and refreshing the page, the form digest will not be retrieved appropriately. You'll get an expired Form Digest. This will lead to 403 errors on post requests.
I've documented the behavior with a sample solution, and gif file here:
https://github.com/martinlingstuyl/spfx-requestdigest-issue
Originally, I filed it with the crew of PnP.js. They are also saying it's an SPFx bug, not a pnp.js bug. See the conversation here: https://github.com/pnp/pnpjs/issues/2379
Steps to reproduce
- Create an SPFx command set extension
- Create an SPFx service that consumes the PageContext to retrieve the Form Digest value.
- Use the SPfx service in the Command set extension to show or use the form digest.
- Wait an hour with the browser open
- Refresh the page
- The form digest that's being retrieved will be an old digest.
Expected behavior
I'd expect to always get a newed-up form digest after refreshing the page.
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
@martinlingstuyl - sorry for the delay. I can confirm the issue and we will be looking into it.
Awesome! Great to hear it @AJIXuMuK!
@martinlingstuyl - so, so update here. The issue will be fixed, but it will take time. The problem is in Service Worker used by Lists and Libraries. It will be updated but in the first half of the next year. So for now, I believe the only workaround here is to check the digest value, check the expiration and request it if needed.
But we WILL fix it. It won't just happen right away unfortunately.
That's ok, I've got my workaround in place...thanks for looking into it and getting back to me on it.