Skip to content

Commit

Permalink
Change default imgproxy middleware path
Browse files Browse the repository at this point in the history
  • Loading branch information
esdete2 committed Jul 19, 2024
1 parent 3b8b960 commit a4f4f55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ Unfortunately, it's not possible to pass a loader function directly to the next
// imageLoader.ts
import { imgProxyLoader } from '@networkteam/zebra-utils';

export default imgProxyLoader('_img');
export default imgProxyLoader('_image');
```

The `imgProxyLoader` creates a path with the provided width and quality (through the next image), as well as the base64 encoded src. The function takes a path segment (`_img` by default) which has to match the path segment in the following middleware.
The `imgProxyLoader` creates a path with the provided width and quality (through the next image), as well as the base64 encoded src. The function takes a path segment (`_image` by default) which has to match the path segment in the following middleware.

### Middleware

Expand All @@ -133,19 +133,19 @@ import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export const middleware = async (request: NextRequest) => {
if (request.nextUrl.pathname.startsWith('/_img/')) {
if (request.nextUrl.pathname.startsWith('/_image/')) {
return imgProxyMiddleware(request);
}

return NextResponse.next();
};

export const config = {
matcher: '/_img/:path*',
matcher: '/_image/:path*',
};
```

The middleware catches all paths starting with `/_img`, so all paths created by the imgproxy loader. Make sure the path segment matches the provided path segment of the imgproxy loader. Besides the request, `imgProxyMiddleware` takes `ImgProxyMiddlewareOptions`, where the allowedWidths could be overwritten.
The middleware catches all paths starting with `/_image`, so all paths created by the imgproxy loader. Make sure the path segment matches the provided path segment of the imgproxy loader. Besides the request, `imgProxyMiddleware` takes `ImgProxyMiddlewareOptions`, where the allowedWidths could be overwritten.

### Next config

Expand Down
2 changes: 1 addition & 1 deletion src/imgproxy/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { urlSafeBase64 } from './utils';
import { ImageLoaderProps } from 'next/image';

const imgProxyLoader =
(pathSegment: string = '_img') =>
(pathSegment: string = '_image') =>
({ src, width, quality }: ImageLoaderProps) => {
// If the source is not an S3 URL, return the original source
if (!src.startsWith('s3://')) return src;
Expand Down

0 comments on commit a4f4f55

Please sign in to comment.