feat: read assets inside an edge function - #11674
Conversation
|
| read: (file) => { | ||
| const controller = new AbortController(); | ||
| const signal = controller.signal; | ||
|
|
||
| return new ReadableStream({ | ||
| async start(controller) { | ||
| try { | ||
| const response = await fetch(manifest.appPath + file, { signal }); | ||
| const reader = /** @type {ReadableStream} */ (response.body).getReader(); | ||
|
|
||
| while (true) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) break; | ||
| controller.enqueue(value); | ||
| } | ||
|
|
||
| controller.close(); | ||
| } catch (error) { | ||
| controller.error(error); | ||
| } | ||
| }, | ||
| cancel(reason) { | ||
| controller.abort(reason); | ||
| } | ||
| }); | ||
| } |
There was a problem hiding this comment.
This should be a shared function imported from @sveltejs/kit (or @sveltejs/kit/adapter-utils or something). What is a good name for a function that synchronously returns a ReadableStream containing the body of an asynchronously fetched asset?
|
The "fetch to read asset" concept should also work for cloudflare pages I think |
|
Seems to be working quite nicely with @vercel/og on the edge https://svelte-og-image.vercel.app/ https://github.com/eltigerchino/svelte-og-image/blob/main/src/routes/og/%2Bserver.ts The image below is rendered at the edge from https://svelte-og-image.vercel.app/og?title=Svelte%20OG%20Image |
closes sveltejs#12446 closes sveltejs#11674 --------- Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>
resolves #12446
just an experiment... wanted to see if we could implement
readinside edge functions by just requesting them. might be useful in a pinchPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits