miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

__STATIC_CONTENT_MANIFEST is empty - can't list files in the Workers Site

Open ItalyPaleAle opened this issue 2 years ago • 5 comments

When using Workers Site, import manifestJSON from '__STATIC_CONTENT_MANIFEST' imports a variable manifestJSON that contains the JSON manifest, encoded as string, which lists all files in the Site.

Using Miniflare, that manifest is empty.

I understand from the docs that this is by design, however there are situations where accessing the manifest is needed. My app does that to match the correct file to return (e.g. if the extension is missing)

Svelte Kit's adapter cloudflare-workers is another app (more popular/relevant than what I'm building :) ) that needs that.

Currently, this behavior is preventing me from being able to use Miniflare.

ItalyPaleAle avatar Apr 04 '22 01:04 ItalyPaleAle

Hey! 👋 Thanks for raising this. As mentioned in the docs you linked, the reason I did this was to disable caching, which would serve outdated files. Wrangler solves this problem by uploading files with a content hash appended to their name. I can think of a couple potential solutions:

  1. Don't store hashes in the manifest ({ "index.html": "index.html" }), but purge site files from the cache on reload
  2. Store hashes in the manifest ({ "index.html": "index.abcde.html" }), but remove them when performing file lookups (i.e. __STATIC_CONTENT.get("index.abcde.html") would actually lookup index.html). If we went with this option, we could add a magic string to make removing the hash easier (e.g. index.$MF_HASH:abcde$.html).

I'm tempted to go with option 1, even though I don't like the idea of deleting cached files especially with the --cache-persist flag enabled, but am open to suggestions.

mrbbot avatar Apr 06 '22 17:04 mrbbot

Thanks for responding and acknowledging the issue!

I'll be honest that I am not familiar enough with the inner workings of Wrangler. I will trust your judgement on this!

ItalyPaleAle avatar Apr 07 '22 02:04 ItalyPaleAle

@ItalyPaleAle are you passing sitePath value when initializing Miniflare?

const mf = new Miniflare({
  scriptPath: "index.js",
  sitePath: "public",
  modules: true,
});

See Cloudflare Starter Kit → site/index.ts, test/test.ts

koistya avatar May 27 '22 12:05 koistya

@koistya I'm using the CLI and I'm passing the --site flag.

ItalyPaleAle avatar May 30 '22 19:05 ItalyPaleAle

Oh, I see, it works with Miniflare server, but not with jest-environment-miniflare (VM linker).

I had to add a stub for __STATIC_CONTENT_MANIFEST to make it work with jest-environment-miniflare:

jest.config.js

export default {
  ...
  moduleNameMapper: {
    __STATIC_CONTENT_MANIFEST: "<rootDir>/test/manifest.ts",
  },
  ...
};

test/manifest.ts

/**
 * __STATIC_CONTENT_MANIFEST stub
 */
export default "{}";

koistya avatar Jun 18 '22 10:06 koistya