Skip to content

Commit d8150d2

Browse files
authored
feat(images): Implement local patterns for images (#740)
1 parent b911c3e commit d8150d2

File tree

6 files changed

+386
-259
lines changed

6 files changed

+386
-259
lines changed

.changeset/shaky-kings-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
feat(images): implement localPatterns for images

examples/playground14/e2e/cloudflare.spec.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,38 @@ test.describe("playground/cloudflare", () => {
1919
await expect(res.json()).resolves.toEqual(expect.objectContaining({ PROCESS_ENV_VAR: "process.env" }));
2020
});
2121

22-
test("fetch an image allowed by remotePatterns", async ({ page }) => {
23-
const res = await page.request.get("/_next/image?url=https://avatars.githubusercontent.com/u/248818");
24-
expect(res.status()).toBe(200);
25-
expect(res.headers()).toMatchObject({ "content-type": "image/jpeg" });
22+
test.describe("remotePatterns", () => {
23+
test("fetch an image allowed by remotePatterns", async ({ page }) => {
24+
const res = await page.request.get("/_next/image?url=https://avatars.githubusercontent.com/u/248818");
25+
expect(res.status()).toBe(200);
26+
expect(res.headers()).toMatchObject({ "content-type": "image/jpeg" });
27+
});
28+
29+
test("400 when fetching an image disallowed by remotePatterns", async ({ page }) => {
30+
const res = await page.request.get("/_next/image?url=https://avatars.githubusercontent.com/u/248817");
31+
expect(res.status()).toBe(400);
32+
});
2633
});
2734

28-
test("404 when fetching an image disallowed by remotePatterns", async ({ page }) => {
29-
const res = await page.request.get("/_next/image?url=https://avatars.githubusercontent.com/u/248817");
30-
expect(res.status()).toBe(400);
35+
test.describe("localPatterns", () => {
36+
test("fetch an image allowed by localPatterns", async ({ page }) => {
37+
const res = await page.request.get("/_next/image?url=/snipp/snipp.webp?iscute=yes");
38+
expect(res.status()).toBe(200);
39+
expect(res.headers()).toMatchObject({ "content-type": "image/webp" });
40+
});
41+
42+
test("400 when fetching an image disallowed by localPatterns with wrong query parameter", async ({
43+
page,
44+
}) => {
45+
const res = await page.request.get("/_next/image?url=/snipp/snipp?iscute=no");
46+
expect(res.status()).toBe(400);
47+
});
48+
49+
test("400 when fetching an image disallowed by localPatterns without query parameter", async ({
50+
page,
51+
}) => {
52+
const res = await page.request.get("/_next/image?url=/snipp/snipp");
53+
expect(res.status()).toBe(400);
54+
});
3155
});
3256
});

examples/playground14/next.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const nextConfig = {
1919
pathname: "/u/248818",
2020
},
2121
],
22+
localPatterns: [
23+
{
24+
pathname: "/snipp/**",
25+
search: "?iscute=yes",
26+
},
27+
],
2228
},
2329
};
2430

315 KB
Binary file not shown.

0 commit comments

Comments
 (0)