I’m using rescript-requesthttps://github.com/bloodyowl/rescript-request to issue XMLHttp requests in my Vercel site. Unfortunately, Vercel attempts to aggressively use server side rendering and XMLHttp has no implementation in node.js.
As a result, I get the following error:
Error occurred prerendering page "mypage". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: XMLHttpRequest is not defined
In particular, I don’t seem to have control over where the underlying rescript-request module gets imported and therefore am not sure how to annotate that import with dynamic.
Are you thinking of dynamically loading your page component ("mypage")? I don’t get the rationale between XMLHttpReqeust is not defined and Advanced Features: Dynamic Import | Next.js.
I believe that the XMLHttpRequest is not defined error comes from the attempted pre-rendering of rescript-request requests (XMLHttpRequest is not available in node). This error only occurs if I refresh the page, but doesn’t occur if I switch to a page that wants to issue XMLHttpRequest.
The way to shut down SSR rendering in next.js is using dynamic imports for relevant libraries (in this case rescript-request) to make sure that they are not used server side.
To answer your question, I guess I don’t need the whole component to be dynamically loaded (some of it may be pre-rendered successfully) as long as anything that uses rescript-request is dynamically loaded.
Not sure why the second one does not work for you. Perhaps, some slight nuances or differences in next.js versions? I just copy-pasted it from the code used in production. Requires some debugging.
And here’s a bonus snippet. The same as above, but shows a progress bar so that the page loading process has better UX: