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.

Nextjs Middleware doesn't work with webpack URL assets ( `new URL(..., import.meta.url)` ) , `ReferenceError: document is not defined`

See original GitHub issue

What version of Next.js are you using?

12.0

What version of Node.js are you using?

v14.17.5

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

vercel

Describe the Bug

When using middleware, got error ReferenceError: document is not defined

Here’s the code: https://github.com/quran/quran.com-frontend-next/blob/e64f1200a17ed162a394447898d4f729d8851a36/src/pages/test-geo/_middleware.ts

/* eslint-disable import/prefer-default-export */
import type { NextRequest } from 'next/server';

export function middleware(req: NextRequest) {
  const { geo } = req;

  return new Response(JSON.stringify({ message: 'hello world!', geo }), {
    status: 200,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

Here’s the API URL: https://quran-com-95pgbz4dx-qurancom.vercel.app/test-geo

And the vercel log is here : https://vercel.com/qurancom/quran-com/5byW9AFxDFa93J9AUtGS97PJR8FV/functions

We tried to pinpoint the issue, turns out the problem is here https://github.com/quran/quran.com-frontend-next/blob/0d120d9e44e590702cd8f58b3a99b110af390eff/src/hooks/useTarteelVoiceSearch.ts#L180

new AudioWorklet(new URL('src/audioInput/MicInputProcessor.ts', import.meta.url)),

The problem is related to import.meta.url https://webpack.js.org/guides/asset-modules/#url-assets

Expected Behavior

no error

To Reproduce

The repo is open source

  • This is the reference commit

  • Install the dependencies

  • yarn dev

  • go to localhost:3000/test-geo

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
javivelascocommented, Jan 14, 2022

This was fixed by https://github.com/vercel/next.js/pull/33134 and released in 12.0.8

0reactions
balazsorban44commented, Feb 13, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

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 >
Solve “document is not defined” errors in Next.js | by WebTutPro
You can import your Scroll component using dynamic imports and the srr: false option. This way your component won't even be rendered on...
Read more >
next.config.js: Environment Variables
Learn to add and access environment variables in your Next.js application at build time. ... variables won't work due to the nature of...
Read more >
Resolve | webpack
Configure how modules are resolved. For example, when calling import 'lodash' in ES2015, the resolve options can change where webpack goes to look...
Read more >
Asset Bundling (Vite) - The PHP Framework For Web Artisans
Have you started a new Laravel application using our Vite scaffolding but need to move back to Laravel Mix and webpack? No problem....
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