init commit,
This commit is contained in:
171
99_references/supabase-examples/caching/with-cloudflare-workers-kv/.gitignore
vendored
Normal file
171
99_references/supabase-examples/caching/with-cloudflare-workers-kv/.gitignore
vendored
Normal file
@@ -0,0 +1,171 @@
|
||||
# Logs
|
||||
|
||||
logs
|
||||
_.log
|
||||
npm-debug.log_
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Runtime data
|
||||
|
||||
pids
|
||||
_.pid
|
||||
_.seed
|
||||
\*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
|
||||
coverage
|
||||
\*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
|
||||
\*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
|
||||
\*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
|
||||
.cache/
|
||||
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
|
||||
.temp
|
||||
.cache
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.\*
|
||||
|
||||
# wrangler project
|
||||
|
||||
.dev.vars
|
@@ -0,0 +1,62 @@
|
||||
# Use waitUntil to perform work after Cloudflare Worker returns response
|
||||
|
||||
**[📹 Video](https://egghead.io/lessons/supabase-use-waituntil-to-perform-work-after-cloudflare-worker-returns-response?af=9qsk0a)**
|
||||
|
||||
The `waitUntil` function allows us to continue performing work in our Cloudflare Worker, after a response has been sent back to the client.
|
||||
|
||||
In this lesson, we modify the `revalidate` route to send a response immediately, and then continue on to fetch new data and refresh our KV store.
|
||||
|
||||
Finally, we use the Thunder Client extension to simulate a POST request to the `revalidate` route, and confirm that this receives a response before the cache is updated.
|
||||
|
||||
## Code Snippets
|
||||
|
||||
**Update Revalidate route to respond immediately**
|
||||
|
||||
```javascript
|
||||
router.post(
|
||||
"/revalidate",
|
||||
withContent,
|
||||
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY, ARTICLES }, context) => {
|
||||
const updateCache = async () => {
|
||||
const { type, record, old_record } = request.content;
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
||||
|
||||
if (type === "INSERT" || type === "UPDATE") {
|
||||
await writeTo(ARTICLES, `/articles/${record.id}`, record);
|
||||
}
|
||||
|
||||
if (type === "DELETE") {
|
||||
await ARTICLES.delete(`/articles/${old_record.id}`);
|
||||
}
|
||||
|
||||
const { data: articles } = await supabase.from("articles").select("*");
|
||||
await writeTo(ARTICLES, "/articles", articles);
|
||||
console.log("updated cache");
|
||||
};
|
||||
|
||||
context.waitUntil(updateCache());
|
||||
|
||||
console.log("sending response");
|
||||
|
||||
return json({ received: true });
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
**Run wrangler development server**
|
||||
|
||||
```bash
|
||||
npx wrangler dev
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- [Cloudflare waitUntil docs](https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/)
|
||||
- [Supabase.js docs](https://github.com/supabase/supabase-js)
|
||||
- [Wrangler CLI docs](https://developers.cloudflare.com/workers/wrangler/commands/)
|
||||
- [KV Storage docs](https://developers.cloudflare.com/workers/runtime-apis/kv/)
|
||||
- [Thunder Client VS Code extension](https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client)
|
||||
|
||||
---
|
||||
|
||||
Enjoying the course? Follow Jon Meyers on [Twitter](https://twitter.com/jonmeyers_io) and subscribe to the [YouTube channel](https://www.youtube.com/c/jonmeyers).
|
3118
99_references/supabase-examples/caching/with-cloudflare-workers-kv/package-lock.json
generated
Normal file
3118
99_references/supabase-examples/caching/with-cloudflare-workers-kv/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "supabase-at-the-edge",
|
||||
"version": "0.0.0",
|
||||
"devDependencies": {
|
||||
"wrangler": "2.20.1"
|
||||
},
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "wrangler dev",
|
||||
"deploy": "wrangler publish"
|
||||
},
|
||||
"dependencies": {
|
||||
"@supabase/supabase-js": "^1.35.4",
|
||||
"itty-router": "^2.6.1",
|
||||
"itty-router-extras": "^0.4.2"
|
||||
}
|
||||
}
|
@@ -0,0 +1,100 @@
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
import { Router } from "itty-router";
|
||||
import { json, status, withContent } from "itty-router-extras";
|
||||
import { readFrom, writeTo } from "./utils/cache";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.get("/read-kv", async (request, { ARTICLES }) => {
|
||||
const articles = await readFrom(ARTICLES, "/articles");
|
||||
return json(articles);
|
||||
});
|
||||
|
||||
router.get("/write-kv", async (request, { ARTICLES }) => {
|
||||
const articles = [{ title: "test3" }, { title: "test4" }];
|
||||
await writeTo(ARTICLES, "/articles", articles);
|
||||
|
||||
return json(articles);
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/articles",
|
||||
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY, ARTICLES }) => {
|
||||
const cachedArticles = await readFrom(ARTICLES, "/articles");
|
||||
|
||||
if (cachedArticles) {
|
||||
console.log("sending the cache");
|
||||
return json(cachedArticles);
|
||||
}
|
||||
|
||||
console.log("fetching fresh articles");
|
||||
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
||||
|
||||
const { data } = await supabase.from("articles").select("*");
|
||||
await writeTo(ARTICLES, "/articles", data);
|
||||
return json(data);
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/articles/:id",
|
||||
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY, ARTICLES }) => {
|
||||
const { id } = request.params;
|
||||
const cachedArticle = await readFrom(ARTICLES, `/articles/${id}`);
|
||||
|
||||
if (cachedArticle) {
|
||||
console.log("sending the cache");
|
||||
return json(cachedArticle);
|
||||
}
|
||||
|
||||
console.log("fetching fresh article");
|
||||
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
||||
|
||||
const { data } = await supabase
|
||||
.from("articles")
|
||||
.select("*")
|
||||
.match({ id })
|
||||
.single();
|
||||
|
||||
await writeTo(ARTICLES, `/articles/${id}`, data);
|
||||
|
||||
return json(data);
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/revalidate",
|
||||
withContent,
|
||||
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY, ARTICLES }, context) => {
|
||||
const updateCache = async () => {
|
||||
const { type, record, old_record } = request.content;
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
||||
|
||||
if (type === "INSERT" || type === "UPDATE") {
|
||||
await writeTo(ARTICLES, `/articles/${record.id}`, record);
|
||||
}
|
||||
|
||||
if (type === "DELETE") {
|
||||
await ARTICLES.delete(`/articles/${old_record.id}`);
|
||||
}
|
||||
|
||||
const { data: articles } = await supabase.from("articles").select("*");
|
||||
await writeTo(ARTICLES, "/articles", articles);
|
||||
console.log("updated cache");
|
||||
};
|
||||
|
||||
context.waitUntil(updateCache());
|
||||
|
||||
console.log("sending response");
|
||||
|
||||
return json({ received: true });
|
||||
}
|
||||
);
|
||||
|
||||
router.all("*", () => status(404, "Not found"));
|
||||
|
||||
export default {
|
||||
fetch: router.handle,
|
||||
};
|
@@ -0,0 +1,8 @@
|
||||
export const readFrom = async (cache, path) => {
|
||||
const data = await cache.get(path);
|
||||
return JSON.parse(data);
|
||||
};
|
||||
|
||||
export const writeTo = async (cache, path, data) => {
|
||||
await cache.put(path, JSON.stringify(data));
|
||||
};
|
@@ -0,0 +1,7 @@
|
||||
name = "supabase-at-the-edge"
|
||||
main = "src/index.js"
|
||||
compatibility_date = "2022-07-07"
|
||||
|
||||
kv_namespaces = [
|
||||
{ binding = "replace-with-your-kv-name", id = "replace-with-your-kv-production-id", preview_id = "replace-with-your-kv-preview-id" }
|
||||
]
|
Reference in New Issue
Block a user