Skip to content

Commit 1af40c1

Browse files
authored
add instructions on how to use an API endpoint instead of a custom server
1 parent 765cf73 commit 1af40c1

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

README.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ npm install --save @bitpatty/next-image-s3-imgproxy-loader
2020

2121
### Registering the endpoint
2222

23-
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:
23+
#### Method 1: Custom Server
24+
25+
The library by default proxies request through a custom 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:
2426

2527
```js
2628
// server.js
@@ -69,6 +71,38 @@ app.prepare().then(() => {
6971
});
7072
```
7173

74+
#### Method 2: API Endpoint
75+
76+
For serverless environments you can use an API endpoint instead of a custom endpoint.
77+
78+
The setup is similar, register your endpoint as follows:
79+
80+
```typescript
81+
// pages/api/image.ts
82+
import { NextApiRequest, NextApiResponse } from 'next';
83+
import { handle } from '@bitpatty/next-image-s3-imgproxy-loader';
84+
85+
const handler = (req: NextApiRequest, res: NextApiResponse): void => {
86+
if (req.method !== 'GET') {
87+
res.statusCode = 405;
88+
res.send('');
89+
return;
90+
}
91+
92+
handle(new URL('http://localhost:4000/'), req.query, res, {
93+
signature: {
94+
key: '91bdcda48ce22cd7d8d3a0eda930b3db1762bc1cba5dc13542e723b68fe55d6f9d18199cbe35191a45faf22593405cad0fe76ffec67d24f8aee861ac8fe44d96',
95+
salt: '72456c286761260f320391fe500fcec53755958dabd288867a6db072e1bc1dbd84b15079838a83a715edc1ecad50c3ce91dd8fdef6f981816fa274f91d8ecf06',
96+
},
97+
bucketWhitelist: ['test-bucket'],
98+
});
99+
};
100+
101+
export default handler;
102+
```
103+
104+
With this method, you have to [supply the endpoint](#overriding-the-endpoint) to the `<ProxyImage>` component.
105+
72106
## Using the component
73107

74108
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.
@@ -122,17 +156,7 @@ const imagePath = buildProxyImagePath('test-bucket/test-image.png', {
122156

123157
## Overriding the endpoint
124158

125-
You can override the default endpoint address or use an absolute address in the component instead.
126-
127-
```js
128-
// server.js
129-
createServer((req, res) => {
130-
// ...
131-
if (pathname === '/my-endpoint') {
132-
// ...
133-
}
134-
}
135-
```
159+
If you use a different endpoint than the one provided by `IMGPROXY_ENDPOINT` you can override the endpoint used by the component by providing the `endpoint` property. `endpoint` can be both a path but also a URL.
136160

137161
```tsx
138162
<ProxyImage file="mybucket/myfile.png" endpoint="/my-endpoint" />;

example/pages/api/image.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NextApiRequest, NextApiResponse } from 'next';
2+
import { handle } from '../../../dist';
3+
4+
const handler = (req: NextApiRequest, res: NextApiResponse): void => {
5+
if (req.method !== 'GET') {
6+
res.statusCode = 405;
7+
res.send('');
8+
return;
9+
}
10+
11+
handle(new URL('http://localhost:4000/'), req.query, res, {
12+
signature: {
13+
key: '91bdcda48ce22cd7d8d3a0eda930b3db1762bc1cba5dc13542e723b68fe55d6f9d18199cbe35191a45faf22593405cad0fe76ffec67d24f8aee861ac8fe44d96',
14+
salt: '72456c286761260f320391fe500fcec53755958dabd288867a6db072e1bc1dbd84b15079838a83a715edc1ecad50c3ce91dd8fdef6f981816fa274f91d8ecf06',
15+
},
16+
bucketWhitelist: ['test-bucket'],
17+
});
18+
};
19+
20+
export default handler;

0 commit comments

Comments
 (0)