#-

1 messages · Page 1 of 1 (latest)

bright tundraBOT
#

Hi @vast mural. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you're correct that the Edge Runtime in Next.js uses a strict subset of Node.js APIs, which are compatible with Edge computing platforms like Vercel. This means that certain Node.js APIs, like those used by Prisma, are not available in this runtime.

However, you have a couple of options to work around this:

  1. Use an alternate package that is compatible with edge compute: If a library is dependent on Node.js APIs and not compatible with Edge Functions, you can explore an alternate package that is compatible with edge compute. For example, if you are looking to handle JWTs or encryption/decryption, you can use jose (https://github.com/panva/jose) which is compliant with the edge runtime. [^1^]

  2. Use a Serverless Function: Serverless Functions use the full Node.js runtime, so you can use Prisma in a Serverless Function instead of in middleware. [^1^]

If you want to use Prisma in a Serverless Function, you can install it via npm:

npm install @prisma/client

Then, you can import your Prisma Client instance into any file where it's needed:

import prisma from '../lib/prisma';

And use it to interact with your database:

const feed = await prisma.post.findMany({
  where: { published: true },
  include: {
    author: {
      select: { name: true },
    },
  },
});

[^2^][^3^][^4^]

Unfortunately, there is no way to disable the Edge Runtime for middleware in Next.js. As of Next.js 13.1, the Edge Runtime is now stable for API routes and is used by default for middleware for better performance. [^5^]

#

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@vast mural kapa.ai is still learning and improving, please let us how it did by reacting below

#

Let me check the knowledge base for you. One moment! :books:

#

I understand your concern. Middleware in Next.js is designed to run before a request is completed, allowing you to modify the request or the response objects, or to decide if the request should continue in the chain. However, as you've noted, the middleware runs on the Edge runtime, which doesn't support all Node.js APIs, including Prisma.

When I mentioned using a Serverless Function, I was referring to the possibility of moving the logic that requires Prisma into a separate Serverless Function. Serverless Functions in Next.js are similar to API routes, but they are designed to run server-side code. They support the full Node.js runtime, so you can use Prisma within them.

Here's an example of how you might structure this:

  1. Middleware (Edge Runtime): Handles the initial request. If necessary, it makes a fetch request to your Serverless Function.
import { NextResponse } from 'next/server'

export function middleware(request) {
  // If necessary, make a fetch request to your Serverless Function
  const data = await fetch('/api/my-serverless-function').then(res => res.json())

  // Continue with your middleware logic
  // ...

  return NextResponse.next()
}
  1. Serverless Function (Full Node.js Runtime): Handles the fetch request from your middleware, uses Prisma to interact with your database, and returns the data.
import prisma from '../lib/prisma'

export default async function handler(req, res) {
  const data = await prisma.myModel.findMany()

  res.status(200).json(data)
}

This way, you're still able to use Prisma in your application, but you're doing so within the constraints of the Edge and Node.js runtimes.

#

Please note that this approach involves an additional HTTP request and might not be suitable for all use cases. It's important to consider the trade-offs and choose the best approach for your specific needs.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@vast mural kapa.ai is still learning and improving, please let us how it did by reacting below