kit
kit copied to clipboard
Get assets full URL or trigger handle for fetch(asset)
Describe the problem
Expose a way in handle to get access to the final deployed path of assets. This will allow things like on the fly image optimisation.
Describe the proposed solution
hooks.server.ts can have a new method handleAssets, or can augment existing method
If you do a fetch('/image.png'), it triggers handleAssets, provides it with the full path to that image. With which I can return a custom response. handleAssets could be used to use sharp on the image on the fly, so inherently making SSR'd image optimisation a reality, like Nuxt Image
Would it be slower than static optimisation? Yes, but sometimes you just cannot optimise early, and the server costs may not matter as much
Alternatives considered
- Do nothing
Importance
would make my life easier
Additional Information
No response
If by 'asset' you mean a file in the static directory, then I'm not sure this is possible — SvelteKit doesn't see requests for these files, because the deployment platform generally intercepts them. In some cases we could build the adapters differently, so that SvelteKit did handle requests for static files, but it would come at a substantial cost:
- you potentially have to wait for a cold start, then the server has to somehow retrieve the file from storage (how? the asset URL points back to the server!), even if you're not doing any processing at all
- if your app is running in a single region rather than at the edge, then you massively increase latency for many users (at least on platforms where static assets are automatically served from a CDN)
- the server has to do way more computation — in many cases that translates directly into higher bills
It feels like you'd be better off writing an endpoint that did this — /optimise/[...asset]/+server.js or something. Unless I've misunderstood the proposal?