rykener/django-manifest-loader

Manifest has leading slash, but staticfiles expects none

WhyNotHugo opened this issue · 4 comments

Trying to use this in non-debug mode fails:

self = <django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 0x7fe1adb9b670>, name = '/css/app.f6e28323.css'

    def stored_name(self, name):
        parsed_name = urlsplit(unquote(name))
        clean_name = parsed_name.path.strip()
        hash_key = self.hash_key(clean_name)
        cache_name = self.hashed_files.get(hash_key)
        if cache_name is None:
            if self.manifest_strict:
>               raise ValueError(
                    "Missing staticfiles manifest entry for '%s'" % clean_name
                )
E               ValueError: Missing staticfiles manifest entry for '/css/app.f6e28323.css'

/usr/local/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py:465: ValueError

From what I can tell, the issue is that django-manifest-loader attempts to find files in the staticfiles stoage using the path found in manifest.json. The issue is that paths in manifest.json have a leading slash, but paths in staticfiles.json do not.

E.g.:

$ jq < manifest.json| grep css.app
  "app.css": "/css/app.f6e28323.css",
$ jq < staticfiles.json |  grep f6e28323
    "css/app.f6e28323.css": "css/app.f6e28323.d95a1d9dad84.css",

I think that this can be fixed by changing manifest_loader.utils.make_url to remove the leading slash.

However, is there any scenario in which the slash in absent? Otherwise I can't imagine how django-manifest-loader would ever work in non-DEBUG mode.

Actually, the fact that django-manifest-loader uses staticfiles as a backend is problematic (and I realise that's what I was trying to avoid by switching to lib). The hashes added by ManifestStaticFilesStorage imply that js files loaded by js files will have mismatching filesnames. I actually need filenames to remain unchanged, so using staticfiles as a backend is a no-go.

I guess I need to use a custom loader? https://django-manifest-loader.readthedocs.io/en/latest/docs/usage.html?highlight=loader#activating-the-custom-loader

Oh, it's not the loader that delegates to staticfiles, but the manifest tag itself.

I'm failing to understand how this library is meant to be used. Staticfiles itself adds hashes to scripts, so if one script refers to another, the path doesn't match and it won't load. I currently work around this by compiling everything into one single file (with no hash in its filename) that doesn't load other files, but this has multiple issues (caching is a mess, can't split into multiple script files).

I realised I can use manifest.json to map to my entry point scripts, which can then load all others with their webpack-given hash directly. I though that django-manifest-loader would help, but it just uses staticfiles as a backend, so ends up having all the same issues...

But it doesn't seem possible to avoid using staticfiles as a backend? So only running with DEBUG=True works... What am I missing here?

you may need:

...
  output: {
    publicPath: 'auto',
  },
...

in the webpack config.