-
Notifications
You must be signed in to change notification settings - Fork 87
Description
I maintain an open source project utilising django and vue. Currently on vue2 I use webpack with webpack bundle tracker. While migrating to Vue3 I am thinking about switching to vite and django-vite as it seems to work a lot better. I just have one inconvenience.
I would be happy to contribute a fix/improvement if that is wanted.
Problem
Switching between testing on with dev server / HMR is cumbersome because you need to change settings/environments
Solution
add an app specific setting autodetect_dev_server
(or similar)
in asset_load.py
when generating the ws_client check if the setting is enabled and detect if the dev server is running
def generate_vite_ws_client(self, **kwargs: Dict[str, str]) -> str:
if self.autodetect_dev_mode:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.001)
try:
# check if a service is running on the dev server host/port
s.connect((self.dev_server_host, self.dev_server_port))
self.dev_mode = True
except:
pass
if not self.dev_mode:
return ""
url = self._get_dev_server_url(self.ws_client_url)
return TagGenerator.script(
url,
attrs={"type": "module", **kwargs},
)
This code is just a rough first idea, you know your codebase best so if a different method (e.g. maybe the constructor) is more fitting for running this check and deciding if dev mode should be enabled or not is better feel free to give feedback.
Considerations
This will add a minor delay (in my tests 20-30ms) into the page load but only while DEBUG and autodetect is enabled. Since you don't reload often when using HMR the impact will be insignificant. The setting can be enabled on all dev workspaces and disabled by default, this way you don't have to change it all the time
Other options
- right now I have a small script in
settings.py
that checks if the dev server is running and changes the setting accordingly. The problem here is that if you start django before starting vite it wont detect the server at load time. - you could configure different environments, but that is additional setup work for each workstation
Invalid options
- just using
DEBUG
because every now and then you might want to turn on debug on production to generate a quick report or investigate an issue and that would break the page if no dev server is present
Thank you for the great project, I already use it for a smaller project and really like how well it works