Specify a different D1 database on preview page

For Workes & Pages, what is the name of the domain?

astro-cloudflare-pages-a5o

What is the issue or error you’re encountering

I am using an Astro app which uses D1 for APIs. I want to specify a different D1 database on the preview branch.

What steps have you taken to resolve the issue?

Tried separate wrangler.toml for preview and production. But pages does not support different configs for wrangler

1 Like

Added as below, this will let you connect to the preview-db in local mode & production db in prod mode.

#:schema node_modules/wrangler/config-schema.json
name = "sample"
compatibility_date = "2024-07-22"
pages_build_output_dir = "./dist"

[[d1_databases]]
binding = "DB"                                       
database_name = "sample-d1-preview"
database_id = "123"

[env.preview]
name = "preview"
[[env.preview.d1_databases]]
binding = "DB"                                       
database_name = "sample-d1-preview"
database_id = "a2b2e325-bc14-47e7-9883-22ee46806c4d"

[env.production]
name = "production"
[[env.production.d1_databases]]
binding = "DB"                                    
database_name = "sample"
database_id = "75f68c49-2c64-4557-8a82-0d367023f3ef"
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.