Skip to content

Commit 33cbca1

Browse files
committed
I got it working!!!
Thanks to sveltejs/kit#2966 (comment)
1 parent 5b935a9 commit 33cbca1

10 files changed

+56
-50
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ node_modules
88
!.env.example
99
vite.config.js.timestamp-*
1010
vite.config.ts.timestamp-*
11+
kv-data/

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"singleQuote": true,
33
"printWidth": 100,
4+
"proseWrap": "always",
45
"plugins": ["prettier-plugin-svelte"],
56
"pluginSearchDirs": ["."],
67
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]

README.md

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
1-
# create-svelte
1+
# svelte-local-cloudflare-dev-working
22

3-
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
3+
This ended up just being a playground for me to figure out how to run a Sveltekit app on Cloudflare
4+
and attach to KVs, DOs, etc.
45

5-
## Creating a project
6-
7-
If you're seeing this, you've probably already done this step. Congrats!
8-
9-
```bash
10-
# create a new project in the current directory
11-
npm create svelte@latest
12-
13-
# create a new project in my-app
14-
npm create svelte@latest my-app
15-
```
16-
17-
## Developing
18-
19-
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
20-
21-
```bash
22-
npm run dev
23-
24-
# or start the server and open the app in a new browser tab
25-
npm run dev -- --open
26-
```
27-
28-
## Building
29-
30-
To create a production version of your app:
31-
32-
```bash
33-
npm run build
34-
```
35-
36-
You can preview the production build with `npm run preview`.
37-
38-
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
6+
See my SvelteKit Tips & Tricks doc, "Local development with Cloudflare."

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
{
2-
"name": "giftideas",
2+
"name": "svelte-local-cloudflare-dev-working",
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"--": "should I use --ext js,ts,svelte,html,png,gif,jpg,jpeg ?",
7-
"watch": "nodemon --ext '*' --watch src --exec \"vite build --minify false --sourcemap true\"",
8-
"dev": "wrangler pages dev .svelte-kit/cloudflare --kv GIFTS --live-reload",
6+
"dev": "vite dev",
97
"build": "vite build",
108
"preview": "vite preview",
119
"test": "playwright test",
@@ -25,6 +23,7 @@
2523
"eslint": "^8.32.0",
2624
"eslint-config-prettier": "^8.6.0",
2725
"eslint-plugin-svelte3": "^4.0.0",
26+
"miniflare": "^2.11.0",
2827
"nodemon": "^2.0.20",
2928
"prettier": "^2.8.3",
3029
"prettier-plugin-svelte": "^2.9.0",

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks.server.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { dev } from '$app/environment';
2+
import type { Handle } from '@sveltejs/kit';
3+
4+
export const handle: Handle = async ({ event, resolve }) => {
5+
if (dev) {
6+
const { fallBackPlatformToMiniFlareInDev } = await import('$lib/clients/miniflare');
7+
// @ts-expect-error I don't know why
8+
event.platform = await fallBackPlatformToMiniFlareInDev(event.platform);
9+
}
10+
return resolve(event);
11+
};

src/lib/clients/miniflare.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Miniflare, Log, LogLevel } from 'miniflare';
2+
import { dev } from '$app/environment';
3+
4+
export const fallBackPlatformToMiniFlareInDev = async (_platform: App.Platform) => {
5+
if (!dev) return _platform;
6+
7+
if (_platform) return _platform;
8+
const mf = new Miniflare({
9+
log: new Log(LogLevel.INFO),
10+
kvPersist: './kv-data', // Use filebase or in memory store
11+
kvNamespaces: ['GIFTS'], //Declare array with NameSpaces
12+
globalAsyncIO: true,
13+
globalTimers: true,
14+
globalRandom: true,
15+
16+
script: `
17+
addEventListener("fetch", (event) => {
18+
event.waitUntil(Promise.resolve(event.request.url));
19+
event.respondWith(new Response(event.request.headers.get("X-Message")));
20+
});
21+
addEventListener("scheduled", (event) => {
22+
event.waitUntil(Promise.resolve(event.scheduledTime));
23+
});
24+
`,
25+
});
26+
27+
const env = await mf.getBindings();
28+
// @ts-expect-error i don't know why
29+
const platform: App.Platform = { env };
30+
return platform;
31+
};

src/routes/+page.server.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import type { Actions, PageServerLoad } from './$types';
2-
import { redirect } from '@sveltejs/kit';
3-
import { env } from '$env/dynamic/private';
42

53
export const load: PageServerLoad = async ({ platform }) => {
64
console.log({platform});
@@ -16,16 +14,11 @@ export const load: PageServerLoad = async ({ platform }) => {
1614

1715
export const actions: Actions = {
1816
default: async ({ platform, request }) => {
19-
const promises = [];
20-
try {
2117
const data = await request.formData();
2218
const hello = data.get('hello') as string;
2319
console.log({ actions: 'yes', hello });
2420
if (hello) {
25-
promises.push(platform?.env?.GIFTS.put('hello', hello));
21+
await platform?.env?.GIFTS.put('hello', hello);
2622
}
27-
} finally {
28-
await Promise.all(promises);
2923
}
30-
},
3124
};

src/routes/+page.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<h1>Welcome to SvelteKit</h1>
1010
<p>Data.hello is now {data.hello}</p>
11-
<form method="POST" use:enhance>
11+
<form method="POST">
1212
Change it to
1313
<input name="hello" bind:value={hello} type="text" />
1414
<input type="submit" />

wrangler.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name = "giftideas"
1+
name = "svelte-local-cloudflare-dev-working"
22
compatibility_date = "2023-01-11"
33

44
[env.dev]

0 commit comments

Comments
 (0)