apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.85k stars 13.55k forks source link

Missing prefix in URLs of scripts and CSS files when STATIC_ASSETS_PREFIX config is set #29965

Open huandu opened 1 month ago

huandu commented 1 month ago

Bug description

Per document on STATIC_ASSETS_PREFIX in config, it should be the prefix of all static assets URLs. However, it's not true for most of JS and CSS files. I believe it's a bug in superset/extensions/__init__.py. The UIManifestProcessor.get_manifest method should add STATIC_ASSETS_PREFIX to all assets listed in manifest.

How to reproduce the bug

  1. Set STATIC_ASSETS_PREFIX to a custom host and restart Superset server.
  2. Reload Superset website and monitor network traffic in DevTools. We can see that most of JS and CSS files are not downloaded from the host we just set in step 1.

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 1 month ago

Hey @huandu, I'm here to help you with any bugs, questions, or contributions you might have for the repository. Let me know how I can assist you! 😊

The issue arises because the UIManifestProcessor.get_manifest method does not prepend the STATIC_ASSETS_PREFIX to the asset paths. The method currently returns paths like "/static/dist/main-js.js" and "/static/dist/main-css.css" without the prefix, even when STATIC_ASSETS_PREFIX is set.

Here's the relevant part of the implementation:

def get_manifest(self) -> dict[str, Callable[[str], list[str]]]:
    loaded_chunks = set()

    def get_files(bundle: str, asset_type: str = "js") -> list[str]:
        files = self.get_manifest_files(bundle, asset_type)
        filtered_files = [f for f in files if f not in loaded_chunks]
        for f in filtered_files:
            loaded_chunks.add(f)
        return filtered_files

    return {
        "js_manifest": lambda bundle: get_files(bundle, "js"),
        "css_manifest": lambda bundle: get_files(bundle, "css"),
        "assets_prefix": self.app.config["STATIC_ASSETS_PREFIX"]
        if self.app
        else "",
    }

The assets_prefix is correctly set in the returned dictionary, but it is not applied to the individual asset paths. This discrepancy leads to the URLs of scripts and CSS files missing the configured prefix [1][2].

To fix this, you would need to modify the get_files function to prepend the STATIC_ASSETS_PREFIX to each asset path.

To continue talking to Dosu, mention @dosu.

rusackas commented 1 month ago

Thanks for opening the PR!