This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This gist aims to show you how to use KV datastore using Cloudflare pages. The reason I created this quick guide is it took | |
| me almost 2 weeks to get it working, mainly because it is very new and the documentation is not up fully fleshed out yet | |
| https://developers.cloudflare.com/workers/runtime-apis/kv | |
| https://developers.cloudflare.com/pages/platform/functions | |
| https://blog.cloudflare.com/wrangler-v2-beta/ | |
| Steps: | |
| Install wrangler 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export async function getJson(url, options) { | |
| try { | |
| const response = await fetch(url, options); | |
| const contentType = response.headers.get("content-type"); | |
| if (! response.ok || ! contentType.includes("application/json")) { | |
| console.log(`ERROR: getJSON -> Invalid Response -> response.status = ${ | |
| response.status | |
| }`,); | |
| console.log(`ERROR: getJSON -> Invalid Response ->response.statusText = ${ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function getFormData(request) { | |
| const { headers } = request; | |
| const contentType = headers.get("content-type") || ""; | |
| if (contentType.includes("application/json")) { | |
| return await request.json(); | |
| } | |
| if (contentType.includes("form")) { | |
| const formData = await request.formData(); |
NewerOlder