Skip to content

Conversation

@AyushDharDubey
Copy link
Collaborator

@AyushDharDubey AyushDharDubey commented Dec 14, 2025

Closes #70

This PR integrates django-vite to streamline the frontend development workflow and enable hot module reloading for static assets during development.

  • Adds django-vite to bridge Django templates with the Vite dev server
  • Enables HMR-assisted reloading for JS/CSS/static changes during development
  • Removes the need to manually run npm run build for every frontend change in dev
  • Moved the static_src/public folder contents into static_src/public/mainapp: to change outDir to public for consistency with django-vite.

New workflow

Development

  1. Start the Vite dev server:

    npm run dev
    • Runs Vite on http://localhost:5173
    • Watches mainapp/static_src/
    • Automatically reloads the whole vite+django setup on change using django-vite
  2. Start Django:

    python manage.py runserver

Frontend changes now propagate automatically without rebuilding or restarting Django.


Production

Unchanged and explicit:

npm run build
python manage.py collectstatic
  • Vite outputs bundled assets
  • Django serves static files as usual

  • No runtime impact in production
  • HMR is enabled only when DEBUG=True
  • Existing static paths remain compatible

@AyushDharDubey
Copy link
Collaborator Author

I am planning to reorganize the README to break the setup steps and contributing guidelines in a future PR.

I will also document the above steps therein in the same PR.

@lonvia
Copy link
Collaborator

lonvia commented Dec 16, 2025

Would it somehow possible to integrate running the vite dev server into manage.py runserver? What I mean is: when calling 'runserver', it starts the dev server in the back and also automatically kills it when exiting.

@AyushDharDubey
Copy link
Collaborator Author

I did research into this after our discussion months back... unfortunately django doesn't feature in itself a reimplementation of runsrever, but we have an npm package in the same use case: concurrently

If we implement this, we will be running something like npm run dev:start to start both of the servers at once, it can also label the logs from vite and django separately. We will have to do a bit restructuring of the project: move the whole node package into root of the project....

I did not implement this one because it didn't felt much of a required use case... also, it will wrap our predominantly django project into an npm start command🙃

@lonvia
Copy link
Collaborator

lonvia commented Dec 16, 2025

I agree, that's definitely not what we'd want.

I was more hoping that runserver could be extended to span a subprocess, which gets automatically SIGHUPed when stopping the runserver process. If that's not easily possible, don't worry about it. I can use a shell script to simulate the behaviour. (Background: I don't do development with 3dmr, just occasionally spin it up to test a PR. And every time I do that, I have forgotten how "spinning up" works. So I prefer to have script or Makefile lying around that reminds me about the steps. But that's all a problem of my setup and doesn't need solving globally.)

@lonvia lonvia merged commit cfb15c7 into fossgis:main Dec 16, 2025
1 check passed
@AyushDharDubey AyushDharDubey deleted the django-vite-HMR-integration branch December 16, 2025 17:58
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.

Simplify and automate Vite–Django static asset workflow

2 participants