Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update base-server.ts #64926

Open
wants to merge 1 commit into
base: 14-2-1
Choose a base branch
from
Open

Update base-server.ts #64926

wants to merge 1 commit into from

Conversation

Talentaa
Copy link

fixed Error handling upgrade request TypeError: Cannot read properties of undefined (reading 'bind')
issue url #55802

fixed Error handling upgrade request TypeError: Cannot read properties of undefined (reading 'bind')
@ijjk
Copy link
Member

ijjk commented Apr 23, 2024

Allow CI Workflow Run

  • approve CI run for commit: 45ad0a1

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, can you add a test case for this that fails without the change, this doesn't seem like a full fix if _res.setHeader is having issues here.

@ijjk
Copy link
Member

ijjk commented Apr 24, 2024

Failing test suites

Commit: e19d91c

TURBOPACK=1 pnpm test test/integration/eslint/test/next-lint.test.js (turbopack)

  • Next Lint > First Time Setup > installs eslint and eslint-config-next as devDependencies if missing with yarn
  • Next Lint > First Time Setup > creates .eslintrc.json file with a default configuration
  • Next Lint > First Time Setup > creates .eslintrc.json file with a default app router configuration
  • Next Lint > First Time Setup > shows a successful message when completed
Expand output

● Next Lint › First Time Setup › installs eslint and eslint-config-next as devDependencies if missing with yarn

ENOENT: no such file or directory, open '/tmp/dt5x2u6pqco/package.json'

● Next Lint › First Time Setup › creates .eslintrc.json file with a default configuration

ENOENT: no such file or directory, open '/tmp/yhiytgkclj/package.json'

● Next Lint › First Time Setup › creates .eslintrc.json file with a default app router configuration

ENOENT: no such file or directory, open '/tmp/g4d93wi99mh/package.json'

● Next Lint › First Time Setup › shows a successful message when completed

ENOENT: no such file or directory, open '/tmp/6jrtyuyt47u/package.json'

Read more about building and testing Next.js in contributing.md.

TURBOPACK=1 pnpm test-dev test/development/basic/node-builtins.test.ts (turbopack)

  • node builtins > should support node.js builtins in server component
  • node builtins > should support node.js builtins prefixed by node: in server component
Expand output

● node builtins › should support node.js builtins in server component

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)

  124 |
  125 |       const data = $('#node-browser-polyfills').text()
> 126 |       const parsedData = JSON.parse(data)
      |                               ^
  127 |
  128 |       expect(parsedData.vm).toBe(105)
  129 |       expect(parsedData.hash).toBe(

  at Object.parse (development/basic/node-builtins.test.ts:126:31)

● node builtins › should support node.js builtins prefixed by node: in server component

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)

  149 |
  150 |       const data = $('#node-browser-polyfills').text()
> 151 |       const parsedData = JSON.parse(data)
      |                               ^
  152 |
  153 |       expect(parsedData.vm).toBe(105)
  154 |       expect(parsedData.hash).toBe(

  at Object.parse (development/basic/node-builtins.test.ts:151:31)

Read more about building and testing Next.js in contributing.md.

@MalekBouba
Copy link

@ijjk @Talentaa Can we expect this to merged?
Any help needed?

Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, could you add a test case where this is failing?

@MalekBouba
Copy link

MalekBouba commented Aug 29, 2024

Hi, could you add a test case where this is failing?

I'm encountering this exact failure while trying to implement a micro-frontends app using Turbo & NextJs rewriting feature (via middlewares).

I needed to implement a routing system that check both for auth and micro-app availability and handle a decent redirection/error page.

All is good and I succeeded to get a chain of middleware that work in harmony and did what's needed but unfortunately not the webpack's HMR. The related socket keeps failing due to this (bind "undefined") issue. Which offcourse affects 📉 hugely our DX.

After hours of digging, It seems like the ws upgrade query did it. It keeps returning http error (produced by the use case code fail), then, it retries without any success.. after around a minute of retries, it refresh the window automatically.

Finally, When I forced the suggested code by @Talentaa in apps/main/node_modules/next/dist/server/base-server.js, the HMR socket works again, HMR works and the stack DX rises again 📈.

Possible related PRs & issues #31515 #23147

@ijjk
Copy link
Member

ijjk commented Sep 11, 2024

@MalekBouba are you using a custom server? If so could you show the setup in a minimal repro so we could add a test case for this?

@MalekBouba
Copy link

MalekBouba commented Sep 12, 2024

@ijjk We're not, we tried to stick with the official, standard tools, no custom servers, no overrides.
My app is based on this example from the docs.
The only difference to be mentioned is that we upgraded next to latest (instead of 13.x in the example) and that we use middleware to rewrite (instead of next configs in the example).

Here is how we do this:

import { type NextRequest, NextResponse } from "next/server";

export default function MyRoutingMiddleware(request: NextRequest) {
  if (myCondition) return NextResponse.redirect(/*...*/);
  if (myCondition) return NextResponse.rewrite(/*...*/);
  return undefined;
}

and,

import { NextResponse } from 'next/server'
import { NextMiddlewareResult } from 'next/dist/server/web/types'
import NextAuth from 'next-auth'

const { auth } = NextAuth(authConfig)

export function MyAuthMiddleware(middleware: any): any | NextMiddlewareResult {
  return auth((request) => {
    const response = NextResponse.next()
    if (myCondition) return NextResponse.next()
    if (!request.auth) return NextResponse.redirect(/*...*/)
    return middleware(request, undefined, response)
  })
}

then,

import { type NextRequest, NextResponse } from "next/server";

import MyAuthMiddleware from "middlewares/a";
import MyRoutingMiddleware from "middlewares/b";

const middlewares = [MyAuthMiddleware, MyRoutingMiddleware];

export default async function middleware(request: NextRequest) {
  for (const fn of middlewares) {
    const response = await fn(request);
    if (response) return response;
  }
  return NextResponse.next();
}

PS:

export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|_next/data|/__nextjs_original-stack-frame|assets|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}

Everything work fine in prod, we just suffer bad DX in dev env.
I can work on a full reproduction example if you think it's needed.

@ijjk
Copy link
Member

ijjk commented Sep 12, 2024

@MalekBouba does the issue resolve if you add _next/webpack-hmr to your negative matcher like you have for _next/data and similar? I'm not able to reproduce this with a basic middleware that is rewriting even for that route.

@MalekBouba
Copy link

@MalekBouba does the issue resolve if you add _next/webpack-hmr to your negative matcher like you have for _next/data and similar?

Unfortunately, no, it doesn't. that's what we tried first.

I'm not able to reproduce this with a basic middleware that is rewriting even for that route.

Do you need clone project for minimal reproduction?

@ijjk
Copy link
Member

ijjk commented Sep 12, 2024

Seeing the project would make adding a test case much easier!

@MalekBouba
Copy link

Seeing the project would make adding a test case much easier!

I'll work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants