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

adds cloudflare cf object to platform property #9978

Merged
merged 2 commits into from
May 19, 2023
Merged

adds cloudflare cf object to platform property #9978

merged 2 commits into from
May 19, 2023

Conversation

Spioune
Copy link
Contributor

@Spioune Spioune commented May 18, 2023

Cloudflare provides a CF object on the incoming Request (docs)
The CF object contains geolocation info similar to Vercel's X-Vercel-IP-*.

Since it is specific to Cloudflare, it is preferable to add it to the platform property.

Without this CF object, it is currently not possible to get the user location (edge function location)
It will also close this issue #5447

@changeset-bot
Copy link

changeset-bot bot commented May 18, 2023

🦋 Changeset detected

Latest commit: 43ca9ec

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-cloudflare Minor

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

@Rich-Harris Rich-Harris merged commit b5d96cb into sveltejs:master May 19, 2023
@Rich-Harris
Copy link
Member

thank you!

@205g0
Copy link

205g0 commented May 29, 2023

super nice, just found this via the changelog and before I've worked around this by adding cf to locals in hooks via a custom union type but now, this is much better

@205g0
Copy link

205g0 commented May 29, 2023

Sorry for spamming this thread but I've just tried to access platform.cf in a load function and it didn't work (I've also added cf to app.d.ts):

export const load = async ({ locals, platform }) => {
  ...
  return { cf: platform?.cf }
}

cf is undefined in production. My prior version where I added cf to locals myself worked.

@Spioune did I use it as intended?

@Spioune
Copy link
Contributor Author

Spioune commented May 29, 2023

@205g0 From what I see there is no error in your code.

Make sure you use the latest version of @sveltejs/adapter-cloudflare version 2.3.0

I just created a new project with npm create cloudflare <project_name> and it works properly.

@205g0
Copy link

205g0 commented May 30, 2023

Thanks for your reply:

Make sure you use the latest version of @sveltejs/adapter-cloudflare version 2.3.0

It was.

I just created a new project with npm create cloudflare <project_name> and it works properly.

Did you deploy it to production/Cloudflare (because you can get this cf object only when deployed to CF)? If yes, do you have a Cloudflare preview link? And deployed a +page.svelte which exposes the cf object? As said, it didn't work for me--cf was undefined.

I still use my locals work-around in hooks because it's more DRY for my use case. But for others driving by, it would be great if this feature is really working.

@205g0
Copy link

205g0 commented May 30, 2023

Ok, I've just tried it another time and it works now, seems I did something wrong the othre time.

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

Successfully merging this pull request may close these issues.

4 participants