Skip to content

Commit d15e992

Browse files
committed
Fix speed test scaling issue, explained in README (#1)
1 parent 766cb54 commit d15e992

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ export type SpeedPoint = {
3030

3131
### Speed Test
3232

33+
A 30 MB file is hosted on a GitHub Pages site with a custom domain. The custom
34+
domain allows CORS requests, which is necessary for the speed test to work.
35+
36+
```ts
37+
const response = await fetch(
38+
`https://speed-test-files.zzzzion.com/data-30-mb.txt?timestamp=${Date.now()}`
39+
);
40+
const downloadData = await response.blob(); // Get the file as a Blob
41+
```
42+
43+
The fetch request is timed to measure the download speed.
44+
45+
The same file is then uploaded to a Cloudflare Worker, hosted at
46+
[speed-test-upload.zzzzion.workers.dev](https://speed-test-upload.zzzzion.workers.dev/).
47+
48+
```js
49+
addEventListener("fetch", (event) => {
50+
event.respondWith(handleRequest(event.request));
51+
});
52+
53+
async function handleRequest(request) {
54+
if (request.method === "POST") {
55+
const formData = await request.formData();
56+
const file = formData.get("file");
57+
return new Response("File uploaded and processed", { status: 200 });
58+
}
59+
return new Response("Method not allowed", { status: 405 });
60+
}
61+
```
62+
63+
This measures the time it takes to upload the file to Cloudflare's Edge network.
64+
65+
#### Old method
66+
3367
User's network speed is testing by generating dummy 25 MB data using a Next.js
3468
API route. The frontend requests the speed test endpoint, and measures the time
3569
it takes to download the data. It then uploads the same data back to the server

components/ui/SpeedTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const SpeedTest: React.FC<SpeedTestProps> = ({
147147
const formData = new FormData();
148148
formData.append("file", downloadData, "test-file.bin"); // Upload the downloaded data
149149

150-
// POST request to the dummy API
150+
// POST request to the Cloudflare worker
151151
await fetch("speed-test-upload.zzzzion.workers.dev", {
152152
method: "POST",
153153
body: formData,

0 commit comments

Comments
 (0)