|
1 | | -# WIP next/image loader for imgproxy s3 |
| 1 | +# `next/image` loader for imgproxy (S3) |
| 2 | + |
| 3 | +This library is a layer on top of the the `next/image` component, which allows you to load images from an [imgproxy](https://github.com/imgproxy/imgproxy) instance connected to an s3. |
| 4 | + |
| 5 | +## Sample Usage |
| 6 | + |
| 7 | +### Registering the endpoint |
| 8 | + |
| 9 | +The library proxies request through a next endpoint. To register the endpoint create a [custom server](https://nextjs.org/docs/advanced-features/custom-server) in your project and add the following lines: |
| 10 | + |
| 11 | +```js |
| 12 | +// server.js |
| 13 | +const imgProxy = require('@bitpatty/next-image-s3-imgproxy-loader'); |
| 14 | + |
| 15 | +app.prepare().then(() => { |
| 16 | + createServer((req, res) => { |
| 17 | + const parsedUrl = parse(req.url, true); |
| 18 | + const { pathname, query } = parsedUrl; |
| 19 | + |
| 20 | + if (pathname === imgProxy.IMGPROXY_ENDPOINT) { |
| 21 | + imgProxy.imageOptimizer( |
| 22 | + new URL('<url to your imgproxy instance>'), |
| 23 | + query, |
| 24 | + res, |
| 25 | + // Optional, only provide this if you use |
| 26 | + // imgproxy signatures |
| 27 | + { |
| 28 | + key: '<imgproxy secret>', |
| 29 | + salt: '<imgproxy salt>', |
| 30 | + }, |
| 31 | + ); |
| 32 | + } else { |
| 33 | + handle(req, res, parsedUrl); |
| 34 | + } |
| 35 | + }).listen(3000, (err) => { |
| 36 | + if (err) throw err; |
| 37 | + console.log('> Ready on http://localhost:3000'); |
| 38 | + }); |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +## Using the component |
| 43 | + |
| 44 | +After registering the endpoint you can use the `<ProxyImage />` component as you would with the `Image` component from `next/image`, except that you need to provide a file (`<bucketname>/<filename>`) instead of `src` and optional proxy params for image transformations/optimizations. |
| 45 | + |
| 46 | +```tsx |
| 47 | +import ProxyImage, { |
| 48 | + ImgProxyParamBuilder, |
| 49 | +} from '@bitpatty/next-image-s3-imgproxy-loader'; |
| 50 | + |
| 51 | +<ProxyImage |
| 52 | + file="mybucket/myfile.png" |
| 53 | + layout="fill" |
| 54 | + proxyParams={new ImgProxyParamBuilder().rotate(180).blur(10).build()} |
| 55 | +/>; |
| 56 | +``` |
| 57 | + |
| 58 | +## Using the raw path |
| 59 | + |
| 60 | +In case using the component is not an option, you can instead use the image path itself, by utilizing the `buildProxyImagePath` function. |
| 61 | + |
| 62 | +```tsx |
| 63 | +import { buildProxyImagePath } from '@bitpatty/next-image-s3-imgproxy-loader'; |
| 64 | + |
| 65 | +<div |
| 66 | + style={{ |
| 67 | + backgroundImage: `url(${buildProxyImagePath( |
| 68 | + 'test-bucket/test-image.png', |
| 69 | + new ImgProxyParamBuilder().blur(10).build(), |
| 70 | + )})`, |
| 71 | + backgroundSize: 'cover', |
| 72 | + }} |
| 73 | +> |
| 74 | + {/* Content */} |
| 75 | +</div>; |
| 76 | +``` |
0 commit comments