question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Static Assets with Workers

See original GitHub issue

This one is pretty straightforward. As a website developer, I would like to serve “static” assets on the internet. These are not user generated content, but usually stuff like html file, logos, maybe some images, etc.

The current options are:

  • Workers Sites, which requires a combination of configuration ([site] in wrangler.toml) and code (configuring @cloudflare/kv-asset-handler). It’s incredibly fragile and not particularly liked by the community. It’s backed by KV. It also has issues with consistency since there’s no efficient way to expire assets, so during deploy it’s possible that some assets just 404. It was the only thing folks had for a while.
    • We should silently stop supporting this. We shouldn’t add a deprecation warning, and @cloudflare/kv-asset-handler should still get security updates and bugfixes, but we shouldn’t add any new features, and guide people towards newer solutions.
  • Pages, which is an opinionated stack around Workers. Also backed by KV, but the implementation is not exposed to the user. They’ve announced a direct upload api, but it’s still not clear how it would integrate with a wrangler project.
    • If it’s a new project that’s centered around static assets, the recommendation is to simply start with Pages.
    • It’s hard to migrate an existing non-trivial project to use Pages (mostly because of missing features).
    • We can’t currently recommend people just use Pages for static assets until Pages specifies how that would work. (I… don’t know myself? How would it work? Does it require the convergence effort? How would routing work? And references? And previews? I suspect we’ll use the upload api as an implementation detail underneath an abstraction, possibly one of the below options)
  • --experimental-public <path>: Exists in wrangler2. Backed by KV and kv-asset-handler, but the implementation is not exposed to the user. Light on features. Faces the same 404 issue as Sites. Particularly nice because it’s “zero config”

Plan 1: --assets <path>

  • Rename --experimental-public to --assets
  • Allow multiple values (--assets <path1> --assets <path2>)
  • Add to wrangler.toml as assets
  • Optionally have an object form { bucket: <bucket>, include: <glob>, ... }
    • bucket: path to assets
    • include: glob that matches files to include
    • exclude: glob that matches files to exclude
    • mount: path to mount to
  • Implementation-wise, this can stick to using KV for now, but
    • We should move to using R2 to get past the 25 mb limit
    • We should start work on static-assets-as-a-cloudflare-primitive
    • We might use the Pages upload api for this
    • Gives us a path to solve the 404/consistency issues

Plan 2: imports

import ASSETS from "assets:path/to/bucket?include=glob&exclude=glob";

ASSETS.fetch(request, env); // env is required because it'll contain the kv (or whatever) binding 
  • This is the story for people who’ll want deep customisation on how they want to handle static assets.
  • With code/abstraction, they’ll be able to support usecases like those described vy the options in http://expressjs.com/en/resources/middleware/serve-static.html
  • There’s a story here where we can graduate this to a proper web standard
  • It’s critical that we figure out how to make this work with jest etc

Plan 3: File loader

Adds a new type to rules config

[[rules]]
type = "file"
globs = "glob/*.png"
import styles from style.css;

console.log(styles);
// http://something.com/path/to/style.css
  • This is useful for serving single files
  • Further useful to use as references (href/src) in html
  • For files below a particular size, we could embed them directly in the Worker (with data: uris, but this requires the WOrkers runtime to support this as well)

Plan 4: Integrate Pages

We should come up with a plan to integrate Pages for assets directly into a wrangler project

Open questions:

  • Should we use Pages upload API?
  • R2?
  • What’s a good solution for expiring assets?
  • With versioning, we won’t be able to ‘delete’ assets like we now do.
  • How can we hide the kv namespaces we make?
  • Overall, is there a cloudflare story for first class static assets?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rozenmdcommented, Jun 14, 2022

I was just thinking out loud how it’s cool how it’s almost possible to deploy a static eleventy site in 3 commands:

echo '# Page header' > README.md
npx @11ty/eleventy
npx wrangler publish --assets _site 

I’m thinking if an assets flag is provided, that should be enough for us to provide a boilerplate worker, and run dev/deploy with no further config required.

Folks can of course provide their own worker script if they’d like, but I’m thinking it shouldn’t be a requirement any more.

1reaction
threepointonecommented, Jun 9, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 6478 - Pseudowire Status for Static ...
Standards Track [Page 2] RFC 6478 Pseudowire Status for Static Pseudowires May 2012 1. Introduction The default control plane for Pseudowire (PW) technology ......
Read more >
RFC 6866: Problem Statement for Renumbering IPv6 ...
RFC 6866: Problem Statement for Renumbering IPv6 Hosts with Static Addresses in Enterprise Networks.
Read more >
Workers Sites
Workers Sites enables developers to deploy static applications directly to Workers. It can be used for deploying applications built with static ...
Read more >
Cachable Static Assets with Spring MVC
This article focuses on caching static assets (such as Javascript and CSS files) when serving them with Spring Boot and Spring MVC.
Read more >
Serve static content in an Amazon S3 bucket through a VPC ...
When you serve static content that is hosted on Amazon Web Services (AWS), the recommended approach is to use an Amazon Simple Storage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found