The specialized middleware in Next.js gets dispatched to the browser

Attempting to retrieve user information based on the current session, I created a small _middleware.ts file. In order to get the users' data, I imported a prisma (ORM) client. However, upon loading the page in the browser, an error message appeared:

Error: PrismaClient is unable to be run in the browser.

I was under the impression that the _middleware would only run in the browser, so I'm surprised that it's being sent to the client as well.

Is there a way to address or prevent this issue from occurring?

Edit:

One potential solution I came across involves configuring webpack to exclude the "middleware" from the front-end bundle. However, this feels like a workaround and may introduce other bugs.

Edit2:

In my research, I also discovered the following command in next.js:

// Errors from the middleware are reported as client-side errors
// since the middleware is compiled using the client compiler

Could the use of the client compiler for compilation be causing the problem?

Answer №1

In the case that solely concerns PrismaClient, you have the option to handle it this way:

if (typeof window === "undefined") {
.. initialize prisma
}

It is advisable not to alter the default behavior of Next.js for ease of maintenance and future upgrades.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

setting the minimum date for a datepicker

Does anyone know how to set the minimum date for a calendar to be 2 days from the current date? For example, if today is the 27th, the minimum date should be the 29th. Any suggestions? Thanks. https://i.sstatic.net/7yHhH.png #html code <mat-form-field ...

An issue has occurred: Error - The specified NgModule is not a valid NgModule

Everything was working smoothly, but suddenly I encountered this error message while running ng serve. I haven't made any recent upgrades or changes to dependencies. Could someone please provide guidance on how to resolve this issue? ERROR in Error: ...

Guide on setting up @types from an NPM module containing the "@" symbol in its title

Installing the node package is easy npm install @gamestdio/timer --save But when attempting to add the corresponding types npm install @types/@gamestdio/timer --save An error occurs Invalid package name "@types/": name can only include URL-friendly ch ...

Retrieving a file URL from Sanity within a blocks array

I've been working on a website with Next JS and using Sanity as the CMS. While Sanity has schemas for images, I ran into an issue when trying to handle videos. The documentation recommends using GROQ queries to convert file URLs at the request level, ...

result of stitchSchema is null

After stitching two schemas together and running it on localhost to make a query, I noticed that the data in the second schema is returning null. The reason for this is unclear to me. The code below is used to combine remote schemas and run a localhost gr ...

The Next.js application is functioning smoothly in development, but encounters errors during the building and deployment processes

While my Next.js app compiles and runs smoothly locally during development (using npm run dev), I encounter a failed build when attempting to compile the project (using npm run build). After researching online, it seems that unhandled promises may be the c ...

What is the best way to create tests for pages middleware in Next.js version 12?

I have recently implemented some logic in my pages middleware using Next 12 and now I am looking to write tests for it. However, I am unsure of where to start. Can anyone point me towards a tutorial or resource that provides a comprehensive example of test ...

Dealing with 404 errors in both server.js and the client side utilizing NextJS and next-routes

I'm in search of a more elegant solution for my next-routes to gracefully handle file not found (specifically route not found) when executing from server.js (server side) and also while operating on the client side with Link. The current solution I ha ...

Tips for updating the border color of a button:

Struggling to alter the border color of a button Attempting borderColor attribute proved futile: borderColor: '#FFFFFF' Anticipated result Code snippet: headerBtn: { backgroundColor: 'black', fontSize: '16px', f ...

Filtering nested arrays in Angular by cross-referencing with a navigation menu

In the legacy application I'm working on, we have a navigation menu along with a list of user roles. Due to its legacy nature, we have accumulated a significant number of user roles over time. The main goal is to dynamically display the navigation me ...

I can't figure out why I'm getting the error message "Uncaught ReferenceError: process is not defined

I have a react/typescript app and prior to updating vite, my code checked whether the environment was development or production with the following logic: function getEnvironment(): "production" | "development" { if (process.env.NODE_E ...

Uncover the solution to eliminating webpack warnings associated with incorporating the winston logger by utilizing the ContextReplacementPlugin

When running webpack on a project that includes the winston package, several warnings are generated. This is because webpack automatically includes non-javascript files due to a lazy-loading mechanism in a dependency called logform. The issue arises when ...

Error Encountered with vscode.workspace.workspaceFolders. Troubleshooting VS Code Extensions

While developing an extension for vscode, I encountered an error that I'm having trouble resolving. The goal is to create a script that generates a new file based on user inputs, but I'm running into an issue when trying to retrieve the path for ...

Using const enums across multiple files with react-scripts-ts

Within our project, we have two distinct subprojects (backend and frontend) that are compiled independently. The frontend utilizes react-scripts-ts, so it is crucial to avoid cross-imports between the two subprojects to maintain the integrity of the transp ...

Setting up TypeScript to function with Webpack's resolve.modules

Imagine having a webpack configuration that looks like this: resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'], modules: ['my_modules', 'node_modules'], }, You have a ...

Having trouble importing a video file? An error occurred while parsing the module: Unexpected character ' ' at line 1, column 0. This issue was encountered while

Hello, I am currently facing an issue where I am attempting to set a background with a looping video. However, when I try to import the mp4 video from the folder, I encounter the following error: Error - ./videos/video.mp4 Module parse failed: Unexpected c ...

I am currently experiencing difficulties with sending the image through my Next.js application on my Node.js backend

I am currently working on a next js application with the goal of sending image data, compression ratio, and output format from the frontend form to the backend (nodejs) application. The aim is for the backend to process this information and return the comp ...

Navigating a relative path import to the correct `@types` in Typescript

When the browser runs, I require import * as d3 from '../lib/d3.js'; for d3 to be fetched correctly. I have confirmed that this does work as intended. However, the file that contains the above code, let's call it main.js, is generated from ...

Apollo's useQuery executes just once and doesn't run again when components are re-rendered

Recently diving into the world of GraphQL and nextJS, I find myself facing a challenge in my ongoing project. My goal is to fetch data from the GraphQL server only once and then have that same data persist throughout multiple component re-renders. I atte ...

"Attempting to verify a JSON Web Token using a promise that returns an object not compatible with the specified

Learning about Typescript has been quite a challenge for me, especially when it comes to using the correct syntax. I have implemented a promise to retrieve decoded content from jwt.verify - jsonwebtoken. It is functioning as intended and providing an obje ...