Skip to content

Commit

Permalink
update to address stale pages pnp#2186
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-rodgers committed Mar 28, 2022
1 parent 14eeb44 commit 6cd50c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
11 changes: 6 additions & 5 deletions debug/serve/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { spfi, SPBrowser } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/webs";
import { getRandomString } from "@pnp/core";
// import { graph } from "@pnp/graph/presets/all";

Expand Down Expand Up @@ -29,11 +32,9 @@ document.onreadystatechange = async () => {

const sp = spfi("https://318studios.sharepoint.com/sites/dev/").using(SPBrowser());

// const r = await sp.web.update({
// Title: "New Title: " + getRandomString(4),
// });

const r = await sp.web();
const r = await sp.web.lists.getByTitle("Generic").items.add({
Title: "Added"
});

html.push(`<textarea cols="200" rows="40">${JSON.stringify(r, null, 4)}</textarea>`);

Expand Down
18 changes: 8 additions & 10 deletions packages/sp/behaviors/request-digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ interface IDigestInfo {
value: string;
}

function clearExpired(digest: IDigestInfo | null | undefined): IDigestInfo | null {
const now = new Date();
return !objectDefinedNotNull(digest) || (now > digest.expiration) ? null : digest;
}

// allows for the caching of digests across all calls which each have their own IDigestInfo wrapper.
const digests = new Map<string, IDigestInfo>();

Expand All @@ -28,18 +33,11 @@ export function RequestDigest(hook?: (url: string, init: RequestInit) => IDigest
const webUrl = extractWebUrl(urlAsString);

// do we have one in the cache that is still valid
let digest: IDigestInfo = digests.get(webUrl);
if (digest !== undefined) {

const now = new Date();
if (now > digest.expiration) {
digest = null;
}
}
// from #2186 we need to always ensure the digest we get isn't expired
let digest: IDigestInfo = clearExpired(digests.get(webUrl));

if (!objectDefinedNotNull(digest) && typeof hook === "function") {
// we assume anything we get from the hook is not already expired
digest = hook(urlAsString, init);
digest = clearExpired(hook(urlAsString, init));
}

if (!objectDefinedNotNull(digest)) {
Expand Down

0 comments on commit 6cd50c4

Please sign in to comment.