Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors

Hi @hrishikesh ,
Please add the env variable to our site also:

Site ID: f6920b29-cfac-4cbb-ac1f-3c34cabef01e

Thank you.

You can use the Netlify CLI to add this environment variable, e.g.

➜ netlify env:set __NEXT_PRIVATE_PREBUNDLED_REACT next
Set environment variable __NEXT_PRIVATE_PREBUNDLED_REACT= next in the all context

nickyt.online on  main [✘!?] via  v18.15.0 took 3s 
➜ netlify env:list
1 environment variable for site somesite in the dev context 
.-------------------------------------------------.
|              Environment variables              |
|-------------------------------------------------|
|               Key               | Value | Scope |
|---------------------------------|-------|-------|
| __NEXT_PRIVATE_PREBUNDLED_REACT | next  | All   |
'-------------------------------------------------'
? Show values? Yes
1 Like

To clarify, this should work for anyone at any account level. You may need to configure the CLI first, so this is the doc for that:

https://cli.netlify.com/getting-started

(you’ll need to have done at least netlify login and also netlify link before you can use the CLI workflow explained above.)

@sadhikari and @roberto I managed to get the variable added for your sites before Nick taught us this trick.

When you are ready for it to be removed, that will be possible through the UI :slight_smile:

1 Like

Receiving this error when using that env variable, with the deploy failing:
Invalid AWS Lambda parameters used in this request.

Confirmed it’s being set correctly and upgraded to Next.js 13.4.2 (latest). Anyone else been able to successfully deploy? Just tested over at Vercel and it works out-of-the-box (not surprisingly).

3 Likes

I have the same exact error. I am not adding any env variables that might clash with AWS.

There’s a pull request tracking the upgrade of Next runtime to 13.4.x here: chore: upgrade to next v13.4 by taty2010 · Pull Request #2080 · netlify/next-runtime · GitHub

From what I’ve found, this happens on any error, including automatic handling of 404s, fetch requests that don’t return 200 and manually triggering notFound() in the page component. I’ve testet this up to 13.4.2 and it always leads to 500. It’s especially annoying because it’s impossible to debug what the underlying error is right now. You can try/catch yourself and avoid the problem, but it’s really not a solution.

3:13:45 PM: Failed during stage 'building site': 
Environment variable key '__NEXT_PRIVATE_PREBUNDLED_REACT' 
must start with a letter

Was able to add the variable however the deploy fails at the initialization phase.

Hey all! We’re aware of the issues around this change with Next.js and are working to ensure Next.js continues working normally on Netlify. As soon as we have any further updates, we’ll update this thread.

1 Like

Would it be possible to have some more transparency on which version of next the netlify runtime currently supports “stable”? I feel like it’s really hard to get an answer on that and should at least be public in the runtime readme on github.

1 Like

Hi @hrishikesh

Can you add the env variable for this site aswell:
siteID: da2f62d5-0e6b-4205-9096-3f32d0ff5bd6

Thank you!

Hi, I have found quite a janky solutions but works for me.

Turns out the __NEXT_PRIVATE_PREBUNDLED_REACT environment variable might have nothing to do with build process regarding to NextJS code, it uses to override to uses Vercel’s precompiled react-dom that has ./server.edge imports on next/server runtime. Means that this variable has to be present in Lambda functions. But because of some quirks you cannot add them directly.

So, I do some patches to make react overriding to always trigger before starts any builds and my site now works normally without issues.

cleanshot_2023-05-25_at_02.51.03

1 Like

Hi @frederikfink this isn’t necessary and won’t help. Also this does not need to be done on our end.

1 Like

Can confirm that applying the monkey patch above does work for me (next 13.4.3)

Where do you put this code?

In theory, anywhere before next-runtime bundling functions. But safely, I prefer to patch this code before running next build or any next cli.

This is how I did it:

  1. Create a prebuild script at prebuild.js in the route of your project. This is the code you want:
console.log("********* PREBUILDING");
const path = require("node:path");
const fs = require("fs");
const baseDir = process.cwd();

const prebuildScripts = async () => {
  const file = path.join(
    baseDir,
    "/node_modules",
    "next/dist/server/require-hook.js"
  );

  const content = await fs.promises.readFile(file, "utf-8");
  await fs.promises.writeFile(
    file,
    content.replace(
      "if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT) {",
      "if (true) {"
    )
  );
};

prebuildScripts();

  1. Update package.json to include a prebuild step:
  "scripts": {
    "dev": "next dev",
    "prebuild": "node prebuild.js",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
3 Likes

Thanks for sharing this with the community!

Yup this was the fix right here , i tried all other solutions from different posts but none of them worked

I’ve tried it but still not working, non-working route: brc-20.netlify.com/mint/ticker

How do you run this when your site builds on netlify?