adamchainz / django-browser-reload

Automatically reload your browser in development.
MIT License
516 stars 26 forks source link

When use with Django Components, changes to the .py files do not update until server restart #224

Open ayoung-burnsmcd opened 11 months ago

ayoung-burnsmcd commented 11 months ago

Python Version

3.9.18

Django Version

4.2.7

Package Version

No response

Browser

No response

Description

When used with Django Components you have to restart the server every time you update the .py file that registers the component.

If you disable Django Browser Reload then the component functionality works as expected.

I created a sample repo with a project demonstrating the issue. Directions are in the project readme.

https://github.com/ayoung-burnsmcd/components-browser-reload

adamchainz commented 11 months ago

Are you saying that without Django-browser-reload, the server does restart? And that with Django-browser-reload, it does?

ayoung-burnsmcd commented 11 months ago

Without Django-browser-reload, runserver recognizes the update to the Django-component .py file and will output the updates on a browser refresh.

With Django-browser-reload, runserver does not recognizes the update to the Django-component .py file and will not show the update until the server is stopped and restarted.

imankulov commented 11 months ago

We encountered the same issue.

django-components stores Python files along with their media files and HTML templates. It uses django_components.template_loader.Loader, which extends the template directory list to include the parent directories of the Python components.

Ref: https://github.com/EmilStenstrom/django-components#installation

The "django-browser-reload" app prevents the server from being reloaded when there are changes in the component Python files, as it considers them either template files or static assets.

Ref: https://github.com/adamchainz/django-browser-reload/blob/main/src/django_browser_reload/views.py#L114

If we agree that Python files are specific enough to require server-side reloading, regardless of their location, the fix should be quite straightforward. I am ready to create a pull request.

As a further development of this idea, we can configure the list of extensions that always require a reload via settings. However, I am not sure what else, besides Python, should be included there.

ayoung-burnsmcd commented 11 months ago

A list of extensions, or extensions at a path, would be a great solution. This would allow current functionality to be unaltered while allowing a developer to customize as needed.

imankulov commented 11 months ago

I've created a PR #229 to address the issue, and I took the liberty of hardcoding the Python extension as the one that forces the development server to reload regardless.

I decided not to parameterize it because I felt like it was such a rare case that adding a settings option for it and documenting it would be more confusing than helpful. It would be a separate but puzzling challenge -- how would we name that settings option if we decided to introduce it? 🤷

However, I am open to feedback. I would especially love to hear @adamchainz's input. Thank you!

ayoung-burnsmcd commented 10 months ago

I applied the patch in #229 locally and it worked great!

Thank you so much!