Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

ESM compatibility: Fix crash on missing global env vars #188

Merged
merged 4 commits into from
May 28, 2021

Conversation

ttraenkler
Copy link
Contributor

@ttraenkler ttraenkler commented May 28, 2021

Fixes kv-asset-handler crashing when env vars are missing as is always the case with the new ESM module syntax which wraps those in an env object that is passed explicitly.

ReferenceError: __STATIC_CONTENT is not defined
ReferenceError: __STATIC_CONTENT_MANIFEST is not defined

This partially fixes the problems reported in #174, for the other half of the solution just wrap { request } and ts-ignore the typescript error to pass in the event object to getAssetFromKV. I did not do this change to stay backwards compatible until this is addressed properly by the Cloudflare team.

Copy link

@evanderkoogh evanderkoogh left a comment

Choose a reason for hiding this comment

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

Yup.. doesn't solve the problem, but at least this makes it possible for anyone to solve it themselves.

We will need to figure out a separate proper solution that is backward-compatible.

@ttraenkler
Copy link
Contributor Author

@evanderkoogh: Exactly! How the public API needs to change is best left to you but until then using this we can work around the issue. As mentioned in the PR if backwards compatibility is a requirement a new optional last env parameter for the affected functions could work - and if present it is used instead of the globals

@ttraenkler
Copy link
Contributor Author

It seems actually that the __STATIC_CONTENT_MANIFEST is not included in the new env object anymore so without it the patch does not really work with assets uploaded with a hash as part of the filename as done by wrangler's workers-site code as of 1.16.1.

Copy link
Contributor

@Cherry Cherry left a comment

Choose a reason for hiding this comment

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

Looks good to me. I'm not super familiar with ESM on Workers yet, but the change makes sense.

@Cherry Cherry merged commit f1febcd into cloudflare:master May 28, 2021
Cherry added a commit that referenced this pull request May 28, 2021
Cherry added a commit that referenced this pull request May 28, 2021
@lemonmade
Copy link

Would it be possible to get a release that includes this fix? Until this fix is released, I have to fork this library to use it in my worker project, but would love to use the real thing again :)

@kristianfreeman
Copy link
Contributor

@Cherry we should release a new version this week :)

@Cherry
Copy link
Contributor

Cherry commented Jun 14, 2021

@Cherry we should release a new version this week :)

💯 I'll shoot you a message

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

Successfully merging this pull request may close these issues.

5 participants