Polyfill XMLHttpRequest on the server #17404
-
Hey everyone, I have some data layer code that uses the global XMLHttpRequest object to do the queries, and I need to use this code on the server side with I tried using the const xhr = require("xmlhttprequest");
global.XMLHttpRequest = xhr.XMLHttpRequest; When starting the dev server, the app doesn't crash anymore regarding a missing Does anyone know how to polyfill this the right way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think you'd want to include this import xhr from 'xmlhttprequest';
export async function getStaticProps(ctx) {
global.XMLHttpRequest = xhr.XMLHttpRequest;
} Also if you are able to I'd recommend switching all xhr requests to use |
Beta Was this translation helpful? Give feedback.
I think you'd want to include this
global.XMLHttpRequest = xhr.XMLHttpRequest;
inside yourgetStaticProps
function call.Also if you are able to I'd recommend switching all xhr requests to use
fetch
. Next polyfills this on the server by default https://nextjs.org/docs/basic-features/supported-browsers-features#server-side-polyfills