Skip to content
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

Caching issue with Next.js and Turso dev server #225

Closed
u2ix opened this issue Jun 15, 2024 · 7 comments
Closed

Caching issue with Next.js and Turso dev server #225

u2ix opened this issue Jun 15, 2024 · 7 comments

Comments

@u2ix
Copy link

u2ix commented Jun 15, 2024

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?

@ChoaibMouhrach
Copy link

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)

@giannif
Copy link

giannif commented Jun 17, 2024

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

@airstart
Copy link

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.

@pstachula-dev
Copy link

OMG I looking for a solution to this problem for few hours, this error is ridiculous. Thanks a lot for your help!

@akarabach
Copy link

It takes hours to understand why this is not working on vercel, but working locally. looks like until nextjs 15 is not released - this should be a part of my config

@annjose
Copy link

annjose commented Nov 21, 2024

Thank you so much for sharing this! I have been puzzled by this behavior for half an hour - the local data was stale, but when I run on the server, it would work fine. I tried this noCache config and it fixed the issues.

@u2ix
Copy link
Author

u2ix commented Dec 10, 2024

Probably this won't be needed anymore with Next.js 15 due to the new opt-in for caching, I hope. So closing this now. Please add a comment if the problem still arises with version 15.

@u2ix u2ix closed this as completed Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants