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

Adapt Superflare to work with Wrangler 3 and Remix v2 (vite compiler) #66

Open
wants to merge 130 commits into
base: main
Choose a base branch
from

Conversation

acusti
Copy link

@acusti acusti commented Jul 19, 2024

This PR:

  • updates the superflare/remix package + remix template + docs site to work with remix v2 and the vite compiler (based on the official cloudflare workers template)
  • updates all packages to the latest v5.x version of typescript and fixes some new typescript errors
  • updates the superflare CLI to work with wrangler v3 commands and miniflare v3 APIs. this entailed changes to some of the CLI command APIs:
    • the migrate command’s --db option is now the name of the DB binding, not a path to the actual local DB file
    • the dev command now runs remix vite:dev for the main server + wrangler dev in parallel to enable Durable Objects
  • removes direct dependency on better-sqlite3 in favor of wrangler’s getPlatformProxy API
  • removes ./d1js/* vendored files
  • adds a new export to the @superflare/remix plugin, available at @superflare/remix/dev, which provides superflareDevProxyVitePlugin, a drop-in replacement for remix’s cloudflareDevProxyVitePlugin that supports superflare and its automatic session handling and auth features
  • upgrades to latest pnpm

implements #62

@@ -88,14 +88,17 @@ export async function getManifest(
return JSON.parse(manifest);
}

function getNodeText(node: RenderableTreeNode) {
function getNodeText(renderableNode: RenderableTreeNode) {
if (typeof renderableNode === "string") return renderableNode;
Copy link
Author

@acusti acusti Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jplhomer heads up that this line is a change in behavior. in the previous version of this function, if typeof node === "string", the function returned an empty string, which i thought might be a bug, but if it is in fact desired, i will just lump this check in with the conditions in the next if statement (on line 93) so that it also returns an empty string.

adds getSessionCookie to AppLoadContext and provides it from load-context.ts
also, bring over code to ensure session has a sessionId from superflare#handleFetch
@acusti
Copy link
Author

acusti commented Aug 13, 2024

@jplhomer i believe the last major missing piece for basic functionality was the logic to commitSession if isDirty() at the end of every request when running the app using the vite-based dev server. i originally planned to implement that as vite server middleware and register it after the remix cloudflareDevProxyVitePlugin. then i discovered that that plugin doesn’t call next() on success, which means that no further middleware will be called after it executes.

so instead, i added the commitSession logic to entry.server.ts. this seems to work, but the downside is that it’s code that must be put into the docs and into the remix template but can’t actually be provided by the @superflare/remix package.

an alternative solution that would enable us to avoid requiring custom code in the developer’s entry.server.ts file would be to re-implement the cloudflareDevProxyVitePlugin as a part of the superflareDevProxyVitePlugin, so that we can make it call next() and then have a final registered middleware that commits the cookie and doesn’t call next() to serve as the server terminus. this was actually my original plan until i realized it would require duplicating all of this file into @superflare/remix, which seemed less than ideal, so i didn’t go with that. but it’s totally doable if that seems preferable to you.

the remix-cms example now allows registering and logging in, which is exciting. i need to also update the superflare dev CLI command to both run remix vite:dev and wrangler dev in parallel to make durable objects work, but first i’m working on fixing the AppLoadContext types.

• reverts the changes implemented in 22f47d4
• the reason getPlatformProxy didn’t work when implemented in fe766b9 is that getPlatformProxy cannot handle Durable Objects if a script_name isn’t provided for the bindings (which was resolved in f5874c0)
• move @remix-run/cloudflare AppLoadContext definition to index.ts
• use AppLoadContext where necessary
• use WorkersRequest for accuracy + global Request for @remix-run/cloudflare’s createRequestHandler return value compatibility
• restore local ./load-context.ts to expand @remix-run/cloudflare’s AppLoadContext type to include a cloudflare property based on Env interface from ./worker-configuration.d.ts
added new @superflare/remix-dev package to host the superflareDevProxyVitePlugin in such a way that it can be independently imported into users’ vite.config.ts file without then getting included in the built cloudflare workers bundle
the template dependency string will have to be changed from "workspace:*" → "*" once the new package has been published
@acusti
Copy link
Author

acusti commented Aug 15, 2024

@jplhomer i got the AppLoadContext types working (it seems to require having a local ./load-context.ts file to do the type interface re-declaration, but that matches the first-class cloudflare-workers remix template, and the version we need is smaller and simpler), and i fixed the remaining issues with the remix-cms example. there are a number of type errors still in that example, but i think most of them are due to the limitations of JSON serializing superflare models (specifically the Article model), which i think would be resolved by adopting the remix single fetch API and its brand of type inference.

i also found out that including the superflareDevProxyVitePlugin in @superflare/remix means that now, importing anything from @superflare/remix results in wrangler getting included in whatever bundle is produced, which breaks the build for workers. so, i created a new @superflare/remix-dev package and moved the superflareDevProxyVitePlugin into that package. now, that package only gets imported into vite.config.ts, so none of its dependencies get included in the built worker. maybe there’s another way to make that work, but having a separate package felt like a clean and straightforward way to me.

is it possible to get another pkg-pr-new build made from the latest version of this branch so that i can try using it in an external application? i believe all the pieces are now in place.

@acusti
Copy link
Author

acusti commented Aug 15, 2024

@jplhomer thanks for triggering the pkg-pr-new workflow! the build failed because some of the changes in @superflare/remix depend on a new type being exported from superflare, but running each package’s build script in parallel meant the new type wasn’t available at build time and so typescript reported an error. i removed the --parallel flag from the root build script. when i tested that change locally, it resulted in the packages being built serially in alphabetical order, so superflare, then superflare-remix, then superflare-remix-dev, which happens to also be the order of the dependency graph. this should ensure the new types are built and available before tsc encounters them being imported from the next package, so i think it will work this time.

@acusti
Copy link
Author

acusti commented Aug 16, 2024

@jplhomer one thing i wanted to call out from my previous message: i chose to call the new dev-only package i introduced @superflare/remix-dev (to provide superflareDevProxyVitePlugin), but i was also considering just calling it @superflare/dev (like @remix-run/dev). happy to make that change if it sounds better to you. also, i could just keep the superflareDevProxyVitePlugin inside the @superflare/remix package and have it live in a completely separate file and import path (e.g. import { superflareDevProxyVitePlugin } from "@superflare/remix/dev";). i’m also happy to do that if it seems preferable.

@acusti
Copy link
Author

acusti commented Aug 21, 2024

@jplhomer i decided to try implementing a separate entry point in the @superflare/remix package for the superflareDevProxyVitePlugin as i mentioned in my previous comment (available at @superflare/remix/dev). it works well and is a simpler implementation, so there’s no more new superflare package to worry about. however, in order to test it in my own app, i will need another pkg-pr-new workflow to be triggered. thanks in advance, and sorry for the trouble!

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

Successfully merging this pull request may close these issues.

3 participants