From 5fbbd85b12c3572d72c6ebaf15f18ad76ec7a594 Mon Sep 17 00:00:00 2001 From: Ming Di Leom <2809763-curben@users.noreply.gitlab.com> Date: Sun, 6 Oct 2024 20:06:16 +0000 Subject: [PATCH] feat: cloudflare pages function --- _config.yml | 1 + functions/images/[[catchall]].js | 9 +++++++++ source/_routes.json | 5 +++++ wrangler.toml | 6 ++++++ 4 files changed, 21 insertions(+) create mode 100644 functions/images/[[catchall]].js create mode 100644 source/_routes.json diff --git a/_config.yml b/_config.yml index 756c29c..acbdeb0 100644 --- a/_config.yml +++ b/_config.yml @@ -40,6 +40,7 @@ skip_render: include: - "_headers" # Netlify header rule - "_redirects" # Netlify redirects rule + - "_routes.json" # Cloudflare Pages function # Writing new_post_name: :title.md # File name of new posts diff --git a/functions/images/[[catchall]].js b/functions/images/[[catchall]].js new file mode 100644 index 0000000..e13767e --- /dev/null +++ b/functions/images/[[catchall]].js @@ -0,0 +1,9 @@ +export async function onRequestGet(context) { + // const {pathname} = new URL(context.request.url) + // const imageURL = new URL(pathname, 'https://mdleom.com') + // const imageRequest = new Request(imageURL, { + // headers: context.request.headers + // }) + // return fetch(imageRequest) + return context.env.CF_IMAGES.fetch(context.request) +} diff --git a/source/_routes.json b/source/_routes.json new file mode 100644 index 0000000..0b70362 --- /dev/null +++ b/source/_routes.json @@ -0,0 +1,5 @@ +{ + "version": 1, + "include": ["/images/*"], + "exclude": [] +} diff --git a/wrangler.toml b/wrangler.toml index 6c2da45..49d6e83 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -4,4 +4,10 @@ compatibility_date = "2024-09-23" compatibility_flags = [ "nodejs_compat" ] send_metrics = false +# worker route = { pattern = "https://mdleom.com/images/*", zone_name = "mdleom.com" } + +# pages function +[[services]] +binding = "CF_IMAGES" +service = "cf-images"