#Defining multiple folders to act as a `public` folder

1 messages · Page 1 of 1 (latest)

frigid bone
#

You could create a route where you use the fs module to read the files and send them to the browser

Otherwise you will need to change to Express so you can add that folder as static

narrow vortex
#

Thats what I was planning to do but I can for see a number or security issues, I think Ill just end up switching to Vite

narrow vortex
#

If you don't mind, having a second pair of eyes scan over this code to make sure there aren't any holes in my security would be appreciated

const STATIC_CONTEXTS = [
  { files: "node_modules/@ruffle-rs/ruffle/", prefix: "/ruffle/" }
];

export default async function handleRequest(
  request: Request,
  responseStatusCode: number,
  responseHeaders: Headers,
  remixContext: EntryContext
): Promise<Response> {
  const { pathname } = new URL(request.url);

  for (const staticContext of STATIC_CONTEXTS) {
    if (pathname.startsWith(staticContext.prefix)) {
      // Resolve the file path
      const filePath = path.resolve(
        staticContext.files,
        pathname.slice(staticContext.prefix.length)
      );

      // Make sure the resolved path starts with the static context (should prevent ../ paths)
      if (filePath.startsWith(path.resolve(staticContext.files))) {
        try {
          // Respond with file
          const file = await fs.readFile(filePath);

          responseHeaders.set("Content-Type", mime.lookup(filePath) || "");

          return new Response(file, {
            headers: responseHeaders,
            status: 200
          });
        } catch {
          // Do nothing (continue to 404 page)
        }
      } else {
        // Do nothing (continue to 404 page)
      }
    }
  }

  ...
}
frigid bone
#

is this in your entry.server?

#

I recommend you to don't do it there, create an actual route, you can do routes/$.ts to create a catch-all route

#

if you do it in entry.server, Remix will first run the loaders of any route that match (or root)

narrow vortex
#

ah I see, that explains the errors in console

#

should be easy enough to adapt