Error rendering widget from Next.js app: "InvariantError: Invariant: Expected document.currentScript to be a <script> element. Received [object HTMLScriptElement] instead."

We are building our ChatGPT app using Next.js. I cannot include a link in this post, but it’s from the /vercel-labs/chatgpt-apps-sdk-nextjs-starter repo on Github.

When the widget is being rendered, we are intermittently experiencing the following error, and the widget fails to load:

Uncaught (in promise) InvariantError: Invariant: Expected document.currentScript to be a <script> element. Received [object HTMLScriptElement] instead. This is a bug in Next.js.

The error is thrown here, during the Next.js app bootstrapping process. I cannot include links in this post, but it’s in /packages/next/src/client/asset-prefix.ts#L6-L10

Sometimes when reloading the widget will render fine. Other times, the same error occurs and it fails to load.

I wonder if there is some kind of problem with the way the sandbox is rendering the HTML document?

I’ve filed a ticket with the repo at /vercel-labs/chatgpt-apps-sdk-nextjs-starter/issues/19 as well.

Experiencing the same. I got started with Next.js in ChatGPT using the same template from Vercel..

The tool call happens, the mcp server works correctly and process the tool input. The widget gets stuck in load, and I never get the confirmation modal.

When this happens I can see in the browser console the exact same error.

Has anyone found a solution to this? Going through the same problem now. What I noticed is that if I ask in the chat to refresh the widget then it works. I’m running also a build with a fixed assetPrefix in my next.config pointing to the host where my build is hosted.

I put together a patch that I believe helped the problem. I can’t post links here, but you may find it on GitHub at

/vercel-labs/chatgpt-apps-sdk-nextjs-starter/issues/19#issuecomment-3757667335

Let me know if it works for you.