Skip to content

Commit 744b059

Browse files
committed
test: de-flake on-demand revalidation tests
it turns out... > By default, tag-based purges apply to all of the site’s deploys. To target a specific deploy, > specify one or more of the following [...] from https://docs.netlify.com/build/caching/caching-overview/#use-a-function-with-the-purgecache-helper-to-purge-by-cache-tag Whoops. So concurrent tests were conflicting with one another's expectations.
1 parent 0aa3a10 commit 744b059

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

tests/e2e/fixtures/edge-site/netlify/functions/purge-cdn.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { type Config, purgeCache } from "@netlify/functions";
1+
import { type Config, type Context, purgeCache } from "@netlify/functions";
22

3-
const handler = async (request: Request): Promise<Response> => {
3+
const handler = async (
4+
request: Request,
5+
context: Context,
6+
): Promise<Response> => {
47
const url = new URL(request.url);
58
const tagToPurge = url.searchParams.get("tag");
69

@@ -14,8 +17,17 @@ const handler = async (request: Request): Promise<Response> => {
1417
);
1518
}
1619

17-
console.log("Purging", tagToPurge);
18-
await purgeCache({ tags: [tagToPurge] });
20+
// e2e tests exclusively create manual deploys, so this assumption is safe
21+
if (!context.url.hostname.includes("--")) {
22+
return Response.json(
23+
{ error: "No deploy alias found in hostname (missing --)" },
24+
{ status: 400 },
25+
);
26+
}
27+
await purgeCache({
28+
tags: [tagToPurge],
29+
deployAlias: context.url.hostname.split("--")[0],
30+
});
1931

2032
return new Response(null, { status: 204 });
2133
};

tests/e2e/fixtures/react-router-edge-site/netlify/functions/purge-cdn.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { type Config, purgeCache } from '@netlify/functions'
1+
import { type Config, type Context, purgeCache } from '@netlify/functions'
22

3-
const handler = async (request: Request): Promise<Response> => {
3+
const handler = async (request: Request, context: Context): Promise<Response> => {
44
const url = new URL(request.url)
55
const tagToPurge = url.searchParams.get('tag')
66

@@ -14,8 +14,14 @@ const handler = async (request: Request): Promise<Response> => {
1414
)
1515
}
1616

17-
console.log('Purging', tagToPurge)
18-
await purgeCache({ tags: [tagToPurge] })
17+
// e2e tests exclusively create manual deploys, so this assumption is safe
18+
if (!context.url.hostname.includes('--')) {
19+
return Response.json({ error: 'No deploy alias found in hostname (missing --)' }, { status: 400 })
20+
}
21+
await purgeCache({
22+
tags: [tagToPurge],
23+
deployAlias: context.url.hostname.split('--')[0],
24+
})
1925

2026
return new Response(null, { status: 204 })
2127
}

tests/e2e/fixtures/react-router-serverless-site/netlify/functions/purge-cdn.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { type Config, purgeCache } from '@netlify/functions'
1+
import { type Config, type Context, purgeCache } from '@netlify/functions'
22

3-
const handler = async (request: Request): Promise<Response> => {
3+
const handler = async (request: Request, context: Context): Promise<Response> => {
44
const url = new URL(request.url)
55
const tagToPurge = url.searchParams.get('tag')
66

@@ -14,8 +14,14 @@ const handler = async (request: Request): Promise<Response> => {
1414
)
1515
}
1616

17-
console.log('Purging', tagToPurge)
18-
await purgeCache({ tags: [tagToPurge] })
17+
// e2e tests exclusively create manual deploys, so this assumption is safe
18+
if (!context.url.hostname.includes('--')) {
19+
return Response.json({ error: 'No deploy alias found in hostname (missing --)' }, { status: 400 })
20+
}
21+
await purgeCache({
22+
tags: [tagToPurge],
23+
deployAlias: context.url.hostname.split('--')[0],
24+
})
1925

2026
return new Response(null, { status: 204 })
2127
}

tests/e2e/fixtures/serverless-site/netlify/functions/purge-cdn.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { type Config, purgeCache } from "@netlify/functions";
1+
import { type Config, type Context, purgeCache } from "@netlify/functions";
22

3-
const handler = async (request: Request): Promise<Response> => {
3+
const handler = async (
4+
request: Request,
5+
context: Context,
6+
): Promise<Response> => {
47
const url = new URL(request.url);
58
const tagToPurge = url.searchParams.get("tag");
69

@@ -14,8 +17,17 @@ const handler = async (request: Request): Promise<Response> => {
1417
);
1518
}
1619

17-
console.log("Purging", tagToPurge);
18-
await purgeCache({ tags: [tagToPurge] });
20+
// e2e tests exclusively create manual deploys, so this assumption is safe
21+
if (!context.url.hostname.includes("--")) {
22+
return Response.json(
23+
{ error: "No deploy alias found in hostname (missing --)" },
24+
{ status: 400 },
25+
);
26+
}
27+
await purgeCache({
28+
tags: [tagToPurge],
29+
deployAlias: context.url.hostname.split("--")[0],
30+
});
1931

2032
return new Response(null, { status: 204 });
2133
};

0 commit comments

Comments
 (0)