]> git.ardis.lu - alu.bz/commitdiff
Use Workers redirect instead of client-side
authorArdis Lu <ardisglu@gmail.com>
Wed, 17 May 2023 02:59:11 +0000 (19:59 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 17 May 2023 02:59:11 +0000 (19:59 -0700)
_routes.json [new file with mode: 0644]
functions/[path].js [new file with mode: 0644]
functions/api.js
index.html

diff --git a/_routes.json b/_routes.json
new file mode 100644 (file)
index 0000000..335fdfe
--- /dev/null
@@ -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 (file)
index 0000000..59d652d
--- /dev/null
@@ -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);
+}
index 76829a5da70844956f2f785c1f8831d01476ed12..47efb8f5438a1a89d7a2299616234481a5494757 100644 (file)
@@ -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);
-}
index 8bb53417958a17ae57a377eebf7971916f347a6d..49ab72a44a0162448a4dc2f71959580e7ce9ec51 100644 (file)
   <link rel="icon" href="data:,">
   <link rel="stylesheet" href="style.css">
   <title>alu.bz</title>
-  <script>
-    (async () => {
-      if (location.pathname !== '' && location.pathname !== '/') {
-        location.replace(await fetch(`/api?${location.pathname.substring(1)}`).then(r => r.text()));
-      }
-    })();
-  </script>
 </head>
 
 <body>
This page took 0.035877 seconds and 4 git commands to generate.