Skip to content

Commit

Permalink
added usage description to changeset and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
AirBorne04 committed Oct 26, 2022
1 parent d82ab96 commit a134e6e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions .changeset/poor-frogs-clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
---

enable access to cloudflare runtime [KV, R2, Durable Objects]
- access native cloudflare runtime through `import { getRuntime } from "@astrojs/cloudflare/runtime"` now you can call `getRuntime(Astro.request)` and get access to the runtime environment
12 changes: 12 additions & 0 deletions packages/integrations/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ $ pnpm install wrangler --save-dev

It's then possible to update the preview script in your `package.json` to `"preview": "wrangler pages dev ./dist"`.This will allow you run your entire application locally with [Wrangler](https://github.com/cloudflare/wrangler2), which supports secrets, environment variables, KV namespaces, Durable Objects and [all other supported Cloudflare bindings](https://developers.cloudflare.com/pages/platform/functions/#adding-bindings).

## Access to the cloudflare runtime

You have the posibility to access all the cloudflare bindings and environment variables from your astro pages and api routes through the adapter API

```
import { getRuntime } from "@astrojs/cloudflare/runtime";
getRuntime(Astro.request);
```

Depending your adapter mode (advanced = worker, directory = pages) the runtime object will look a little different due to the difference in the cloudflare API.

## Streams

Some integrations such as [React](https://github.com/withastro/astro/tree/main/packages/integrations/react) rely on web streams. Currently Cloudflare Pages Functions require enabling a flag to support Streams.
Expand Down
5 changes: 1 addition & 4 deletions packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
"exports": {
".": "./dist/index.js",
"./runtime": {
"import": "./dist/runtime.js",
"types": "./dist/runtime.d.ts"
},
"./runtime": "./dist/runtime.js",
"./server.advanced.js": "./dist/server.advanced.js",
"./server.directory.js": "./dist/server.directory.js",
"./package.json": "./package.json"
Expand Down
12 changes: 10 additions & 2 deletions packages/integrations/cloudflare/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export type PagesRuntime<T = unknown, U = unknown> = {
next(request: Request): void;
};

export function getRuntime<T = unknown, U = unknown>(request: Request): WorkerRuntime<T> | PagesRuntime<T, U> {
return Reflect.get(request, Symbol.for('runtime'));
export function getRuntime<T = unknown, U = unknown>(
request: Request
): WorkerRuntime<T> | PagesRuntime<T, U> {
if (!!request) {
return Reflect.get(request, Symbol.for('runtime'));
} else {
throw new Error(
'To retrieve the current cloudflare runtime you need to pass in the Astro request object'
);
}
}

0 comments on commit a134e6e

Please sign in to comment.