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

Astro DB on Cloudflare: Build fails with "Cannot create a remote client: missing app token" error. #10872

Open
1 task
pMay2021 opened this issue Apr 24, 2024 · 13 comments
Labels
needs response Issue needs response from OP

Comments

@pMay2021
Copy link

pMay2021 commented Apr 24, 2024

Astro Info

Astro                    v4.6.4
Node                     v21.7.3
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/tailwind
                         @astrojs/mdx
                         astro:db
                         @astrojs/db/file-url

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

My Cloudflare account deploys automatically on GitHub checkin.

Astro DB works fine local and with --remote (though I've been seeing some ENETUNREACH today but let's park that).

Deployment on Cloudflare fails with the following error:

Success: Assets published!
15:21:40.397	Error: Failed to publish your Function. Got error: Uncaught Error: Cannot create a remote client: missing app token.
  at chunks/pages/404_MemiTqFJ.mjs:18015:11 in createRemoteDatabaseClient
  at chunks/pages/404_MemiTqFJ.mjs:18234:18

ASTRO_STUDIO_APP_TOKEN = value, is set on Cloudflare settings.

My configuration is this:

export default defineConfig({
  integrations: [tailwind(), mdx(), db()],
  output: "server",
  adapter: cloudflare()
});

Not sure what else to do.

What's the expected result?

Build to complete and deploy.

Link to Minimal Reproducible Example

It's on my CF deployment.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 24, 2024
@matthewp
Copy link
Contributor

We had some downtime earlier today. Can you confirm that you're still experiencing this?

@pMay2021
Copy link
Author

@matthewp still seeing both the errors.

  1. On my local dev machine, I keep facing the following intermittently:
18:17:01 [ERROR] [Astro DB Error] request to https://gbaimk52fwt9fulhykeljirqxnif-astro.turso.io/v2/pipeline failed, reason: connect ENETUNREACH 2a09:8280:1::6:d39d:443

  1. And when I check in code, the same app token error appears again.

18:19:35.576	Success: Assets published!
18:19:37.675	Error: Failed to publish your Function. Got error: Uncaught Error: Cannot create a remote client: missing app token.
  at chunks/pages/404_MemiTqFJ.mjs:18015:11 in createRemoteDatabaseClient
  at chunks/pages/404_MemiTqFJ.mjs:18234:18

  1. npm run build does not throw any error for me.

@ematipico
Copy link
Member

@pMay2021 are you providing the environment variable ASTRO_STUDIO_APP_TOKEN? You should provide either via CI or via CLI

@ematipico ematipico added needs response Issue needs response from OP and removed needs triage Issue needs to be triaged labels Apr 25, 2024
@matthewp
Copy link
Contributor

Locally they would not need the token, it gets retrieved as long as they are logged in. @pMay2021 Can you try logging in again locally? I doubt that's the problem but would help to isolate.

@pMay2021
Copy link
Author

pMay2021 commented Apr 25, 2024

Hi @matthewp what do you mean "login locally"? There's no issue on my local machine. But unfortunately this morning I'm hitting the request to https://gbaimk52fwt9fulhykeljirqxnif-astro.turso.io/v2/pipeline failed, reason: connect ENETUNREACH 2a09:8280:1::6:d39d:443 repeatedly on --remote. I think all three issues are linked to the DB implementation

  1. The ENETUNREACH that occurs frequently (not always; because when I refresh my pages it goes through sometimes and sometimes not)
  2. The failure to do a --force-reset
  3. The failure on Cloudflare build

Stackblitz reproduction

I'm unable to reproduce on Stackblitz. Astro DB doesn't install. I get an error saying

Command failed with exit code 1: npm install @astrojs/db@^0.10.6
npm ERR! code ERR_INVALID_PROTOCOL
npm ERR! Protocol "https:" not supported. Expected "http:"

and when I tried to install manually, I get:

vite] Error when evaluating SSR module /home/projects/github-wdwm38/astro.config.mjs: failed to import "@astrojs/db"
|- Error: Cannot find module '@astrojs/db' imported from '/home/projects/github-wdwm38/astro.config.mjs'

here's the link to a very basic project:

https://stackblitz.com/edit/github-wdwm38?file=astro.config.mjs (it won't built and fails with an error)

I have a question - how do I make the remote database connection work from Stackblitz? I've created a temp token on Studio, do I give it to you to paste it in a .env?

(PS - I apologize if I'm missing something basic. I'm new to a lot of this--but I love Astro though!).

@alirexa
Copy link

alirexa commented May 13, 2024

I have the same problem as @pMay2021, not any problems locally but when trying to deploy, I get this error at the last step:

Error: Failed to publish your Function. Got error: Uncaught Error: Cannot create a remote client: missing app token. at chunks/pages/index_QU-HpiYl.mjs:8513:11 in createRemoteDatabaseClient at chunks/pages/index_QU-HpiYl.mjs:8729:18

I have to point out, I don't have the problem in a previews commit which connects to the DB but no Insert to the DB. The latest commit does DB Insert too.

@matthewp
Copy link
Contributor

@alirexa are you also using Cloudflare?

@ematipico
Copy link
Member

@alirexa are you also using Cloudflare?

They do, looking at their astro info output

@matthewp
Copy link
Contributor

@alirexa Did you set the ASTRO_STUDIO_APP_TOKEN environment variable? You can do it in the Settings -> Variables page.

Screen Shot 2024-05-17 at 10 04 05 AM

@aurelius-coffee
Copy link

I'm experiencing the same issue. After playing around with this for a little while I found the issue in my case is using db in middleware.ts. Without using it everything works fine. API routes work, form submissions in Astro pages work, I haven't tried actions.

Not sure if I should comment here, or create a new issue, so I'll start here.

Astro Info

Astro v4.9.2
Node v20.13.1
System Linux (x64)
Package Manager pnpm
Output server
Adapter @astrojs/cloudflare
Integrations @astrojs/mdx
@astrojs/sitemap
astro:db
@astrojs/db/file-url

Link to reproducible build

https://github.com/aurelius-coffee/my-astro-app/tree/main

I tried stackblitz, but couldn't log into astro studio there.

image

@matthewp
Copy link
Contributor

Very interesting, we'll check that out.

@aurelius-coffee
Copy link

aurelius-coffee commented May 29, 2024

If this was turso I think we could create the db var inside the middleware using context.locals.runtime no problem, something like this:

import { defineMiddleware } from "astro:middleware";
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client/web";

import { comments } from '../lib/schema'

// `context` and `next` are automatically typed
export const onRequest = defineMiddleware(async (context, next) => {
    const turso = createClient({
        url: context.locals.runtime.env.TURSO_DATABASE_URL!,
        authToken: context.locals.runtime.env.TURSO_AUTH_TOKEN,
      });
      
    const db = drizzle(turso);
    const result = await db.select().from(comments).all();
    console.log('result is', result)
    return next()
});

@pMay2021
Copy link
Author

pMay2021 commented May 30, 2024

new edit: I can now confirm that if I remove the dependency on Astro DB from middleware, Cloudflare builds and works.

Ok, I'm back after a while. I gave up trying to get it running on CF and focused on getting my features done on the local env, and everything works fine, as in my local instance connects perfectly to the remote Astro DB.

Finally, this morning I tried to deploy on Cloudflare, and it failed in the last step, with the same error as @alirexa

Note: I have configured ASTRO_STUDIO_APP_TOKEN on CF.

Error: Failed to publish your Function. Got error: Uncaught Error: Cannot create a remote client: missing app token.
  at chunks/pages/__ByV4bDDm.mjs:8387:11 in createRemoteDatabaseClient
  at chunks/pages/__ByV4bDDm.mjs:8603:18

Usage of db in middleware

While I don't import "astro:db" in my middleware, I do call a library function that imports "astro:db".

I cannot easily remove the db dependency without breaking a lot of pages.

I can now confirm that if I remove the dependency on Astro DB from middleware, Cloudflare builds and works.

An oddity I noticed

I did notice an oddity on Astro Studio though, not that it matters, but here's what I saw.

For example, when I generate a token, I see something like this on the screen:

550044079a64f3a327c8b1641fe60c4czb5c410f:twbsvvepb2mc283demddk6ieag0u (this won't work, don't worry)

but if I use the copy to clipboard, it actually gives me

550044079a64f3a327c8b1641fe60c4czb5c410f:twbsvvepb2mc283demddk6ieag0u:twbsvvepb2mc283demddk6ieag0u repeating the last : portion.

not sure if that matters.

Astro info


Astro v4.9.2
Node v21.7.3
System macOS (arm64)
Package Manager npm
Output server
Adapter @astrojs/cloudflare
Integrations @astrojs/tailwind
@astrojs/mdx
astro:db
@astrojs/db/file-url

Summary

Hopefully, it's now helpful from two examples that the Cloudflare build fails when there's Astro DB dependency in the middleware and this blocker can be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs response Issue needs response from OP
Projects
None yet
Development

No branches or pull requests

5 participants