|
| 1 | +# Images |
| 2 | + |
| 3 | +The Epic Stack distinguishes between user images and static app image assets. |
| 4 | +User images are stored in SQLite along with the rest of the application data, |
| 5 | +while static images are stored in the [../public/img/](../public/img/) folder. |
| 6 | +See the [images decision doc](./decisions/018-images.md) for more details. |
| 7 | + |
| 8 | +## User Images |
| 9 | + |
| 10 | +User images (uploaded by a user) are stored in SQLite and served via resource |
| 11 | +routes, for example: |
| 12 | +[/resources/user-images/$imageId](../app/routes/resources+/user-images.$imageId.tsx). |
| 13 | + |
| 14 | +## Image Optimization |
| 15 | + |
| 16 | +The Epic Stack uses [openimg](https://github.com/andrelandgraf/openimg) to |
| 17 | +optimize images on demand, introduced via |
| 18 | +[this decision doc](./decisions/039-image-optimization.md). |
| 19 | + |
| 20 | +### Server Part |
| 21 | + |
| 22 | +The [/resources/images](../app/routes/resources+/images.tsx) endpoint accepts |
| 23 | +the search parameters `src`, `w` (width), `h` (height), `format`, and `fit` to |
| 24 | +perform image transformations and serve optimized variants. The transformations |
| 25 | +are performed with `sharp`, and the optimized images are cached in |
| 26 | +`./data/images` on the filesystem and via HTTP caching. |
| 27 | + |
| 28 | +### Client Part |
| 29 | + |
| 30 | +On the client side, the `Img` React component from openimg/react is used to |
| 31 | +query the [/resources/images](../app/routes/resources+/images.tsx) endpoint with |
| 32 | +the appropriate query parameters, including the source image string. The |
| 33 | +component renders a picture element that requests modern formats and sets |
| 34 | +attributes such as `fetchpriority`, `loading`, and `decoding` to optimize image |
| 35 | +loading. It also computes `srcset` and `sizes` based on the provided `width` and |
| 36 | +`height` props. Use the `isAboveFold` prop on the `Img` component to priotize |
| 37 | +images that should load immediately. |
| 38 | + |
| 39 | +### Image Sources |
| 40 | + |
| 41 | +If you want to add a new image storage location, like an S3 bucket, update the |
| 42 | +[/resources/images](../app/routes/resources+/images.tsx) endpoint and modify the |
| 43 | +`getSource` function to instruct openimg on how to retrieve the source images |
| 44 | +from the new location. Currently, the endpoint uses fetch requests to retrieve |
| 45 | +user images from the resource route endpoints and the filesystem to retrieve |
| 46 | +static application assets from the public and assets folders. |
0 commit comments