diff --git a/functions/redirect.js b/functions/redirect.js
index 383586e..3904736 100644
--- a/functions/redirect.js
+++ b/functions/redirect.js
@@ -1,13 +1,29 @@
-export async function onRequest({env, request}) {
- const base = env.base || 'https://2fa.pages.dev/'
- const country = request.cf?.country.toLowerCase() || "int";
- let uri = `${base}${country}`
- const res = await fetch(uri, {
- cf: {
- cacheTtl: 86400, cacheEverything: true
+export async function onRequestGet({request}) {
+ const url = new URL(request.url);
+ const base = `${url.protocol}//${url.hostname}/`;
+ const redirectStatus = 302;
+ try {
+ const country = request.cf?.country?.toLowerCase() || 'int';
+ let uri = `${base}${country}/`;
+
+ const res = await fetch(uri, {
+ cf: {
+ cacheTtlByStatus: {
+ '200': 60 * 60 * 24 * 7, // Cache request 1 week
+ '404': 60 * 60 * 24, // Cache request 1 day
+ },
+ },
+ });
+
+ // Redirect to /int/ if that page works
+ if (res.status !== 200) {
+ const int = await fetch(`${base}int/`);
+ uri = int.status === 200 ? `${base}/int/`:`${base}/503/`;
}
- })
- if (res.status !== 200) uri = `${base}/int`
- return Response.redirect(uri, 302);
+ return Response.redirect(uri, redirectStatus);
+ } catch (e) {
+ console.error(e);
+ return Response.redirect(`${base}/502/`, redirectStatus);
+ }
}
diff --git a/layouts/404.html b/layouts/404.html
new file mode 100644
index 0000000..2fa943e
--- /dev/null
+++ b/layouts/404.html
@@ -0,0 +1,20 @@
+{{ define "main" }}
+
+ 404
+ Not Found
+
+
+{{ end }}
diff --git a/layouts/502.html b/layouts/502.html
new file mode 100644
index 0000000..3de3a9a
--- /dev/null
+++ b/layouts/502.html
@@ -0,0 +1,20 @@
+{{ define "main" }}
+
+ 502
+ Bad Gateway
+
+
+{{ end }}
diff --git a/layouts/503.html b/layouts/503.html
new file mode 100644
index 0000000..c5cfc5e
--- /dev/null
+++ b/layouts/503.html
@@ -0,0 +1,20 @@
+{{ define "main" }}
+
+ 503
+ Service Unavailable
+
+
+{{ end }}