|
1 | 1 | # Cloudflare Pages & R2 |
2 | 2 |
|
3 | | -Cloudflare is an excellent choice for hosting small or medium static JSON APIs — |
4 | | -especially if you’re already hosting your frontend or documentation there. |
| 3 | +Cloudflare is an excellent choice for hosting small or medium static JSON APIs — |
| 4 | +especially if you are already hosting your frontend or documentation there. |
5 | 5 |
|
6 | 6 | --- |
7 | 7 |
|
8 | | -## ✅ Using Cloudflare Pages |
| 8 | +## Using Cloudflare Pages |
9 | 9 |
|
10 | 10 | You can deploy your `api-out/` folder directly to **Cloudflare Pages**: |
11 | 11 |
|
12 | 12 | ```bash |
13 | 13 | npx wrangler pages publish api-out --project-name my-statikapi |
14 | 14 | ``` |
15 | 15 |
|
16 | | -This works great for: |
| 16 | +Cloudflare Pages supports two deployment approaches: |
| 17 | + |
| 18 | +1. **Git Integration** — connect your repository (GitHub, GitLab, or Bitbucket) directly to Cloudflare Pages. |
| 19 | + Every time you push changes to your configured branch, Cloudflare will automatically trigger a new build and redeploy your project. |
| 20 | + |
| 21 | +2. **Manual Deployment** — use the `wrangler pages publish` command to upload a local build (as shown above). |
| 22 | + This is ideal for projects built by CI/CD pipelines or for static API exports like StatikAPI. |
| 23 | + |
| 24 | +Cloudflare also supports **build hooks and webhooks**, which can trigger rebuilds automatically when your external systems (for example, StatikAPI SaaS or your backend) finish a build. |
| 25 | + |
| 26 | +> Example: You can configure a webhook in Cloudflare Pages and have your StatikAPI build process call it whenever `api-out/` is regenerated. |
| 27 | +
|
| 28 | +This approach works great for: |
17 | 29 | - Small APIs |
18 | 30 | - Public demo endpoints |
19 | | -- Combined projects (e.g., your docs + JSON API in one Pages site) |
| 31 | +- Combined projects (for example, your documentation and JSON API in one Pages site) |
20 | 32 |
|
21 | | -Your JSON files will be served from the same CDN edge network that powers Pages. |
| 33 | +Your JSON files will be served from the same global CDN edge network that powers Cloudflare Pages. |
22 | 34 |
|
23 | | -> **Example URL** |
24 | | -> ``` |
25 | | -> https://my-statikapi.pages.dev/api/users/1/index.json |
26 | | -> ``` |
| 35 | +**Example URL** |
| 36 | +``` |
| 37 | +https://my-statikapi.pages.dev/api/users/1/index.json |
| 38 | +``` |
27 | 39 |
|
28 | 40 | --- |
29 | 41 |
|
30 | | -## ⚠️ Fair Usage Policy |
| 42 | +## Fair Usage Policy |
31 | 43 |
|
32 | 44 | Cloudflare Pages is primarily designed for **websites**, not large API systems. |
33 | 45 | If you plan to serve thousands of JSON files or expect high traffic, review their **fair use policy** and bandwidth limits. |
34 | 46 |
|
35 | | -**In short:** |
36 | | -- Occasional JSON requests are fine ✅ |
37 | | -- API-like workloads with heavy access may be rate-limited or blocked ❌ |
38 | | -- Pages cache and limits vary per plan — always check the [Cloudflare Pages Terms of Service](https://developers.cloudflare.com/pages/platform/limits/) and related usage rules. |
| 47 | +**Summary:** |
| 48 | +- Occasional JSON requests are fine. |
| 49 | +- Heavy API-like workloads may be rate-limited or blocked. |
| 50 | +- Limits and caching behavior vary per plan — always review the [Cloudflare Pages Limits](https://developers.cloudflare.com/pages/platform/limits/) and Terms of Service. |
39 | 51 |
|
40 | 52 | --- |
41 | 53 |
|
42 | | -## 💡 For Larger Projects — Use R2 |
| 54 | +## For Larger Projects — Use R2 |
| 55 | + |
| 56 | +If your StatikAPI project produces many JSON files or large objects, consider hosting them in **Cloudflare R2**. |
43 | 57 |
|
44 | | -If your StatikAPI project produces **many JSON files** or large objects, host them in **Cloudflare R2**: |
| 58 | +R2 is Cloudflare’s object storage service, similar to AWS S3, designed for large data sets and high availability. |
45 | 59 |
|
46 | | -1. Upload the entire `api-out/` folder to R2: |
47 | | - ```bash |
48 | | - npx wrangler r2 object put my-bucket --file api-out |
49 | | - ``` |
| 60 | +### Upload to R2 |
50 | 61 |
|
51 | | -2. Configure public access or route it via a Worker: |
52 | | - ```js |
53 | | - export default { |
54 | | - async fetch(request) { |
55 | | - // Proxy JSON files from R2 |
56 | | - } |
57 | | - }; |
58 | | - ``` |
| 62 | +You can upload your entire `api-out/` directory: |
59 | 63 |
|
60 | | -R2 gives you: |
61 | | -- Better scalability and cost control |
62 | | -- API-friendly object storage |
63 | | -- Freedom from Pages file limits |
| 64 | +```bash |
| 65 | +npx wrangler r2 object put my-bucket --file api-out |
| 66 | +``` |
| 67 | + |
| 68 | +### Serve from R2 or via Workers |
| 69 | + |
| 70 | +You can make your R2 bucket public or serve it behind a Worker: |
| 71 | + |
| 72 | +```js |
| 73 | +export default { |
| 74 | + async fetch(request) { |
| 75 | + // Example: Proxy JSON files from R2 |
| 76 | + } |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +**Advantages of R2** |
| 81 | +- Handles large-scale APIs efficiently |
| 82 | +- No egress bandwidth fees within Cloudflare |
| 83 | +- Can integrate seamlessly with Workers and KV |
| 84 | +- Supports advanced routing and caching |
64 | 85 |
|
65 | 86 | --- |
66 | 87 |
|
67 | | -> **Best practice:** |
68 | | -> - For small JSON sets or static sites → Pages ✅ |
69 | | -> - For large datasets or API-style serving → R2 💪 |
70 | | -> - Always follow Cloudflare’s latest policies and ToS. |
| 88 | +## Considerations |
| 89 | + |
| 90 | +- For **small projects** or when combining with an existing site → use **Cloudflare Pages** |
| 91 | +- For **large datasets** or when serving JSON like an API → use **Cloudflare R2** |
| 92 | +- Always follow Cloudflare’s current **usage policies and terms** to stay within fair-use limits |
| 93 | +- You can also host your APIs directly with **StatikAPI Cloud** *(coming soon)* — a managed hosting solution built for static JSON APIs, automatic builds, and versioned deployments. |
| 94 | + [Join the waitlist](#subscription) to get early access. |
0 commit comments