Skip to content

Commit 82f548a

Browse files
committed
Add functions examples section
1 parent 91f0928 commit 82f548a

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed
File renamed without changes.

content/pages/platform/functions/10_pricing-and-limits.md renamed to content/pages/platform/functions/11_pricing-and-limits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
pcx-content-type: how-to
33
title: Pricing and limits
4-
weight: 10
4+
weight: 11
55
---
66

77
# Pricing and limits
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
type: overview
3+
hideChildren: true
4+
pcx-content-type: navigation
5+
title: Functions examples
6+
weight: 8
7+
layout: list
8+
---
9+
10+
# Functions examples
11+
12+
13+
{{<directory-listing>}}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
type: example
3+
summary: Set up an A/B test by controlling what page is served based on
4+
cookies. This version supports passing the request through to test and control
5+
on the origin.
6+
tags:
7+
- Originless
8+
pcx-content-type: configuration
9+
title: A/B testing with middleware
10+
weight: 1001
11+
layout: example
12+
---
13+
14+
```js
15+
---
16+
filename: /functions/_middleware.js
17+
---
18+
const cookieName = "ab-test-cookie"
19+
const newHomepagePathName = "/test"
20+
21+
const abTest = async ({ request, next, env }) => {
22+
const url = new URL(request.url)
23+
// if homepage
24+
if (url.pathname === "/") {
25+
// if cookie ab-test-cookie=new then change the request to go to /test
26+
// if no cookie set, pass x% of traffic and set a cookie value to "current" or "new"
27+
28+
let cookie = request.headers.get("cookie")
29+
// is cookie set?
30+
if (cookie && cookie.includes(`${cookieName}=new`)) {
31+
// pass the request to /test
32+
url.pathname = newHomepagePathName
33+
return env.ASSETS.fetch(url)
34+
} else {
35+
const percentage = Math.floor(Math.random() * 100)
36+
let version = "current" // default version
37+
// change pathname and version name for 50% of traffic
38+
if (percentage < 50) {
39+
url.pathname = newHomepagePathName
40+
version = "new"
41+
}
42+
// get the static file from ASSETS, and attach a cookie
43+
const asset = await env.ASSETS.fetch(url)
44+
let response = new Response(asset.body, asset)
45+
response.headers.append("Set-Cookie", `${cookieName}=${version}; path=/`)
46+
return response
47+
}
48+
}
49+
return next()
50+
};
51+
52+
export const onRequest = [abTest];
53+
```

content/pages/platform/functions/8_advanced-mode.md renamed to content/pages/platform/functions/9_advanced-mode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
pcx-content-type: how-to
33
title: Advanced Mode
4-
weight: 8
4+
weight: 9
55
---
66

77
# Advanced mode

0 commit comments

Comments
 (0)