Git Product home page Git Product logo

Comments (2)

Electroid avatar Electroid commented on July 20, 2024

Thanks for reporting! Could you share your package.json and code so we can reproduce this? If you're able to narrow down to a minimal reproduction that would be great.

from bun.

Yasamato avatar Yasamato commented on July 20, 2024

I'm experiencing something similar, the cause being that fetch sometimes (2-3 out of 10 calls) has an empty response body and as such failing the json decode.

Running it via bun test-fetch.ts is unable to reproduce the error. I only observed this error in combination of an next.js app, but I am unable to pinpoint which version (nextjs or bun) did have a regression.

Below instructions seem to work... Only if I switch the api endpoint to my server it breaks, maybe something wrong with my server instead of the code/bun/nextjs


bunx create-t3-app

Create a server side function in /src/lib/util.ts

'use server'

export async function fetchApi<T>(
  method: 'GET' | 'POST' | 'DELETE',
  path: string,
  data?: object,
) {
  const url = "https://meowfacts.herokuapp.com/?" + path

  return fetch(url, {
    method: method,
    cache: 'no-store',
    headers: {
      "Content-Type": "application/json"
    },
    body: data ? JSON.stringify(data) : null,
  })
    .then((res) => {
      if (!res.ok) {
        throw new Error(
          `[api]: ${res.status} ${method} ${path} responded with error: ${res.statusText}`,
        )
      }

      return res
        .json()
        .then((resData) => {
          console.log("Response body has", Object.keys(resData), typeof(resData))
          return resData as T
        })
        .catch(() => {
          throw Error(
            `[api]: ${res.status} ${method} ${path} failed to decode JSON response`,
          )
        })
    })
    .catch((e) => {
      throw Error(`[api]: ${method} ${path} fetch failed due to ${e}`)
    })
}

Create a client component in /src/components/SearchDemo.tsx

'use client'
import { useEffect, useState } from 'react'
import { fetchApi } from '~/lib/util'

export function SearchDemo() {
  const [search, setSearch] = useState('')
  const [data, setData] = useState<object[]>([])

  useEffect(() => {
    async function update() {
      setData((await fetchApi<object>("GET", search)).data as object[])
    }
    void update()
  }, [setData, search])

  return (
    <div>
      <div className="p-2">
        <input
          type="text"
          value={search}
          onChange={(e) => setSearch(e.target.value)}
        />
      </div>

      {data.map((d) => {
        return <div key={JSON.stringify(d)}>{JSON.stringify(d)}</div>
      })}
    </div>
  )
}

And edit /src/app/page.tsx

import { SearchDemo } from '~/components/SearchDemo'

export default function LandingPage() {
  return <SearchDemo />
}

Starting the server via

bun run dev

You should see now sometimes the error popping up in the console when you type in the input field. This does not occur when using npm btw. Maybe a synergy issue as next hacks into fetch?

from bun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.