@astrojs/cloudflare
Advanced tools
Comparing version 3.1.0 to 3.1.1
# @astrojs/cloudflare | ||
## 3.1.1 | ||
### Patch Changes | ||
- [#5103](https://github.com/withastro/astro/pull/5103) [`d151d9f3f`](https://github.com/withastro/astro/commit/d151d9f3f29c0a57c59b8029a18717808ccc7f8f) Thanks [@AirBorne04](https://github.com/AirBorne04)! - 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. | ||
## 3.1.0 | ||
@@ -4,0 +11,0 @@ |
@@ -7,8 +7,9 @@ import './shim.js'; | ||
}; | ||
name: string; | ||
}; | ||
export declare function createExports(manifest: SSRManifest): { | ||
default: { | ||
fetch: (request: Request, env: Env) => Promise<Response>; | ||
fetch: (request: Request, env: Env, context: any) => Promise<Response>; | ||
}; | ||
}; | ||
export {}; |
@@ -5,3 +5,3 @@ import "./shim.js"; | ||
const app = new App(manifest, false); | ||
const fetch = async (request, env) => { | ||
const fetch = async (request, env, context) => { | ||
const { origin, pathname } = new URL(request.url); | ||
@@ -19,2 +19,3 @@ if (manifest.assets.has(pathname)) { | ||
); | ||
Reflect.set(request, Symbol.for("runtime"), { env, name: "cloudflare", ...context }); | ||
let response = await app.render(request, routeData); | ||
@@ -21,0 +22,0 @@ if (app.setCookieHeaders) { |
import './shim.js'; | ||
import type { SSRManifest } from 'astro'; | ||
export declare function createExports(manifest: SSRManifest): { | ||
onRequest: ({ request, next, }: { | ||
onRequest: ({ request, next, ...runtimeEnv }: { | ||
request: Request; | ||
next: (request: Request) => void; | ||
}) => Promise<void | Response>; | ||
} & Record<string, unknown>) => Promise<void | Response>; | ||
}; |
@@ -7,3 +7,4 @@ import "./shim.js"; | ||
request, | ||
next | ||
next, | ||
...runtimeEnv | ||
}) => { | ||
@@ -22,2 +23,7 @@ const { origin, pathname } = new URL(request.url); | ||
); | ||
Reflect.set(request, Symbol.for("runtime"), { | ||
...runtimeEnv, | ||
name: "cloudflare", | ||
next | ||
}); | ||
let response = await app.render(request, routeData); | ||
@@ -24,0 +30,0 @@ if (app.setCookieHeaders) { |
{ | ||
"name": "@astrojs/cloudflare", | ||
"description": "Deploy your site to cloudflare pages functions", | ||
"version": "3.1.0", | ||
"description": "Deploy your site to cloudflare workers or cloudflare pages", | ||
"version": "3.1.1", | ||
"type": "module", | ||
@@ -22,2 +22,3 @@ "types": "./dist/index.d.ts", | ||
".": "./dist/index.js", | ||
"./runtime": "./dist/runtime.js", | ||
"./server.advanced.js": "./dist/server.advanced.js", | ||
@@ -31,3 +32,3 @@ "./server.directory.js": "./dist/server.directory.js", | ||
"devDependencies": { | ||
"astro": "1.5.0", | ||
"astro": "1.6.0", | ||
"astro-scripts": "0.0.8", | ||
@@ -34,0 +35,0 @@ "chai": "^4.3.6", |
@@ -71,9 +71,23 @@ # @astrojs/cloudflare | ||
## Access to the Cloudflare runtime | ||
You can access all the Cloudflare bindings and environment variables from Astro components and API routes through the adapter API. | ||
``` | ||
import { getRuntime } from "@astrojs/cloudflare/runtime"; | ||
getRuntime(Astro.request); | ||
``` | ||
Depending on your adapter mode (advanced = worker, directory = pages), the runtime object will look a little different due to differences 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 are in beta and don't support the `streams_enable_constructors` feature flag. | ||
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. | ||
In order to work around this: | ||
- install the `"web-streams-polyfill"` package | ||
- add `import "web-streams-polyfill/es2018";` to the top of the front matter of every page which requires streams, such as server rendering a React component. | ||
To do this: | ||
- go to the Cloudflare Pages project | ||
- click on Settings in the top bar, then Functions in the sidebar | ||
- scroll down to Compatibility Flags, click Configure Production Compatibility Flags, and add `streams_enable_constructors` | ||
- do this for both the Production Compatibility Flags and Preview Compatibility Flags | ||
@@ -80,0 +94,0 @@ ## Environment Variables |
@@ -8,2 +8,3 @@ import './shim.js'; | ||
ASSETS: { fetch: (req: Request) => Promise<Response> }; | ||
name: string; | ||
}; | ||
@@ -14,3 +15,3 @@ | ||
const fetch = async (request: Request, env: Env) => { | ||
const fetch = async (request: Request, env: Env, context: any) => { | ||
const { origin, pathname } = new URL(request.url); | ||
@@ -31,2 +32,3 @@ | ||
); | ||
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context }); | ||
let response = await app.render(request, routeData); | ||
@@ -33,0 +35,0 @@ |
@@ -12,8 +12,8 @@ import './shim.js'; | ||
next, | ||
...runtimeEnv | ||
}: { | ||
request: Request; | ||
next: (request: Request) => void; | ||
}) => { | ||
} & Record<string, unknown>) => { | ||
const { origin, pathname } = new URL(request.url); | ||
// static assets | ||
@@ -32,2 +32,7 @@ if (manifest.assets.has(pathname)) { | ||
); | ||
Reflect.set(request, Symbol.for('runtime'), { | ||
...runtimeEnv, | ||
name: 'cloudflare', | ||
next, | ||
}); | ||
let response = await app.render(request, routeData); | ||
@@ -34,0 +39,0 @@ |
Sorry, the diff of this file is not supported yet
38818
31
586
119