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

templates(cloudflare-workers): use static assets #10034

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

Conversation

edmundhung
Copy link
Contributor

@edmundhung edmundhung commented Sep 26, 2024

This PR updates the cloudflare-workers template to use the Static Assets feature announced today.

Note: Static assets is currently in open beta

You can learn more about Static Assets in the documentation.

Closes: #

  • Docs
  • Tests

Testing Strategy:

To test, please run create-remix with the template on my fork

npx create-remix@latest --template edmundhung/remix/templates/cloudflare-workers

Then deploy it with wrangler by running:

npm run deploy

Woohoo! Your Remix app is now running on Cloudflare Workers: https://template.remix-run.workers.dev

Copy link

changeset-bot bot commented Sep 26, 2024

⚠️ No Changeset found

Latest commit: 1b52fe4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@edmundhung edmundhung force-pushed the workers-assets branch 2 times, most recently from 0286b11 to 38cd5c9 Compare September 26, 2024 23:29
@edmundhung edmundhung changed the title Update cloudflare worker template to use static assets Update cloudflare-workers template to use static assets Sep 27, 2024
@edmundhung edmundhung force-pushed the workers-assets branch 2 times, most recently from 1e84cba to c59ad09 Compare September 27, 2024 10:38
@@ -1,12 +1,14 @@
#:schema node_modules/wrangler/config-schema.json

Choose a reason for hiding this comment

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

Where's this coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you use an IDE extension that support json schema over TOML, this will give you auto completion and validation on your wrangler.toml, like this one.

Copy link

@moishinetzer moishinetzer Sep 27, 2024

Choose a reason for hiding this comment

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

fabulous - perhaps add it as a comment?

Choose a reason for hiding this comment

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

It's generated by the wrangler.

Where's this coming from?

Co-authored-by: Brooks Lybrand <[email protected]>
@ayuhito
Copy link

ayuhito commented Sep 28, 2024

I've been testing this template and it's been working flawlessly. However, has anyone else managed to make the asset binding work? I keep getting TypeError: Cannot read properties of undefined (reading 'fetch') when using the Fetcher interface of the asset binding.

This only occurs when running the dev script with remix:vite dev. wrangler dev seems to work, although that requires the whole build step.

@tsteckenborn
Copy link

@ayuhito are you when running pnpm devalso seeing the state error or is that something on my machine? 😕

So it's showing the page, but I'm seeing the error in the terminal.

> remix vite:dev

Re-optimizing dependencies because lockfile has changed
  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed
    at ReadableByteStreamController.close (node:internal/webstreams/readablestream:1157:13)

@ayuhito
Copy link

ayuhito commented Sep 28, 2024

also seeing the state error or is that something on my machine?

Nope. I've got a different error to that. I was alluding to the fact that cloudflareDevProxyVitePlugin might not perfectly handle the static assets interface in my comment.

@MichaelDeBoey MichaelDeBoey changed the title Update cloudflare-workers template to use static assets templates(cloudflare-workers): use static assets Sep 29, 2024
@FraBle
Copy link

FraBle commented Oct 2, 2024

Is there any rough timeline for when this PR might land? (excited about using it)

bucket = "./build/client"
[assets]
# https://developers.cloudflare.com/workers/static-assets/binding/
directory = "./build/client"

Choose a reason for hiding this comment

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

Should we add an ASSETS binding here too?

Copy link
Contributor

@acusti acusti Oct 7, 2024

Choose a reason for hiding this comment

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

@petebacondarwin for the majority of use cases, there’s no need for the binding. the static assets feature intercepts requests and checks if the URL matches anything in the assets directory (source). if it finds a file, it serves that. if not it, invokes your worker. as a result, the ASSETS binding’s only purpose is to provide programmatic access from your worker to the available assets that are already being automatically served when appropriate. i think that’s why the create-cloudflare remix template doesn’t default to including the binding: https://github.com/cloudflare/workers-sdk/blob/main/packages/create-cloudflare/templates-experimental/remix/templates/wrangler.toml

Copy link

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

This looks good to me. If we can land this we can fix up the create-cloudflare tool to point to it.

@focux
Copy link

focux commented Oct 4, 2024

Does this same setup work for SPA mode?

@acusti
Copy link
Contributor

acusti commented Oct 7, 2024

Static Assets (aka Workers Assets) is currently in beta, but according to the Wrangler configuration docs, Workers Sites has been deprecated in favor of Workers Assets:

site object optional deprecated

  • See the Workers Sites section below for more information. Cloudflare Pages and Workers Assets is preferred over this approach.

also, i’ve been using it in production (same as the implementation introduced here) without issue for a few weeks now.

my only comment on this PR is that i’m not sure whether or not it’s worth including the custom getLoadContext pattern for augmenting load context, because it seems like more of an advanced pattern. but it also seems pretty harmless, and maybe it will help template consumers discover the pattern if they find the need for it, so i don’t mind either way.


@focux i haven’t tried it, but i can’t imagine why it wouldn’t work for SPA mode. the static assets feature matches any request to any file in the specified directory (./build/client/ in Remix), so it serves any built JS and HTML (and any other static files placed in the ./public/ directory) automatically.


@FraBle you can use this right away for new projects by using the fork:

npx create-remix@latest --template edmundhung/remix/templates/cloudflare-workers

or just manually apply the diff of this PR to an existing Remix / Cloudflare Workers project if you have one.

Comment on lines +19 to +21
export function getLoadContext({ context }: GetLoadContextArgs) {
return context;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function getLoadContext({ context }: GetLoadContextArgs) {
return context;
}
export const getLoadContext = ({ context }: GetLoadContextArgs) => context;

cloudflareDevProxyVitePlugin(),
cloudflareDevProxyVitePlugin({
getLoadContext,
}),
remix({
future: {
Copy link
Contributor

Choose a reason for hiding this comment

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

@edmundhung let's go ahead and add

v3_lazyRouteDiscovery: true,
v3_singleFetch: true,

to the config

// `getPlatformProxy` used during development via Remix's
// `cloudflareDevProxyVitePlugin`:
// https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy
cf: request.cf,
Copy link
Contributor

Choose a reason for hiding this comment

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

This causes a type error, should it just be cast to any?

Choose a reason for hiding this comment

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

I think we can just set it to

cf: request.cf!

Or, we can change load-context.ts to:

type GetLoadContextArgs = {
  request: Request;
  context: {
    cloudflare: Omit<PlatformProxy<Env, IncomingRequestCfProperties>, "dispose" | "caches" | "cf"> & {
      caches: PlatformProxy<Env, IncomingRequestCfProperties>['caches'] | CacheStorage;
      cf: Request['cf'];
    };
  };
}

The first is simpler, but the second is better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer correctness, I'll defer to @edmundhung

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

Successfully merging this pull request may close these issues.