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

Env vriables with Vite and Cloudflare, the new replace of getRuntime() #8060

Closed
1 task done
Mahmoudgalalz opened this issue Aug 14, 2023 · 15 comments · Fixed by #8190
Closed
1 task done

Env vriables with Vite and Cloudflare, the new replace of getRuntime() #8060

Mahmoudgalalz opened this issue Aug 14, 2023 · 15 comments · Fixed by #8190
Assignees
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority)

Comments

@Mahmoudgalalz
Copy link

What version of astro are you using?

Latest

Are you using an SSR adapter? If so, which one?

Cloudflare

What package manager are you using?

npm

What operating system are you using?

Mac,

What browser are you using?

Chrome

Describe the Bug

I have no access to environment variables via cloudflare, and I do override the connection string an a ts file and import it in a route

What's the expected result?

I should be able to access the old getRuntime() out of the new usage Astro.Locals.runtime

as I cannot access this in the .ts files

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-wj2hw8?file=src%2Fdb.ts

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 Aug 14, 2023
@natemoo-re natemoo-re added pkg: cloudflare - P4: important Violate documented behavior or significantly impacts performance (priority) labels Aug 14, 2023
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Aug 14, 2023
@natemoo-re
Copy link
Member

Ah, I think this consideration was overlooked when switching to Astro.locals.runtime. Ideally import.meta.env should still work, though. IMO you shouldn't need to use getRuntime for this.

@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Aug 14, 2023

@Mahmoudgalalz getRuntime() should still be available as for now. We wanted to get feedback for potential issues, before removing it. However I don't quite get your issue, yet!

@matthewp
Copy link
Contributor

I think @Mahmoudgalalz is saying that this is untyped. To type the locals you do this: https://docs.astro.build/en/guides/middleware/#middleware-types

@kelvinz
Copy link

kelvinz commented Aug 22, 2023

I have a similar question, how can I access my cloudflare env variables at my API endpoint on the server?

Docs say,

import type { APIContext } from "astro";
import { getRuntime } from "@astrojs/cloudflare/runtime";

export async function get({request}: APIContext) => {
  const runtime = getRuntime(request);
  // the type KVNamespace comes from the @cloudflare/workers-types package
  const { MY_KV } = (runtime.env as { MY_KV: KVNamespace }));

  return {
    // ...
  };
};

But since getRuntime is gone, how do I access the env?

@ematipico
Copy link
Member

Probably we should write a better documentation.

Inside an .astro file:

const runtime = Astro.locals.runtime;

Inside an endpoint

export function get(context) {
	const runtime = context.locals.getRuntime();
}

@kelvinz
Copy link

kelvinz commented Aug 22, 2023

@ematipico this is the error I'm getting from ts, Property 'getRuntime' does not exist on type 'Locals'
when I try running const runtime = context.locals.getRuntime()

Also, I'm planning to do this, is this correct? const API_KEY = import.meta.env.DEV ? import.meta.env.API_KEY : runtime.env.API_KEY
So getting the api key from astro dev vs getting it from cloudflare env variables on server.

@ematipico
Copy link
Member

ematipico commented Aug 23, 2023

@ematipico this is the error I'm getting from ts, Property 'getRuntime' does not exist on type 'Locals'
when I try running const runtime = context.locals.getRuntime()

My bad, it should be just runtime.

@kelvin-zhao
Copy link

There doesn't seem to be anything in runtime

@alexanderniebuhr
Copy link
Member

There doesn't seem to be anything in runtime

Could you provide a Minimal Reproducible Example?
The CF Runtime is only available after the Requests are made.

@alexanderniebuhr
Copy link
Member

It does work for me (https://github.com/alexanderniebuhr/smoggy-spiral), maybe you can either provide a Minimal Reproducible Example or compare the difference to your code. Also note, if you try to get it working with astro dev, that will not work. But that limitation is also existence for getRuntime()

@ematipico
Copy link
Member

There doesn't seem to be anything in runtime

Is it possible you checked that while running the Dev server? If so, that's expected.

You should see the info only if you build the project and deploy it to CF.

@kelvin-zhao
Copy link

kelvin-zhao commented Aug 23, 2023

So does this
const API_KEY = import.meta.env.DEV ? import.meta.env.API_KEY : context.locals.runtime.env.API_KEY
make sense to test and run on dev to make sure everything works before pushing it to test on server?
Or is there a better way to do development for such endpoints?
Btw I'm also getting this typescript error Property 'runtime' does not exist on type 'Locals'.

I'm also wondering if there is way to use CORS or someway to ensure that this API endpoint can only be run by my project and its domain?

Really thanks for the help thus far!

@ematipico
Copy link
Member

@kelvin-zhao yeah that code makes sense

@kelvinz
Copy link

kelvinz commented Aug 23, 2023

But how do you get rid of the typescript error?

@ematipico
Copy link
Member

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants