question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware: ReferenceError: document is not defined

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

14.x

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When you have middleware and a web worker, all pages crash from an error in the middleware chunk.

ReferenceError: document is not defined
    at /Users/kristo/d/circulation-patron-web/.next/static/chunks/webpack-middleware-0775957e405959fc.js:298:36
    at /Users/kristo/d/circulation-patron-web/.next/static/chunks/webpack-middleware-0775957e405959fc.js:386:12
    at /Users/kristo/d/circulation-patron-web/.next/static/chunks/webpack-middleware-0775957e405959fc.js:391:12
    at Script.runInContext (node:vm:139:12)
    at Script.runInNewContext (node:vm:144:17)
    at Object.runInNewContext (node:vm:298:38)
    at runInContext (/Users/kristo/d/circulation-patron-web/node_modules/next/dist/server/web/sandbox/context.js:69:33)
    at Object.run (/Users/kristo/d/circulation-patron-web/node_modules/next/dist/server/web/sandbox/sandbox.js:14:9)
    at Server.runMiddleware (/Users/kristo/d/circulation-patron-web/node_modules/next/dist/server/next-server.js:448:46)

This appears to originate from the following code in one of my webpack-middleware chunks:

/******/ 	/* webpack/runtime/jsonp chunk loading */
/******/ 	!function() {
/******/ 		__webpack_require__.b = document.baseURI || self.location.href;

The middleware function I added is as simple as possible:

// _middleware.ts
import { NextRequest, NextFetchEvent, NextResponse } from "next/server";

export default async function middleware(
  _req: NextRequest,
  _ev: NextFetchEvent
) {
  return NextResponse.next();
}

Reproduction

Here is a reproduction: https://stackblitz.com/edit/nextjs-v5luqm?devtoolsheight=33&file=pages/counter.js

Note:

  • If you comment out the worker-counter.js import and use the workerless-counter.js instead, it works.
  • If you remove the _middleware.js page, it works.
  • If you import the worker, and have the middleware, no page anywhere will work.

I’m not sure how this happens, but my guess is that when webpack detects a worker, it changes how it builds in a way that is incompatible with the edge runtime.

In my app it doesnt happen in development, only after a full build. In the repro, it also happens in dev. I also noted that the offending code is in the middleware chunk, and doesn’t appear when you don’t have the worker.

Expected Behavior

The middleware chunk should not be impacted by the web worker.

To Reproduce

https://stackblitz.com/edit/nextjs-v5luqm?devtoolsheight=33&file=pages/counter.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
OwenMelbzcommented, Jan 4, 2022

Same thing happening here after updating to v12 and trying to add Basic Auth protection.

Error was:

Unhandled Runtime Error ReferenceError: document is not defined

/* webpack/runtime/jsonp chunk loading */
/******/ 	!function() {
/******/ 		__webpack_require__.b = document.baseURI || self.location.href;
1reaction
rplotkincommented, Dec 27, 2021

Getting more specific:

/* webpack/runtime/jsonp chunk loading */
/******/ 	!function() {
/******/ 		__webpack_require__.b = typeof document !== 'undefined' && document.baseURI || typeof self.location !== 'undefined' && self.location.href;
/******/

That line, __webpack_require__.b, is NOT present when the Worker code is absent. Also, __webpack_require__.b is not utilized in the closure. It is a completely unnecessary line throwing the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js: document is not defined - Stack Overflow
I think, in server rendering mode, the document is undefined. You should be able to use it inside class lifecycle methods or useEffect...
Read more >
How to solve the document is not defined error - Flavio Copes
Here's how to fix the “referenceerror: document is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
Fixing Next js "ReferenceError: document is not defined"
The error is thrown because document is only available inside the browser and not on the server. Next js executes this code on...
Read more >
Solve “document is not defined” errors in Next.js | by WebTutPro
Because in the Node.js world, document is not defined, document is only available in browsers. There are three ways to fix that: 1....
Read more >
ReferenceError: document is not defined in JavaScript
This error usually occurs when the code is running in a non-browser environment, such as in a Node.js server-side application, and does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found