-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thanks to sveltejs/kit#2966 (comment)
- Loading branch information
Showing
10 changed files
with
56 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ node_modules | |
!.env.example | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* | ||
kv-data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,6 @@ | ||
# create-svelte | ||
# svelte-local-cloudflare-dev-working | ||
|
||
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte). | ||
This ended up just being a playground for me to figure out how to run a Sveltekit app on Cloudflare | ||
and attach to KVs, DOs, etc. | ||
|
||
## Creating a project | ||
|
||
If you're seeing this, you've probably already done this step. Congrats! | ||
|
||
```bash | ||
# create a new project in the current directory | ||
npm create svelte@latest | ||
|
||
# create a new project in my-app | ||
npm create svelte@latest my-app | ||
``` | ||
|
||
## Developing | ||
|
||
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: | ||
|
||
```bash | ||
npm run dev | ||
|
||
# or start the server and open the app in a new browser tab | ||
npm run dev -- --open | ||
``` | ||
|
||
## Building | ||
|
||
To create a production version of your app: | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
You can preview the production build with `npm run preview`. | ||
|
||
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment. | ||
See my SvelteKit Tips & Tricks doc, "Local development with Cloudflare." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { dev } from '$app/environment'; | ||
import type { Handle } from '@sveltejs/kit'; | ||
|
||
export const handle: Handle = async ({ event, resolve }) => { | ||
if (dev) { | ||
const { fallBackPlatformToMiniFlareInDev } = await import('$lib/clients/miniflare'); | ||
// @ts-expect-error I don't know why | ||
event.platform = await fallBackPlatformToMiniFlareInDev(event.platform); | ||
} | ||
return resolve(event); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Miniflare, Log, LogLevel } from 'miniflare'; | ||
import { dev } from '$app/environment'; | ||
|
||
export const fallBackPlatformToMiniFlareInDev = async (_platform: App.Platform) => { | ||
if (!dev) return _platform; | ||
|
||
if (_platform) return _platform; | ||
const mf = new Miniflare({ | ||
log: new Log(LogLevel.INFO), | ||
kvPersist: './kv-data', // Use filebase or in memory store | ||
kvNamespaces: ['GIFTS'], //Declare array with NameSpaces | ||
globalAsyncIO: true, | ||
globalTimers: true, | ||
globalRandom: true, | ||
|
||
script: ` | ||
addEventListener("fetch", (event) => { | ||
event.waitUntil(Promise.resolve(event.request.url)); | ||
event.respondWith(new Response(event.request.headers.get("X-Message"))); | ||
}); | ||
addEventListener("scheduled", (event) => { | ||
event.waitUntil(Promise.resolve(event.scheduledTime)); | ||
}); | ||
`, | ||
}); | ||
|
||
const env = await mf.getBindings(); | ||
// @ts-expect-error i don't know why | ||
const platform: App.Platform = { env }; | ||
return platform; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name = "giftideas" | ||
name = "svelte-local-cloudflare-dev-working" | ||
compatibility_date = "2023-01-11" | ||
|
||
[env.dev] | ||
|
33cbca1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know im a bit late, but the
// @ts-expect-error I don't know why
in hooks.server.ts is due toevent.platform
possibly beingundefined
, however thefallBackPlatformToMiniFlareInDev
function is typed to not acceptundefined
. Adding| undefined
fixes the issue:The issue inside
fallBackPlatformToMiniFlareInDev
is becauseContext
(returned by miniflare) is not inApp.Platform
. Asserting it asApp.Platform
fixes it: