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.

2 Likes

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.

1 Like