Open aaronpowell opened 5 years ago
Doing my own digging into the problem, the issue is with the way the AjaxMonitor
dependency works, specifically the following lines:
Line 98 which fails because the line does a test of window && ...
, which will fail during SSR since window
isn't defined
Line 325 which fails because it expects XMLHttpRequest
to be globally defined, but isn't during SSR.
Line 584 which fails because it expects window
to be a global.
The workaround for this would be to use the DefinePlugin
plugin for webpack. This is doable with Gastby, by updating the gastby-node.js
file like so:
exports.onCreateWebpackConfig = ({stage, plugins, actions}) => {
if (stage === 'build-html') {
actions.setWebpackConfig({
plugins: [
plugins.define({
window: undefined,
XMLHttpRequest: undefined
}),
],
})
}
}
This will define window
and XMLHttpRequest
but define them as undefined
so that the tests will pass and anywhere that's doing a typeof window === 'object'
test will still fail since typeof undefined === 'undefined'
.
@aaronpowell Hi Aaron, is this issue still relevant based on the changes introduced in #1167 to support a generic getGlobal() lookup method?
@MSNev I'll have to have a test and see if I can still repo (assuming that #1167 is in a released npm package).
It's been included since v2.4.1
This appears to still be an issue in 2.5.5. I believe this is because the library is using a helper function to check whether or not something is undefined, instead of checking typeof
at the call sites (example). This results in the code throwing an exception (due to referencing an undefined value) before the helper code can run.
@MSNev I have put together a sample Gatsby app demonstrating these errors: https://github.com/jasonnutter/appinsights-gatsby
For other Gatsby users encountering this bug, I was able to workaround this by using the workaround from @aaronpowell above along with putting the invocation of loadAppInsights()
in my app behind a typeof window !== "undefined"
check.
It should indeed be using typeof to avoid the JS strict mode checking, just as is done in the dependencies @ https://github.com/microsoft/ApplicationInsights-JS/blob/master/extensions/applicationinsights-dependencies-js/src/ajax.ts#L54
@jasonnutter came across this issue. If you loadAppInsights()
in gatsby-browser.js you avoid that window-problem aswell 😄
Description/Screenshot
I'm using Gatsby to create a static website but when I attempted to generate the static HTML files from the React components the Server Side Rendering (SSR) fails in AppInsights:
Steps to Reproduce
Run
npm run build
to generate the static outputExpected behavior
Gatsby is able to generate a set of static files.
Additional context
You'll find a demo that the errors can be repo'ed in on my GitHub. Gatsby works fine when run using the development webserver.