real_witt
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
@GM note that my instructions are for accessing data from a Cloudflare D1 instance, if you're using an external DB from somewhere else then this prob won't help you too much
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
To start off, you'll need to set your env variables in your
wrangler.toml
file (as per the docs https://developers.cloudflare.com/pages/framework-guides/nextjs/deploy-a-nextjs-site/#use-bindings-in-your-nextjs-application)
Then if you're using .ts you generate the types using
npm run build-cf-types
(found that on https://blog.cloudflare.com/blazing-fast-development-with-full-stack-frameworks-and-cloudflare)
Create your route:
(taken from https://developers.cloudflare.com/pages/framework-guides/nextjs/deploy-a-nextjs-site/#access-bindings-in-the-application)
Then to avoid conflicts with mixing client/server component imports, create a getter file.
After @thipperz helped me with this I remember reading something on about this pattern on either stackoverflow or the next.js docs but don't remember the link. Oh well.
Then FINALLY to be able to use the data inside your actual .tsx file with html markup:
The process of getting the data into your .tsx file was pretty poorly documented on the Cloudflare docs, but it could be assumed knowledge, and I'm pretty new to react and next.js so...33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
I’m boutta hit the hay for tonight but I’ll show my configs tomorrow morning in here and hopefully that will help
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
Which is weird considering how basic this is for making any kind of app
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
If there is a better way to do this, I can’t find one anywhere online. In the docs or otherwise
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
I did get an error on returning a non-Response object from the db call at one point, but that setup above works for me so… idk lol
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
:peepoHappyhug:
33 replies
CDCloudflare Developers
•Created by real_witt on 5/3/2024 in #pages-help
How to access a D1 binding from a next.js tsx file (NOT .ts)?
you're a life saver lol
33 replies