-
Notifications
You must be signed in to change notification settings - Fork 1
/
_worker.js
96 lines (90 loc) · 2.95 KB
/
_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {
error, // creates error responses
json, // creates JSON responses
//html, // creates HTML responses
//StatusError,
Router, // the ~440 byte router itself
//withParams, // middleware: puts params directly on the Request
} from 'itty-router'
import { renderPage } from 'vike/server'
//import { todos } from './data/posts' //default posts if KV fails for some reason
const router = Router()
router
// add some middleware upstream on all routes to serve assets
// Pages requirement: Serve the static assets.
// Without this, the Worker will error and no assets will be served.
.all('*', (req, env) => {
const { pathname } = new URL(req.url);
if (pathname.startsWith("/assets/")) {
return env.ASSETS.fetch(req);
}
})
//add other routes, i.e. api routes, can be anything see itty docs. here we just go with /api and make sure its a post request only.
.post('/api/:message', ({ params }) => {
//console.log ("params", params.message)
const expr = params?.message
let message;
switch (expr) {
case 'hello':
message = "hello"
break;
case 'goodbye':
message = "goodbye"
break;
default:
message = "Sorry, no message"
}
const data = {
message,
};
return data
})
//everything else serve with Vike
.all('*', async (req, env) => {
const userAgent = req.headers.get('User-Agent') || ""
console.log('ua', userAgent)
const response = await handleSsr(req.url, userAgent, env)
if (response !== null) return response
})
// Cloudflare Worker module syntax
export default {
fetch: (req, env, ctx) =>
router
.handle(req, env, ctx)
.then(json) // send as JSON
.catch(error), // catch errors
}
async function handleSsr(url, userAgent, env) {
//get KV Data - you need try catch here and return a backup array in case KV failes, which it can sometimes - I've seen outages
let postsData = []
try {
const value = await env.POST_STORE.list(); //change name to throw error
postsData = value.keys
//we don't need to stop anything here, as this is just an issue with getting KV. We should set default posts here.
} catch (error) {
console.log ("error", error?.message || "KV Error Message")
//default posts. To do: place this in file
const todos = [
{ id: '1', message: 'Pet the puppy'},
{ id: '2', message: 'Pet the kitty'},
]
postsData = todos //if KV fails assign default posts
}
//"list_complete": true,
//"cacheStatus": null
//"keys" has the data
const pageContextInit = {
urlOriginal: url,
userAgent,
postsData: postsData
}
const pageContext = await renderPage(pageContextInit)
const { httpResponse } = pageContext
if (!httpResponse) {
return null
} else {
const { statusCode: status, headers } = httpResponse
const stream = httpResponse.getReadableWebStream()
return new Response(stream, { headers, status })
}
}