Twig HMR
Edit a Twig template and see the change without a reload.
During pnpm dev, saving a .twig file updates the page in place, with no full reload.
How it works
- Vite notices the changed template and sends a
morphdomevent to the browser. - The browser fetches the current URL again and diffs the new
<body>into the page with morphdom. - The Tailwind stylesheet is reloaded, so new utility classes apply right away.
Drupal.attachBehaviors()runs for the new markup.
Edit me: templates/demos/twig-hmr.html.twig
With the dev server running, saving this template morphs the page instead of reloading it, so you keep your scroll position.
Requirements
morphdominstalled in your theme.- A cache clear after starting the dev server, so Drupal loads the assets from Vite.
- Twig cache disabled (Configuration → Development settings), otherwise Drupal returns the old markup.
- Your behaviors must be idempotent. Use
once(), because they run again after every update.
Running the dev server in DDEV
Start Vite inside the web container, so DDEV_PRIMARY_URL is set and the assets are served over HTTPS on the port exposed in .ddev/config.vite.yml. Then clear the cache:
ddev exec -d /var/www/html/drupal/web/themes/custom/uebertool_docs \
"./node_modules/.bin/uebertool && ./node_modules/.bin/vite"
ddev drush cr
Turn it off with:
uebertool({
features: {
twighmr: false,
},
})