I'm trying to write a component which reads a file, pulls out some metadata, and then gives a download link for an unmodified version of that file. https://docs.astro.build/en/guides/imports/#node-builtins in the documentation kinda implies that there isn't an Astro alternative to node.js fs -- is that the case? I'd rather not depend on node if I don't need to.
#Is there a platform-independent way to read files?
35 messages · Page 1 of 1 (latest)
How so? It's fundamentally the same as serving an image, right?
yes and images had to be implemented differently for each platform, sometimes by using the platform provided image services
ah
i think i kinda assumed all the big static hosts were spicy nginx serving static files 😵💫
if you're self-hosting using the node adapter, you could do something along the lines of fs.read("../dist/client/xyz.json")
for context, your files in /public get put inside dist/client
How do files in /public get handled for platforms which are particular about code vs data?
it would need slightly different code for the dev mode, you could check whether import.meta.env.DEV is true or false for that
on vercel and netlify, you have no access to them at all from the code
cloudflare provides env.assets.fetch() function
ah
when you say that they are pedantic about code vs data what does that mean specifically? do they do some sort of filtering to prevent a client from just lobbing binaries in the same directory as html/css/etc?
you know how sometimes platforms would create a container to run your code
this container would have access to only your code and run in a specific datacenter
meanwhile the client js,css, images and prerendered html gets submitted to object storage or a cdn
ohhhh i see
to be clear when I'm talking about reading files platform-independently this is still all build-time static site generation
for context I do technical writeups and I'm trying to write a custom import which pulls some metadata out of files at build time and then outputs a component with that metadata and a link to the static file
I considered writing a rollup plugin and importing normally
but that would potentially lead to colliding with other normal-er import implementations
now that I think through it a little bit more what I probably want is just "import but like always as a byte array dont do any processing"
it's possible to make something like that
it becomes problematic because you would hit the code size limits
unless you introduce separate hosting for the files, they would be inlined into code, as base64 encoded for example
if you're creating an integration, you could read the metadata at buildtime, then manually copy over the original files into client.outDir (dist/client), and calculate the final path into the metadata for your code
by integration do you mean a rollup plugin hooking some point in the file import chain?
How would I go about doing an include as byte array? I might write up a "do meta stuff at build time and just link to github cdn where the website source is" as an mvp until i get around to writing an optimization for files small enough to serve out of the host directly
ahh i think i see -- astro imports are delegated to vite which allows you to just append ?raw ?