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

Manifest has leading slash, but staticfiles expects none #54

Open
WhyNotHugo opened this issue Sep 28, 2022 · 4 comments
Open

Manifest has leading slash, but staticfiles expects none #54

WhyNotHugo opened this issue Sep 28, 2022 · 4 comments

Comments

@WhyNotHugo
Copy link

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",
@WhyNotHugo
Copy link
Author

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.

@WhyNotHugo
Copy link
Author

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

@WhyNotHugo
Copy link
Author

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?

@devkral
Copy link

devkral commented Oct 8, 2022

you may need:

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

in the webpack config.

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

No branches or pull requests

2 participants