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

Fix vite.static endpoint #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

samuelhwilliams
Copy link
Contributor

@samuelhwilliams samuelhwilliams commented Feb 18, 2025

When the vite static endpoint is setup in vite.init_app, the endpoint name is hardcoded as vite.static:

app.route(
    "/_vite/<path:filename>", endpoint="vite.static", host=self.vite_routes_host
)(self.vite_static)

The make_static_tag function, however, injects the vite_folder_path variable into the endpoint name:

js_file_url = url_for(f"{vite_folder_path}.static", filename=js_file)
css_file_url = url_for(f"{vite_folder_path}.static", filename=css_file)

If you override the vite folder path from the default value of vite, then these URLs don't resolve. For example a vite folder path of app/vite would try to build URLs for app/vite.static - which doesn't exist.

@samuelhwilliams
Copy link
Contributor Author

@sfermigier if you have time, would appreciate some eyes on this as this appears to be broken 🙏 See also #13, which I've realised after raising this PR is the exact same fix...

@miladnia
Copy link

miladnia commented Feb 21, 2025

Great job! I was about to create a pull request for this bug and realized you had already fixed it a few days ago.

It seems @bonchondev accidentally changed the word vite in the endpoint to {vite_folder_path} while implementing the VITE_FOLDER_PATH feature. I couldn't find any tests for the VITE_FOLDER_PATH config. It would be great if you could add a test case for this feature.

I’ve temporarily fixed the issue by defining a new URL rule in my project:

# Initialize the Flask-Vite extension
app.config["VITE_AUTO_INSERT"] = True
app.config["VITE_FOLDER_PATH"] = "static"
vite = Vite(app)
app.add_url_rule(
    "/assets/<path:filename>",
    endpoint=f"{app.config['VITE_FOLDER_PATH']}.static",
    view_func=vite.vite_static
)

@samuelhwilliams
Copy link
Contributor Author

samuelhwilliams commented Feb 22, 2025

That's the same workaround I've had to do as well 😅

Yep good call on adding a test - will do that shortly 🙏

@samuelhwilliams
Copy link
Contributor Author

samuelhwilliams commented Feb 22, 2025

Unfortunately CI seems very broken and I'm not super familiar configuring tox or poetry, so not making easy progress. None of the historical CI runs still have their logs so it's a bit harder to see what should be happening to compare what's going wrong.

Also no idea what that safety check lint command is referring to.

Will spend a bit more time but 🤷 Feels unfortunate.

edit: alright got it sorted. safety check doesn't seem to run in github, so that's still broken, but 🤷

When the vite static endpoint is setup in vite.init_app, the endpoint
name is hardcoded as `vite.static`:

app.route(
    "/_vite/<path:filename>", endpoint="vite.static", host=self.vite_routes_host
)(self.vite_static)

The `make_static_tag` function, however, injects the `vite_folder_path`
variable into the endpoint name:

js_file_url = url_for(f"{vite_folder_path}.static", filename=js_file)
css_file_url = url_for(f"{vite_folder_path}.static", filename=css_file)

If you override the vite folder path from the default value of `vite`,
then these URLs don't resolve. For example a vite folder path of
`app/vite` would try to build URLs for `app/vite.static` - which doesn't
exist.
CI is no longer working because the cache action is so out of dated.

https://github.com/abilian/flask-vite/actions/runs/13392706813/job/37403982442

This bumps all of the actions to their latest major version.
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

Successfully merging this pull request may close these issues.

2 participants