Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dario-piotrowicz opened this issue Jun 13, 2024 · 3 comments

Comments

@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Jun 13, 2024

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

  • is what's used by the other mainstream fullstack frameworks (e.g. nuxt/nitro, astro, qwik, etc...),
  • is what Cloudflare recommends,
  • and would produce the extra benefit of leaving the bundling up to Remix (/vite) which could then preserve dynamic imports and as a result significantly speed up request handling times.

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
Copy link
Contributor Author

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
Copy link
Member

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants