-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: read assets inside an edge function #11674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
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.
fetchFile
?
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 |
resolves #12446
just an experiment... wanted to see if we could implement
read
inside 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 test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits