remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
28.48k stars 2.4k forks source link

Remix Cloudflare builds should not work on based on the `/functions` directory #9611

Open dario-piotrowicz opened 1 month ago

dario-piotrowicz commented 1 month ago

When producing deployment/production builds that target Cloudflare, such builds rely on the /functions directory, for example see:

Instead of this, builds should use advanced mode and produce an _worker.js directory instead (basic example of the _worker.js directory).

This


Since the [[path]].ts file is part of the user facing API this would require user facing changes, as discussed a while back (internal discord thread) a solution could be to introduce a cloudflare.ts/_worker.ts/etc... file that would be at the root of a Remix project and have such file renamed and moved in the appropriate _worker.js directory in the buildEnd hook.

Optionally a getServerBuild (or similar) utility (or even a path rewrite on the fly) could be introduced to hide from users the fact that relative import path to the Remix build pre file movement does not actually point to a real file

dario-piotrowicz commented 1 month ago

[!NOTE] The main keypoints here are:

  • prod works (build + run)
  • dev works
  • HMR in dev works
  • single source-of-truth for the load context
sergiodxa commented 1 month ago

The server build for CF doesn't really depend on the /functions directory, since you control that file you could remove it, import the build/server/index.js file from inside a _worker.js file and use CF Pages in Advanced Mode.

The server build you import is just an object conforming to the Remix's ServerBuild interface that's the same regardless if you use CF, Node, Bun or Deno. The createPagesFunctionHandler is the one handling the request to Remix and the response from Remix to CF, but you could also skip that function and go directly to createRequestHandler from the @remix-run/cloudflare package, this one is specific for the workerd runtime and not for CF Pages so it works also for CF Workers.

dario-piotrowicz commented 1 month ago

[!NOTE] the remix team has agreed on Remix to output to build/client and build/server and then have the Cloudflare build logic do anything it wants and move those around into build/cloudflare-pages (or whatever the name ends up being) with the buildEnd hook of the remix() plugin