Failed to fetch error in Next during static build or when Hono server is offline #202

Closed
opened 2026-03-13 07:37:24 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @ohkimur on GitHub (Nov 10, 2024).

Description:
When building a fully static app with Next.js that uses authClient from better-auth, I encounter a TypeError: Failed to fetch during the static build process or when the Hono server (which provides the API) is offline. This error occurs despite wrapping calls in try/catch, which does not seem to handle the failure gracefully.

Error Message:

Uncaught (in promise) TypeError: Failed to fetch

Relevant Code:
The following code initializes authClient with the Hono server URL from the environment variable NEXT_PUBLIC_API_URL:

import { createAuthClient } from 'better-auth/react'

export const authClient = createAuthClient({
  baseURL: process.env.NEXT_PUBLIC_API_URL,
})

The error occurs when using the authClient for example here in a custom hook.

import { authClient } from '@/lib'

export const useSession = () => {
  const {
    data: session,
    isPending,
    error,
  } = authClient.useSession()

  return {
    session,
    isPending,
    error,
  }
}

Expected Behavior:

  • During static builds or when the API server is offline, the code should handle the failed network request gracefully without breaking the build or causing unhandled promise rejections.

Steps to Reproduce:

  1. Set up a static Next.js project with authClient from better-auth pointing to a Hono API.
  2. Build the project (next build) or run it with the API server offline.
  3. Observe the TypeError: Failed to fetch error.

Suggested Solution:
An option to handle or bypass network requests during authClient initialization could prevent breaking the build when the API is offline. Alternatively, if authClient could detect and handle offline scenarios gracefully, that would resolve the issue.

Or it would be even better if it wouldn't be hard throwing an error when the API is offline.

Originally created by @ohkimur on GitHub (Nov 10, 2024). **Description**: When building a fully static app with Next.js that uses `authClient` from `better-auth`, I encounter a `TypeError: Failed to fetch` during the static build process or when the `Hono` server (which provides the API) is offline. This error occurs despite wrapping calls in `try/catch`, which does not seem to handle the failure gracefully. **Error Message**: ``` Uncaught (in promise) TypeError: Failed to fetch ``` **Relevant Code**: The following code initializes `authClient` with the `Hono` server URL from the environment variable `NEXT_PUBLIC_API_URL`: ```typescript import { createAuthClient } from 'better-auth/react' export const authClient = createAuthClient({ baseURL: process.env.NEXT_PUBLIC_API_URL, }) ``` The error occurs when using the `authClient` for example here in a custom hook. ```typescript import { authClient } from '@/lib' export const useSession = () => { const { data: session, isPending, error, } = authClient.useSession() return { session, isPending, error, } } ``` **Expected Behavior**: - During static builds or when the API server is offline, the code should handle the failed network request gracefully without breaking the build or causing unhandled promise rejections. **Steps to Reproduce**: 1. Set up a static Next.js project with `authClient` from `better-auth` pointing to a `Hono` API. 2. Build the project (`next build`) or run it with the API server offline. 3. Observe the `TypeError: Failed to fetch` error. **Suggested Solution**: An option to handle or bypass network requests during `authClient` initialization could prevent breaking the build when the API is offline. Alternatively, if `authClient` could detect and handle offline scenarios gracefully, that would resolve the issue. Or it would be even better if it wouldn't be hard throwing an error when the API is offline.
Author
Owner

@Tixyel commented on GitHub (Dec 5, 2024):

+1

@Tixyel commented on GitHub (Dec 5, 2024): +1
Author
Owner

@hellokaton commented on GitHub (Dec 9, 2024):

+1

@hellokaton commented on GitHub (Dec 9, 2024): +1
Author
Owner

@Bekacru commented on GitHub (Dec 17, 2024):

The "fetch failed" error occurs when fetch is called with an invalid URL during the build process. This typically happens if you attempt to statically build a page that requires fetching session. Since the session isn't available at build time (since there is no server and no user session), the build process fails.

To fix this, refactor your app so that pages requiring a user session are not statically built. Instead, make sure the request is fetched properly at runtime.

@Bekacru commented on GitHub (Dec 17, 2024): The "fetch failed" error occurs when fetch is called with an invalid URL during the build process. This typically happens if you attempt to statically build a page that requires fetching session. Since the session isn't available at build time (since there is no server and no user session), the build process fails. To fix this, refactor your app so that pages requiring a user session are not statically built. Instead, make sure the request is fetched properly at runtime.
Author
Owner

@zhaoyunyi commented on GitHub (Dec 25, 2024):

+1

@zhaoyunyi commented on GitHub (Dec 25, 2024): +1
Author
Owner

@Bekacru commented on GitHub (Jan 13, 2025):

network failures are now handled more gracefully

@Bekacru commented on GitHub (Jan 13, 2025): network failures are now handled more gracefully
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#202