tursodatabase / libsql-client-ts

TypeScript/JavaScript client API for libSQL

Home Page:https://docs.turso.tech/sdk/ts/quickstart

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Caching issue with Next.js and Turso dev server

u2ix opened this issue · comments

Discovered a strange behavior inside a Next.js application together with Turso dev server (using http url).

Issue: New entries where not returned from a query.

After a lot of digging, I discovered a directory named fetch-cache and saw that there are the requests to Turso cached. Deleting this directory solved the issue temporarily as the new records where returned from the query thereafter.

To fix the problem, a parameter { cache: 'no-store' } should be included with the fetch call. (More info in the Next.js documentation here)

I could fix this locally by adding a custom fetch function to my config:

function noCacheFetch(
    input: string | URL | globalThis.Request,
    init?: RequestInit,
): Promise<Response> {
    return fetch(input, { ...init, cache: 'no-store' });
}

const client = createClient({
    ...,
    fetch: noCacheFetch
});

But I feel it would be better to include this in the library as default to avoid this problem to future users. What do you think?

i use the turso dev command to launch the dev server for the database alongside nextJS.

i do call the db from a react server component, but I got this error.

Error: Unexpected non-whitespace character after JSON at position 308 (line 15 column 2)

I've been getting the undici error below periodically on my Next.js app. It won't happen for days and then it will happen a few times in a row.

I'm not sure if this is related to setting cache: no-store. Overall it seems like a Vercel/Lambda issue, as reported here. I downgraded to @libsql/client 0.5.6 and haven't seen the error in a few days. 0.5.6 doesn't use native fetch, so it kind of makes sense.

Unhandled Rejection: TypeError: fetch failed
at node:internal/deps/undici/undici:12502:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async globalThis.fetch (/var/task/.next/server/chunks/198.js:1:36495)
at async getIsSchemaDatabase (/var/task/node_modules/.pnpm/@libsql+client@0.6.2/node_modules/@libsql/client/lib-cjs/migrations.js:45:24)
at async HttpClient.getIsSchemaDatabase (/var/task/node_modules/.pnpm/@libsql+client@0.6.2/node_modules/@libsql/client/lib-cjs/http.js:79:38) {
[cause]: Error: Client network socket disconnected before secure TLS connection was established
at TLSSocket.onConnectEnd (node:_tls_wrap:1727:19)
at TLSSocket.emit (node:events:531:35)
at endReadableNT (node:internal/streams/readable:1696:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'ECONNRESET',
path: undefined,
host: 'hippo-lingo-prod-giannif.turso.io',
port: 443,
localAddress: null
}
}
Node.js process exited with exit status: 128. The logs above can help with debugging the issue.

Separately, it seems like this pr was maybe started to address this? I'm not familiar with hrana, so I may be off target there

i use the turso dev command to launch the dev server for the database alongside nextJS.

i do call the db from a react server component, but I got this error.

Error: Unexpected non-whitespace character after JSON at position 308 (line 15 column 2)

I was experiencing the same - only work around was to downgrade @libsql/client to 0.5.6 as mentioned above.

My deployment to Vercel works fine, and I only experience the error running locally with version ^0.6, but this is because my deployed version is referencing a Turso DB rather than a local libsql instance.