8000 [BUG] NextAuth signOut request takes 15 seconds in preview mode · Issue #686 · opennextjs/opennextjs-cloudflare · GitHub
[go: up one dir, main page]

Skip to content

[BUG] NextAuth signOut request takes 15 seconds in preview mode #686

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

Closed
Macil opened this issue May 28, 2025 · 6 comments · Fixed by #689
Closed

[BUG] NextAuth signOut request takes 15 seconds in preview mode #686

Macil opened this issue May 28, 2025 · 6 comments · Fixed by #689
Labels
bug Something isn't working triage

Comments

@Macil
Copy link
Macil commented May 28, 2025

Describe the bug

If you make a sign-out button with Auth.js/NextAuth and then use it while running your project with npm run preview (which uses Cloudflare wrangler and supports durable objects with rpc unlike npm run dev), the request to sign you out takes 15 seconds to complete, and this error shows up in the terminal:

✘ [ERROR] failed to get redirect response Error: Network connection lost.

      at async tz (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:230571:29)
      at async tX (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:230713:150)
      at async r9 (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:231643:29)
      at async doRender
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:127279:30)
      at async NextNodeServer.renderToResponseWithComponentsImpl
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:127494:32)
      at async NextNodeServer.renderPageComponent
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:127902:24)
      at async NextNodeServer.renderToResponseImpl
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:127937:32)
      at async NextNodeServer.pipeImpl
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:126742:29)
      at async NextNodeServer.handleCatchallRenderRequest
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:241609:17)
      at async NextNodeServer.handleRequestImpl
  (home/macil/Coding/next-auth-test/.wrangler/tmp/dev-KJM7H1/worker.js:126633:17) {
    retryable: true
  }


[wrangler:info] POST / 303 See Other (15019ms)

This issue does not affect applications in production deployed to Cloudflare Workers or run locally with npm run dev. I'm a little surprised that npm run preview is the odd one out since generally it seems like npm run preview is the development option that works most closely to Cloudflare Workers.

Steps to reproduce

A simple example project reproducing this bug is at https://github.com/Macil/next-auth-test. The project is a fresh OpenNext Cloudflare template with a straight-forward usage of NextAuth from following NextAuth's documentation. I've kept the git history clean so you can easily see exactly what was done.

The signOut() function is called in a server action in components/SignOut.tsx, following the example given in NextAuth's docs at https://authjs.dev/getting-started/session-management/login.

Expected behavior

Calling NextAuth's signOut() function in a server action should be quick and not log any errors when used under npm run preview.

@opennextjs/cloudflare version

1.0.4

Wrangler version

4.17.0

next info output

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024
  Available memory (MB): 31694
  Available CPU cores: 32
Binaries:
  Node: 23.6.1
  npm: 11.0.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.3.2 // Latest available version is detected (15.3.2).
  eslint-config-next: 15.3.2
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Additional context

It might be worth noting that there is a separate OpenNext/NextAuth bug affecting signout (#606) that this repository has a workaround in place for in open-next.config.ts. I do not think that bug is directly related to the 15 second delay but I might be wrong. The delay is present with or without this workaround.

@sommeeeer
Copy link
Contributor

You could make this reproduction not require OAuth by using this as your auth.ts instead: https://gist.github.com/sommeeeer/e76c207464e96342777bf6a33d52a70f

@sommeeeer
Copy link
Contributor
sommeeeer commented May 29, 2025

This seems to be a bigger scope than just next-auth. It seems that any redirect inside a server action would cause the same delay and error in preview mode. (i.e this code). At first glance from debug mode enabled in wrangler it seems to be a protocol issue. Shouldn't this be http and not https?

  method: 'Network.requestWillBeSent',
  params: {
    requestId: '0',
    loaderId: '',
    request: {
      url: 'https://localhost:8787/posts/2',
      method: 'GET',
      headers: [Object],
      mixedContentType: 'blockable',
      initialPriority: 'VeryLow',
      referrerPolicy: 'unsafe-url',
      isLinkPreload: false
    },
    timestamp: 27415.191,
    wallTime: 1748509860.386,
    initiator: { type: 'script', stack: [Object], lineNumber: 0 },
    type: 'Fetch'
  }
}

@sommeeeer
Copy link
Contributor
sommeeeer commented May 29, 2025

https://github.com/vercel/next.js/blob/6b1e48080e896e0d44a05fe009cb79d2d3f91774/packages/next/src/server/app-render/action-handler.ts#L337-L345 - it seems to hit the catch here: failed to get redirect response. Its trying to fetch with https. This is the URL before the fetch():

URL {                                                                                                                                  
  origin: 'https://localhost:8787',
  href: 'https://localhost:8787/api/auth/signin?callbackUrl=http%3A%2F%2Flocalhost%3A8787%2F',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'localhost:8787',                                                
  hostname: 'localhost',   
  port: '8787',                
  pathname: '/api/auth/signin',                                                                                                                   
  search: '?callbackUrl=http%3A%2F%2Flocalhost%3A8787%2F',                                                                                        
  hash: '',
  searchParams: URLSearchParams(1) { 'callbackUrl' => 'http://localhost:8787/' }
}

The problem seems to be this line: https://github.com/vercel/next.js/blob/6b1e48080e896e0d44a05fe009cb79d2d3f91774/packages/next/src/server/app-render/action-handler.ts#L314-L315 - You can replace it with this to solve the issue:

const fetchUrl = new URL(
  `http://localhost:8787${appRelativeRedirectUrl.pathname}${appRelativeRedirectUrl.search}`
)

Not sure how it should be handled. It turns out Next uses this environment variable (__NEXT_PRIVATE_ORIGIN) internally in standalone mode aswell. Its being set here in startServer that standalone mode uses:

8000

@sommeeeer
Copy link
Contributor
sommeeeer commented May 29, 2025

@Macil A workaround until there is a better solution would be update your wrangler.jsonc to have this:

"env": {
  "preview": {
    "vars": {
      "__NEXT_PRIVATE_ORIGIN": "http://localhost:8787"
     // you might want this aswell
     // "AUTH_URL": "http://localhost:8787"
    }
  },
},

Then to run in preview mode you can do npm run preview -- --env preview

@Macil
Copy link
Author
Macil commented May 30, 2025

Thanks, that workaround works.

Yeah, I previously noticed that I also have to set the environment variable AUTH_URL to http://localhost:8787 in order for NextAuth's Oauth redirect URLs to be set right, because NextAuth would incorrectly default it to be https://localhost:8787 too when in npm run preview. It works fine in npm run dev. I noticed that when using npm run dev, Next.js treats incoming requests as having the "x-forwarded-proto: http" header, which NextAuth uses to determine the default redirect URL, but in npm run preview, Next.js treats incoming requests as having the "x-forwarded-proto: https" header. I wonder if OpenNext should do something to override this in npm run preview, and I wonder if that same workaround might also fix the default value of __NEXT_PRIVATE_ORIGIN or if something else would have to be done for that.

@sommeeeer
Copy l 9537 ink
Contributor
sommeeeer commented May 30, 2025

I wonder if OpenNext should do something to override this in npm run preview, and I wonder if that same workaround might also fix the default value of __NEXT_PRIVATE_ORIGIN or if something else would have to be done for that.

Your probably right the edge converter should consider setting the x-forwarded-proto header to http if we are in preview mode. However, it won't solve that redirect problem. It would still be broken without __NEXT_PRIVATE_ORIGIN.

I'll open a PR on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
2 participants
0