wasm-image-optimization 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,29 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
# usage
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Next.js image optimization with Cloudflare
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
8
|
import { optimizeImage } from 'wasm-image-optimization';
|
|
9
|
-
|
|
10
9
|
export interface Env {}
|
|
11
10
|
|
|
12
11
|
const handleRequest = async (
|
|
13
12
|
request: Request,
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
_env: Env,
|
|
14
|
+
_ctx: ExecutionContext
|
|
16
15
|
): Promise<Response> => {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const params = new URL(request.url).searchParams;
|
|
17
|
+
const url = params.get('url');
|
|
18
|
+
if (!url) {
|
|
19
|
+
return new Response('url is required', { status: 400 });
|
|
20
|
+
}
|
|
21
|
+
const width = params.get('w');
|
|
22
|
+
const quality = params.get('q');
|
|
23
|
+
const srcImage = await fetch(url).then((res) => res.arrayBuffer());
|
|
20
24
|
const image = await optimizeImage({
|
|
21
25
|
image: srcImage,
|
|
22
|
-
width:
|
|
23
|
-
|
|
26
|
+
width: width ? parseInt(width) : undefined,
|
|
27
|
+
quality: quality ? parseInt(quality) : undefined,
|
|
24
28
|
});
|
|
25
29
|
return new Response(image, {
|
|
26
30
|
headers: {
|
|
27
31
|
'Content-Type': 'image/webp',
|
|
32
|
+
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
28
33
|
},
|
|
29
34
|
});
|
|
30
35
|
};
|
|
@@ -33,3 +38,17 @@ export default {
|
|
|
33
38
|
fetch: handleRequest,
|
|
34
39
|
};
|
|
35
40
|
```
|
|
41
|
+
|
|
42
|
+
- next.config.js
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
/**
|
|
46
|
+
* @type { import("next").NextConfig}
|
|
47
|
+
*/
|
|
48
|
+
const config = {
|
|
49
|
+
images: {
|
|
50
|
+
path: 'https://xxx.yyy.workers.dev/',
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
export default config;
|
|
54
|
+
```
|