Full-stack deployment Β· Cloudflare Pages...
Has anyone here successfully ran a next pages app with a D1 db locally?
I did follow this setup and configuration: https://developers.cloudflare.com/pages/framework-guides/nextjs/deploy-a-nextjs-site/#use-bindings-in-your-nextjs-application
Unfortunately I am not having any luck getting this to run.
33 Replies
What issue are you encountering? π
PS: if you want I would suggest to use the latest version of the guide (which is yet to be published): https://b6f0a2e0.cloudflare-docs-7ou.pages.dev/pages/framework-guides/nextjs/deploy-a-nextjs-site/
(but for that you also need to use the yet to be published version of C3:
npx [email protected] --no-auto-update
)Unknown Userβ’10mo ago
Message Not Public
Sign In & Join Server To View
yes, you can definitely use D1 in page.tsx π
you just need to make sure it's using the edge runtime
and you can still access it using
process.env.DB
if you want, but now the recommended way it though the requestContext
function instead π (getRequestContext().env.DB
)prior to adding D1, to run the app locally I would run pnpm run dev, I followed the guide i mentioned earlier to using '@'cloudflare/pages template. Im at the point of adding a D1 db to the application. I have made a prototype before and works fine when hosted but I didnt bother locally since the prototype was to get comfortable with hosting on cloudflare pages and D1.
I have created bindings in the
next.config.mjs
and env.d.ts
the app has a service which is a quick select all from a table and place them in the console
env.d.ts
next.config.mjs
service.ts
The error when calling that service during pnpm run dev
I will change the configurations around to match the unpublished guide and see what happensone sec...
you're not setting the binding correctly there in the config file
I would suggest adding a
// @ts-check
at the top of the file, that would show you that you're not passing to getBindingsProxy
what it's expectingthis is how it should look like
unfortunately the error still persists after adding the
// @ts-check
Would I need to run wrangler dev
at some point?Did you opt your route into the edge runtime?
ah yeah in the code @Mongy shared there's no
runtime
export πedge runtime isnt the problem. The app when hosted on cloudflare pages works fine with the bindings attached to it. Just the issue is trying to do it locally so I can avoid having to constantly redeploy for testing
I am getting an error that the table im trying to access doesn't exist now
this only happens locally
the change i did from my original comment was changing the D1DB value from a string to just D1Database
env.d.ts
This seemed to fix that error i was having earlier. Now the new error is when running locally, any table i try querying doesn't exist.@Mongy local bindings exist only locally and do no interact with the production thing in any way, if you're trying to access a table you have in your "real" database that's not going to work, you unfortunately need to re-create the table yourself for local development
For example following this: https://developers.cloudflare.com/d1/build-databases/import-data/#import-an-existing-database
As per our docs: https://developers.cloudflare.com/pages/functions/bindings/
So to clarify. I would need to spin a local D1 and fill the data similar way I did with the hosted version?
okay i managed to add a table to a local d1, after running
wrangler pages dev src --d1 D1DB=db_id
i see the bindings during the local server boot. I go to the url provided and getting a [wrangler:inf] GET / 404 Not Found
Context:
the nextjs application is using src directory so im not sure if src
is the correct file path to provide for wrangler pages dev
I do appreciate your assistance @Darioyes π
why are you running
wrangler pages dev src
? π€
can you share you folder structure?
does this work if you run next dev
(with either setupDevBindings
or setupDevPlatform
)?
would you be able to share your repo?GitHub
GitHub - A-Cer23/f1-analytics: A web app to visualize F1 data from ...
A web app to visualize F1 data from 1950 - 2023. Contribute to A-Cer23/f1-analytics development by creating an account on GitHub.
running pnpm run dev, any run dev works fine. Once I get to the page where the call is made to the DB i get no table by that name exists.
@Mongy this seems to work for me
I think that that's what you set for your preview_id so that's what's been used to generate the data
oh
that worked!
but it doesn't work with
setupDevPlatform
.... that confused me.... π
to be 100% honest I am not that familiar with d1... but I would suggest you to, retry to create the tables without using a preview_id and just see if things work smoothly in that way π€i just run
pnpm run dev
works fineall the Pages integration (
setupDevPlatform
and wrangler dev
) never interact with remote anywaysyeah i didnt think i would have to use wrangler to populate a local d1 instance. Honestly thought wrangler could pull the data that's hosted on the ID and store locally as a D1 instance.
no I'm sorry that's not currently the case, I would love at some point to implement better tooling to populate the local bindings (which can include pulling the data from remote), but that's currently not the case π
Itβs alright we make do with what we got. I mean pages and d1 are still babies hahaha. So far been enjoying the development experience.
I'm glad to hear that, yeah sorry for the small bumps in the road π
env.d.ts doesn't seem to work for me
getRequestContext().env.DB
doesnt exist
Hey @bun, the issue there (I'm pretty sure) is that you're importing
D1Database
in your d.ts file making it become module scoped
(looking for links to share...)ah true..
sorry I could not find links to share π
(I remember there was a nice stackoverflow thread explaning module vs global π’)
anyways I see that you get it anyways π
TLRD; put
workers-types
in your tsconfig's types
or use declare global
in the d.ts file πyeah it works after i deleted the import
nice! π