Skip to content

Commit 9ca543d

Browse files
committed
feat: with workers
1 parent 40189e0 commit 9ca543d

File tree

8 files changed

+1565
-2131
lines changed

8 files changed

+1565
-2131
lines changed

app/plugins/ws.client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default defineNuxtPlugin((nuxtApp) => {
2+
const { loggedIn } = useUserSession()
3+
if (!loggedIn.value) return
4+
5+
const { data } = useWebSocket('/ws')
6+
const queryCache = useQueryCache()
7+
8+
watch(data, async (message) => {
9+
message = typeof message === 'string' ? message : await message.text()
10+
message = JSON.parse(message)
11+
12+
if (message.event === 'todo:created') {
13+
const todos = queryCache.getQueryData<Todo[]>(['todos']) || []
14+
queryCache.setQueryData(['todos'], [...todos, message.data])
15+
}
16+
})
17+
})

nuxt.config.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ export default defineNuxtConfig({
55
'nuxt-auth-utils',
66
'@nuxt/eslint',
77
'@pinia/nuxt',
8-
'@pinia/colada-nuxt'
8+
'@pinia/colada-nuxt',
9+
'@vueuse/nuxt'
910
],
1011
devtools: {
1112
enabled: true
1213
},
1314
future: { compatibilityVersion: 4 },
14-
compatibilityDate: '2024-07-30',
15+
compatibilityDate: '2025-01-29',
16+
nitro: {
17+
preset: 'cloudflare_durable',
18+
experimental: {
19+
websocket: true
20+
}
21+
},
1522
hub: {
1623
database: true
1724
},

package.json

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,35 @@
99
"postinstall": "nuxt prepare"
1010
},
1111
"dependencies": {
12-
"@iconify-json/simple-icons": "^1.2.20",
13-
"@nuxt/eslint": "^0.7.5",
12+
"@iconify-json/simple-icons": "^1.2.22",
13+
"@nuxt/eslint": "^0.7.6",
1414
"@nuxt/ui": "^2.21.0",
15-
"@nuxthub/core": "^0.8.11",
15+
"@nuxthub/core": "^0.8.15",
1616
"@pinia/colada": "^0.13.3",
1717
"@pinia/colada-nuxt": "^0.0.4",
1818
"@pinia/nuxt": "^0.9.0",
19-
"drizzle-kit": "^0.30.1",
20-
"drizzle-orm": "0.38.3",
19+
"@vueuse/core": "^12.5.0",
20+
"@vueuse/nuxt": "12.5.0",
21+
"drizzle-kit": "^0.30.4",
22+
"drizzle-orm": "0.39.0",
2123
"h3-zod": "^0.5.3",
22-
"nuxt": "^3.15.2",
23-
"nuxt-auth-utils": "^0.5.7",
24-
"pinia": "^2.3.0",
24+
"hookable": "^5.5.3",
25+
"nuxt": "^3.15.4",
26+
"nuxt-auth-utils": "^0.5.10",
27+
"pinia": "^2.3.1",
2528
"zod": "^3.24.1"
2629
},
2730
"devDependencies": {
2831
"@nuxt/devtools": "^1.7.0",
29-
"@nuxt/eslint-config": "^0.7.5",
30-
"eslint": "^9.18.0",
32+
"@nuxt/eslint-config": "^0.7.6",
33+
"eslint": "^9.19.0",
3134
"typescript": "5.7.3",
32-
"wrangler": "^3.102.0"
35+
"wrangler": "^3.106.0"
3336
},
34-
"packageManager": "[email protected]"
37+
"packageManager": "[email protected]",
38+
"pnpm": {
39+
"patchedDependencies": {
40+
"nitropack": "patches/nitropack.patch"
41+
}
42+
}
3543
}

patches/nitropack.patch

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
diff --git a/dist/presets/cloudflare/runtime/cloudflare-durable.mjs b/dist/presets/cloudflare/runtime/cloudflare-durable.mjs
2+
index 32ccfbbc6b0fef89fcbe8a15d73fba81fdada553..cdcd4650c86fc4dc7b871a40330e93fe3e6d1bf0 100644
3+
--- a/dist/presets/cloudflare/runtime/cloudflare-durable.mjs
4+
+++ b/dist/presets/cloudflare/runtime/cloudflare-durable.mjs
5+
@@ -8,6 +8,8 @@ const nitroApp = useNitroApp();
6+
const ws = import.meta._websocket ? wsAdapter(nitroApp.h3App.websocket) : void 0;
7+
export default createHandler({
8+
fetch(request, env, context, url) {
9+
+ context.passThroughOnException();
10+
+
11+
if (env.ASSETS && isPublicAssetURL(url.pathname)) {
12+
return env.ASSETS.fetch(request);
13+
}
14+
@@ -29,12 +31,16 @@ export class $DurableObject extends DurableObject {
15+
ws.handleDurableInit(this, state, env);
16+
}
17+
}
18+
- fetch(request) {
19+
+ async fetch(request) {
20+
if (import.meta._websocket) {
21+
return ws.handleDurableUpgrade(this, request);
22+
}
23+
return new Response("404", { status: 404 });
24+
}
25+
+ publish(topic, data) {
26+
+ console.log('publish', topic, data)
27+
+ ws.publish(topic, typeof data !== 'string' ? JSON.stringify(data) : data)
28+
+ }
29+
alarm() {
30+
this.ctx.waitUntil(
31+
nitroApp.hooks.callHook("cloudflare:durable:alarm", this)

0 commit comments

Comments
 (0)