Comments (2)
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.
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)
- support returning functions from macros
- winston's timestamp format is not working
- --covarage crashed on windows and show files from node_modules HOT 1
- when sending large chunks into a tcp socket at once, only the first ~128kb are sent HOT 1
- windows : chalk, sveltkkit-superform HOT 2
- Bun.build: equivalent of globalNames
- @hapi/hapi hello world memory leak
- [bun:test] expect resolves not toThrow fails
- I can't run bun run dev or anything with bun HOT 2
- Bun install sometimes fails in Github actions HOT 2
- Segmentation Fault at address 0x0 HOT 16
- Using bun on Termux is not on the docs
- No doc on how to install bun on Termux HOT 1
- pdfjs-dist bug dyld[33775]: missing symbol called zsh: killed bun index.ts HOT 1
- Implement commands in Bun shell
- Piping from `/dev/null` stdin on windows fails with `EBADF` HOT 1
- Support `bun test -e` and `bun test -`
- Mime-type not preserved with response.blob()
- `bunx --bun eslint --init` giving path error on Windows HOT 1
- regression: grammy: TypeError: undefined is not a constructor (evaluating 'new node_shim_js_1.AbortController')
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bun.