Microsoft tried to fix (unbreak) this1 (thank you Michael Zlatkovski), but sadly had to roll it back2 as the fix caused unexpected issues. They're going to try adding it as an opt-in.
In the meantime: Since they do this by assigning null
to history.pushState
and history.replaceState
, which shadows the inherited methods with null
properties, the easiest way to undo it is to remove those null
properties:
delete history.pushState; // Remove 'own' property with `null`, reveals original inherited one
delete history.replaceState;
(If you're using TypeScript, you'll have to disable the error you get on that — the properties aren't optional, so TypeScript won't let you remove them. It doesn't know we're not really removing them.)
Removing the own properties that were added via assignment reveals the inherited original properties.
It may seem counter-intuitive to remove something to restore it, but Office.js isn't removing them, it's just shadowing (hiding) them, akin to this:
class FakeHistory {
pushState() {
console.log("Hi, I'm `pushState`");
}
replaceState() {
console.log("Hi, I'm `replaceState`");
}
}
const history = new FakeHistory();
console.log("Quick test of the 'original' methods:");
history.pushState();
history.replaceState();
console.log("...'Remove' them the way Office.js does...");
history.pushState = null;
history.replaceState = null;
console.log("Can't use them anymore:");
try {
history.pushState();
history.replaceState();
} catch (error) {
console.error(error);
}
console.log("Restore them by getting the own properties out of the way:");
delete history.pushState;
delete history.replaceState;
console.log("We can use the 'original' methods again:");
history.pushState();
history.replaceState();
.as-console-wrapper {
max-height: 100% !important;
}
1 (link):
gergzk commented Feb 29 2024 5:44 p.m. GMT
Good news! We've recently made the fix discussed above (just removed the overrides). Expect to see it roll out in the newest office.js versions in about a week or two.
2 (link):
nikhilatsap commented Apr 2, 2024, 11:52 AM GMT+1
Hey @gergzk this error is still there in the office.js hosted at /lib/1 CDN url
gergzk commented Apr 2, 2024, 11:35 PM GMT+1
@nikhilatsap - yes, we had to roll back the fix because it had other unwanted side effects. We'll look into adding it as an opt-in.