Skip to content

Conversation

@acoyfellow
Copy link
Contributor

Adds a full example for deploying SvelteKit apps to Cloudflare Workers with Alchemy

- Add SvelteKit resource with Cloudflare adapter integration
- Include KV and R2 bindings example
- Complete demo with styling and resource access
- Follow standard Alchemy example patterns
@sam-goodwin sam-goodwin changed the title docs: add SvelteKit on Cloudflare Workers guide using Alchemy feat(cloudflare): add SvelteKit on Cloudflare Workers guide using Alchemy Jun 3, 2025
@sam-goodwin
Copy link
Collaborator

Boom! And now we have SvelteKit 🔥

- Import path module for constructing paths
- Correct the main entry point to use the SvelteKit adapter's generated path
- Clarify comments regarding asset directory and compatibility flags
- Demonstrate API routes

- Update Svelte component to reflect updated data structure and improve user experience
Comment on lines 105 to 116
## Using Cloudflare Bindings

In your SvelteKit routes, access Cloudflare resources via `platform.env`:

```ts
// +page.server.ts
export const load = async ({ platform }) => {
const kvData = await platform?.env?.AUTH_STORE?.get('some-key');
const r2Object = await platform?.env?.STORAGE?.get('some-file');
return { kvData };
};
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also:

import { env } from "cloudflare:workers"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works fine at runtime, but SK's SSR build tries to import it in node which doesn't understand the cloudflare: protocol

I tried using Alchemy's cloudflareWorkersDevEnvironmentShim() plugin, but it only applies during dev (apply: "serve").

Any ideas for making the import work during build, or should we stick with platform.env for SSR routes?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's very interesting. Maybe cloudflare's template works around that? Otherwise I don't have a solution. It would need to run it in miniflare.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 6, 2025

Open in StackBlitz

npm i https://pkg.pr.new/sam-goodwin/alchemy@271

commit: ad323cc

@sam-goodwin
Copy link
Collaborator

@claude please review this PR and provide helpful comments throughout the code.

@claude
Copy link
Contributor

claude bot commented Jun 7, 2025

Claude encountered an error —— View job

Failed with exit code 128

I'll analyze this and get back to you.

@acoyfellow
Copy link
Contributor Author

That's odd.

stderr: "fatal: could not open 'examples/cloudflare-sveltekit/vite.config.ts' for reading: No such file or directory\n"

I see https://github.com/acoyfellow/alchemy/blob/cloudflare-sveltekit-example/examples/cloudflare-sveltekit/vite.config.ts

@sam-goodwin
Copy link
Collaborator

That's odd.

stderr: "fatal: could not open 'examples/cloudflare-sveltekit/vite.config.ts' for reading: No such file or directory\n"

I see https://github.com/acoyfellow/alchemy/blob/cloudflare-sveltekit-example/examples/cloudflare-sveltekit/vite.config.ts

Wonder if it it's because it's a fork?

@sam-goodwin sam-goodwin merged commit c516c47 into alchemy-run:main Jun 9, 2025
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.

2 participants