James
03/24/2022, 12:31 AMjs
export async function onRequestGet(request) {
return new Response(`<h1>Hello world</h1>`, {headers: {'content-type': 'text/html'}});
}
simpson
03/24/2022, 12:38 AMcojo
03/24/2022, 3:36 AMfetch()
would work with a regular worker (going to try that shortly), but that it is being intercepted in a weird way by wrangler2's template-worker.ts
injected fetch
wrapper that is causing an exception outside of my code's scope. At least, that is what I see locally (calling response.status
here: https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/pages/functions/template-worker.ts#L112 breaks because I don't have a chance to call accept()
on the webSocket
yet)
Another way of phrasing my question might be - am I correct that this template is being used on the actual Cloudflare deployment as well (I tried deploying to Preview to see if maybe this was a local wrangler2 issue only but it is throwing an exception there too)? If so, is there a way I am missing to call fetch
as in the usual Cloudflare worker fetch
instead of this wrapper version? If not, will that change in the future, or do we just have to do any websocket-specific code on a separate worker env from the rest of our Page setup?
Thanks in advance!cojo
03/24/2022, 4:13 AM.accept()
call (which I do have in there immediately; just not getting that far...):
[pages:err] POST /api/admin/testSig: TypeError: Cannot read properties of undefined (reading 'status')
at next (....../node_modules/wrangler/pages/functions/template-worker.ts:112:50)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at EventTarget.[kDispatchFetch] (....../node_modules/@miniflare/core/src/standards/event.ts:359:13)
at Server.<anonymous> (....../node_modules/@miniflare/http-server/src/index.ts:168:20)
POST /api/admin/testSig 500 Internal Server Error (610.90ms)
......./node_modules/wrangler/wrangler-dist/cli.js:48070
throw ex;
^
TypeError: You must call accept() on this WebSocket before sending messages.
at EventTarget.[kClose] (......./node_modules/@miniflare/web-sockets/src/websocket.ts:164:13)
at WebSocket.<anonymous> (......./node_modules/@miniflare/web-sockets/src/couple.ts:36:30)
...
at WebSocket.emitClose (......../node_modules/@miniflare/web-sockets/node_modules/ws/lib/websocket.js:247:12)
...
at processTicksAndRejections (node:internal/process/task_queues:82:21)
cojo
03/24/2022, 4:14 AMfetch(this.address, {
headers: { Upgrade: 'websocket' },
})
.then((resp) => {
console.log('Using our connect override!');
and we never get to that log.boojum
03/25/2022, 8:02 AMfunctions/index.js
) and taking an action before serving the actual site? For example, redirecting somewhere else based on some condition. It seem like it should be possible but I can't figure this one out. The - not working! - explanation in code:
js
// i live at functions/index.js
const destinationURL = 'https://startpage.com'
const statusCode = 301
export async function onRequest(context) {
const { request } = context
const url = new URL(request.url)
if (url.pathname.startsWith('/test')) {
return Response.redirect(destinationURL, statusCode)
}
return Response
}
boojum
03/25/2022, 8:47 AMRhino233
03/25/2022, 7:22 PMIsaac McFadyen | YYZ01
03/25/2022, 7:26 PMRhino233
03/25/2022, 7:28 PMRhino233
03/25/2022, 7:28 PMRhino233
03/25/2022, 7:30 PMsolidny
03/26/2022, 2:49 PMZottelchen🍭
03/26/2022, 4:50 PMIsaac McFadyen | YYZ01
03/26/2022, 4:56 PMIsaac McFadyen | YYZ01
03/26/2022, 4:57 PMZottelchen🍭
03/26/2022, 5:01 PMIsaac McFadyen | YYZ01
03/26/2022, 5:01 PMIsaac McFadyen | YYZ01
03/26/2022, 5:01 PMZottelchen🍭
03/26/2022, 5:01 PMjohn.spurlock
03/26/2022, 5:27 PMJames
03/26/2022, 5:33 PMnpx wrangler pages functions build
is effectively what's run I believe to generate the resulting worker if you want to take a look at the result - it uses esbuild if I recall correctly. It's all open source at https://github.com/cloudflare/wrangler2 if you want to go digging. Assets for Pages are stored in KV behind the scenes, yeah. I see @User typing so he might have more specifics 👀Walshy | Pages
03/26/2022, 5:34 PMIsaac McFadyen | YYZ01
03/26/2022, 5:34 PMIsaac McFadyen | YYZ01
03/26/2022, 5:35 PMjohn.spurlock
03/26/2022, 5:46 PMIsaac McFadyen | YYZ01
03/26/2022, 6:01 PMesbuild
settings for Functions yet unfortunately (although Walshy is welcome to refute that haha)john.spurlock
03/26/2022, 7:24 PMWalshy | Pages
03/26/2022, 7:26 PM_worker.js
. Good shout by Isaac on checking wrangler2, that should match what we do todayjohn.spurlock
03/26/2022, 9:06 PM0.14.23
- but it's unclear if buildWorker
(https://github.com/cloudflare/wrangler2/blob/7e8ec9a0807deacd58cd25f5a8fd7d21b2fdb535/packages/wrangler/pages/functions/buildWorker.ts#L14) also runs on the build server to produce the worker used for deployment