White page, routing issues, and missing environment variables in browser

I’m working on a Next.js 14 project that works perfectly in development mode (next dev). However, after building the project (next build) and starting it (next start or as a standalone version with node server.js), several issues arise that I cannot resolve.

Setup:

  • Next.js 14
  • Standalone version started with node .next/standalone/server.js
  • Environment variables are passed at both build time and runtime, e.g.:
    NEXT_PUBLIC_API_URL=http://localhost:3000 HOSTNAME="0.0.0.0" node server.js
    
  • The project exists since Next.js 12 and my job was it to refactor the dependencies from 12 to 14. I had to add next-auth too, but it does not seem to be a problem here. As mentioned, it works in development mode.

Issues:

  1. White main page:

    • The main page (/de or /en) shows a completely white screen and the HTML is not served correctly (only <script/> tags seen in the inspector).
    • In the browser console, the following errors appear:
      • Specify NEXT_PUBLIC_API_URL in your .env file (even though the variable is set both at runtime and build time).
      • JavaScript files (.next/static) are served correctly, but the page doesn’t render.
  2. Routing issues:

    • When I visit /de/someinvalidroute, I get a redirect loop error:
      “The page isn’t redirecting properly.”
    • For example, the route /de/someinvalidroute redirects to /de/de, then to /de/de/de, and so on.
  3. Standalone setup:

    • I build the project locally using next build and then copy the contents of the .next/standalone folder to the server. I start the server using node server.js.
    • All necessary files (.next/static, public, etc.) are copied correctly, and environment variables are set.

I have tried:

  1. Cleared build cache and rebuilt:

    rm -rf .next
    next build
    
  2. Set environment variables at build time:

    NEXT_PUBLIC_API_URL=http://localhost:3000 next build
    
  3. Adjusted middleware:

    • I configured the next-intl middleware to exclude static files:
      export const config = {
        matcher: ['/((?!_next/static|_next/image|favicon.ico|images|api).*)'],
      };
      
  4. Local debugging:

    • I created a simple test page (pages/test.js) that does not work:
      export default function Test() {
        return <div>Test page is working!</div>;
      }
      
    • I can’t reach any other page than the main page under /de or /en, otherwise I get redirected.
  5. Cleared browser cache:

    • Tested in incognito mode and cleared the browser cache, but the white page and console errors persist.
  6. Using standard build output, not standalone:

    • I already tried to build the project without the standalone output option. Does not work either.

My questions are:

  1. Why are NEXT_PUBLIC_* variables not recognized in the browser, even though they are set at both build and runtime?
  2. How can I fix the redirect loop (/de/de/de) issue?
  3. Why does the main page remain blank, even though the HTML is served correctly?
  4. Are there known issues with Next.js 14, next-intl, or the standalone mode that could be relevant here?