4

Our team wants to build a documentation website that can be opened in browsers and Excel add-ins. We chose Docusaurus V2 as the main framework to build the documentation website, and embedded office.js in it.

Office.js deletes history.pushState and history.replaceState APIs after being loaded, so I added some JS code to polyfill it, as follows:

<html>
  <head>
    ... ...
    <script type="text/javascript">
      if (history) {
        var pushStateRef = history.pushState;
        var replaceStateRef = history.replaceState;
      }
      function patch() {
        if (history && !history.pushState) {
          history.pushState = pushStateRef;
          history.replaceState = replaceStateRef;
        }
      }
      function onOfficejsLoad() {
        Office.onReady(function() {
          console.log('office.js is ready.');
          patch();
        });
      }
    </script>
    <script
      type="text/javascript"
      src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
      onload="onOfficejsLoad();"
    ></script>
  </head>
</html>

The above code made the website to work well within our add-in in Excel Online in Chrome, Safari, as well as IE 11. However, it did not work well in Excel for Windows: when we clicked to tigger a router event, e.g. clicking on docusaurus' sidebar, there was a error, the router had no effect, and the sidebar did not work well (see Screenshot).

I managed to fix this error by adding the loading of history.js:

<html>
  <head>
    ... ...
    <script
      type="text/javascript"
      src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
      onload="onOfficejsLoad();"
    ></script>
    <script
      nomodule
      type="text/javascript"
      src="https://cdnjs.cloudflare.com/ajax/libs/html5-history-api/4.2.10/history.js"
    ></script>
  </head>
</html>

I still post the question, because I don't understand why the previous version did work our in add-in in Excel Online IE 11, but not in Excel for Windows, shouldn't their behaviors the same? Most importantly, when developing Excel add-ins, is there any best practice to follow to manage the conflit of history.pushState and office.js?

Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
BurdenBear
  • 71
  • 4

1 Answers1

0

I added some JS code to polyfill it

I think what you have done is what I would have done too. I don't think Office.js is right to delete/override the history methods, but perhaps they had good reasons to do so (e.g. only allowing full page refreshes).

However, it did not work well in Excel for Windows

Do you know what browser is being used in Excel for Windows? It could be an entirely different browser that doesn't conform to the standards/runs in a different environment (e.g. not all the HTML5 APIs are provided on the window object). That could be why there's the weird behavior.

Sorry I don't have a Windows machine to debug this issue.

Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
  • There are Edge Chromium, Edge legacy, IE 11 three choices for different versions for Office desktop. Mostly IE 11. – ttimasdf Jun 01 '21 at 08:49