I discovered that office.js
does something to the window.history.replaceState
function. To get things working with the nextjs
dev server, I needed to create a _document.tsx
file like the one below, which patches the method after importing office.js
. This patch is not required for production as we are using the output: 'export'
build option with nextjs
. This was a quick fix - I'm unsure what edge cases may come up due to the patch.
import { Head, Html, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html lang='en'>
<Head>
<meta charSet="UTF-8"/>
<meta httpEquiv="X-UA-Compatible" content="IE=Edge"/>
{process.env.NODE_ENV !== 'production' &&
<script dangerouslySetInnerHTML={{
__html: `window._replaceState = window.replaceState`
}}/>}
<script type="text/javascript"
src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>
{process.env.NODE_ENV !== 'production' &&
<script dangerouslySetInnerHTML={{
__html: `window.history.replaceState = window._replaceState`
}}
/>}
</Head>
<body>
<Main/>
<NextScript/>
</body>
</Html>
);
}