kotx / render

Cloudflare Worker to proxy and cache requests to R2
MIT License
384 stars 85 forks source link

Enable serving of index.html pages for requests ending in / #12

Closed ncw closed 2 years ago

ncw commented 2 years ago

This then allow static web sites to be served properly from an R2 bucket.

ncw commented 2 years ago

Just realised this is broken... Will upload a fixed version shortly!

ncw commented 2 years ago

Fixed now!

kotx commented 2 years ago

It might be nicer to have it support absolute paths like /index.html, but that might need a better name like FALLBACK_PATH or FALLBACK_FILE.

ncw commented 2 years ago

This looks good! All that's missing is adding INDEX_FILE?: string to the Env interface at the top of index.ts.

I missed that. I've never written any typescript before ;-)

Might also need to check if INDEX_FILE is undefined instead of "" as well.

Is this a good way?

@@ -36,7 +37,7 @@ export default {
     }

     const url = new URL(request.url);
-    if (env.INDEX_FILE === "" && url.pathname === "/") {
+    if (!env.INDEX_FILE && url.pathname === "/") {
       return new Response("OK");
     }

@@ -51,7 +52,7 @@ export default {
       var path = (env.PATH_PREFIX || "") + decodeURIComponent(url.pathname.substring(1));

       // Look for index file if asked for a directory
-      if (env.INDEX_FILE !== "" && (path.endsWith("/") || path === "")) {
+      if (env.INDEX_FILE && (path.endsWith("/") || path === "")) {
         path += env.INDEX_FILE;
       }

It might be nicer to have it support absolute paths like /index.html, but that might need a better name like FALLBACK_PATH or FALLBACK_FILE.

I'm not sure I understand you here. An absolute path like /index.html would imply re-using the /index.html for every sub directory wouldn't it? I'm not sure how that would be used.

kotx commented 2 years ago

Is this a good way?

Yup, looks good!

I'm not sure I understand you here. An absolute path like /index.html would imply re-using the /index.html for every sub directory wouldn't it? I'm not sure how that would be used.

Yeah, in hindsight an absolute path for subdirectory fallbacks wouldn't be very useful. We probably don't need that.

ncw commented 2 years ago

I've updated that. PTAL :-)

kotx commented 2 years ago

Just tested this and it's working! Thanks for the PR!

ncw commented 2 years ago

Thank you for a great project 🙂