From 83716d2cfa62a04a0661ae74118a42a19ca63992 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 16 May 2023 19:59:11 -0700 Subject: [PATCH] Use Workers redirect instead of client-side --- _routes.json | 5 +++++ functions/[path].js | 9 +++++++++ functions/api.js | 10 ---------- index.html | 7 ------- 4 files changed, 14 insertions(+), 17 deletions(-) create mode 100644 _routes.json create mode 100644 functions/[path].js diff --git a/_routes.json b/_routes.json new file mode 100644 index 0000000..335fdfe --- /dev/null +++ b/_routes.json @@ -0,0 +1,5 @@ +{ + "version": 1, + "include": ["/*"], + "exclude": ["/index.html", "/style.css", "/script.js"] +} \ No newline at end of file diff --git a/functions/[path].js b/functions/[path].js new file mode 100644 index 0000000..59d652d --- /dev/null +++ b/functions/[path].js @@ -0,0 +1,9 @@ +export async function onRequestGet({ request, env }) { + const url = new URL(request.url); + const shortUrl = url.pathname.substring(1); + const fullUrl = await env.links.get(shortUrl); + if (fullUrl === null) { + return Response.redirect(url.origin, 302); + } + return Response.redirect(fullUrl, 302); +} diff --git a/functions/api.js b/functions/api.js index 76829a5..47efb8f 100644 --- a/functions/api.js +++ b/functions/api.js @@ -79,13 +79,3 @@ export async function onRequestPost({ request, env }) { await env.links.put(shortUrl, fullUrl); return new Response(`Successfully created short URL: https://alu.bz/${shortUrl}`, { headers: { 'Content-Type': 'text/plain; charset=UTF-8' } }); } - -export async function onRequestGet({ request, env }) { - const url = new URL(request.url); - const shortUrl = url.search.substring(1); - const fullUrl = await env.links.get(shortUrl); - if (fullUrl === null) { - return new Response('https://alu.bz'); - } - return new Response(fullUrl); -} diff --git a/index.html b/index.html index 8bb5341..49ab72a 100644 --- a/index.html +++ b/index.html @@ -15,13 +15,6 @@ alu.bz - -- 2.47.3