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

[Cloudflare integration] Expose cf metadata and Cloudflare caches API #7386

Merged
merged 6 commits into from
Jun 13, 2023

Conversation

beynar
Copy link
Contributor

@beynar beynar commented Jun 13, 2023

Changes

This pull request expose the Cloudflare caches API and cf object (request metadata provided by Cloudflare's edge) to the runtime object.

Example

Cloudflare add some usefull informations about the request such as geolocation properties see the docs

const runtime = getRuntime(Astro.request);
const visitor_country  = runtime.cf?.country;

The Cloudflare cache API can now be used.

const runtime = getRuntime(Astro.request);
const cachedObject = await runtime.caches?.default.match(Astro.request);

@changeset-bot
Copy link

changeset-bot bot commented Jun 13, 2023

🦋 Changeset detected

Latest commit: 1204818

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Jun 13, 2023
@matthewp
Copy link
Contributor

I think this sounds good. When #7385 lands do you think it would make sense to deprecate getRuntime and instead put this stuff on Astro.locals?

@beynar
Copy link
Contributor Author

beynar commented Jun 13, 2023

Yes, it will be very beneficial and makes more sense imo. I'm actually doing it in a middleware.

Also getRuntime is wrongly typed and returns undefined in dev mode. I would have suggest to return a default ( Reflect.get(request, Symbol.for("runtime")) || { name: "cloudflare", env: {}, waitUntil: (cb) => Promise.resolve(cb) }) or to type its return as potentially undefined.

@matthewp matthewp merged commit 6d8aa4b into withastro:main Jun 13, 2023
@astrobot-houston astrobot-houston mentioned this pull request Jun 13, 2023
ematipico pushed a commit that referenced this pull request Feb 7, 2025
…#7386)

* Add cf and cache properties to runtime

* add changeset

* reorder import

* fix types and add tests

* fix package name

* test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants