Loading README.md +11 −0 Original line number Diff line number Diff line Loading @@ -572,6 +572,17 @@ As an alternative to disabling the data cache bin, you can run the automatically write assets, or `drush sfc:write <plugin id>` to target a specific plugin. If you want to automatically refresh the page when `sfc:watch` writes a component's assets, you can add this setting to settings.php: ``` $settings['sfc_watch_refresh'] = TRUE; ``` and rebuild cache. This will add a naive bit of JavaScript to each page that checks for the last time any component was written and refresh if it's out of date. **Do not enable this in production**. ## Testing components Since single file components are PHP classes, they can be unit and integration Loading js/watch_refresh.js 0 → 100644 +14 −0 Original line number Diff line number Diff line (function (drupalSettings) { var timestamp = Math.round(new Date().getTime() / 1000); setInterval(function () { fetch(drupalSettings.sfc_watch_file) .then(function (response) { return response.text(); }) .then(function (text) { if (timestamp < parseInt(text)) { location.reload(); } }); }, 1000); })(drupalSettings); phpcs.xml +1 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ <exclude-pattern>dist/*css</exclude-pattern> <exclude-pattern>tailwind.config.js</exclude-pattern> <exclude-pattern>node_modules/*</exclude-pattern> <arg name="extensions" value="inc,install,module,php,profile,test,theme,yml,sfc"/> <arg name="extensions" value="inc,install,module,php,profile,test,theme,yml,sfc,js,css"/> <rule ref="PEAR.Commenting.FileComment.Missing"> <exclude-pattern>*sfc</exclude-pattern> </rule> Loading sfc.libraries.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line watch_refresh: version: VERSION js: js/watch_refresh.js: {} dependencies: - core/drupalSettings sfc.module +14 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ */ use Drupal\Core\Asset\AttachedAssetsInterface; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Drupal\sfc\ComponentNameHelper; Loading Loading @@ -144,3 +145,16 @@ function sfc_require($filename) { $cache = $new_vars; return $new_vars; } /** * Implements hook_page_attachments_alter(). */ function sfc_page_attachments_alter(array &$attachments) { if (Settings::get('sfc_watch_refresh', FALSE)) { /** @var \Drupal\Core\File\FileUrlGenerator $url_generator */ $url_generator = \Drupal::service('file_url_generator'); $attachments['#attached']['library'][] = 'sfc/watch_refresh'; $attachments['#attached']['drupalSettings']['sfc_watch_file'] = $url_generator->generate('public://sfc_watch_file.txt')->setAbsolute()->toString(); touch('public://sfc_watch_file.txt'); } } Loading
README.md +11 −0 Original line number Diff line number Diff line Loading @@ -572,6 +572,17 @@ As an alternative to disabling the data cache bin, you can run the automatically write assets, or `drush sfc:write <plugin id>` to target a specific plugin. If you want to automatically refresh the page when `sfc:watch` writes a component's assets, you can add this setting to settings.php: ``` $settings['sfc_watch_refresh'] = TRUE; ``` and rebuild cache. This will add a naive bit of JavaScript to each page that checks for the last time any component was written and refresh if it's out of date. **Do not enable this in production**. ## Testing components Since single file components are PHP classes, they can be unit and integration Loading
js/watch_refresh.js 0 → 100644 +14 −0 Original line number Diff line number Diff line (function (drupalSettings) { var timestamp = Math.round(new Date().getTime() / 1000); setInterval(function () { fetch(drupalSettings.sfc_watch_file) .then(function (response) { return response.text(); }) .then(function (text) { if (timestamp < parseInt(text)) { location.reload(); } }); }, 1000); })(drupalSettings);
phpcs.xml +1 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ <exclude-pattern>dist/*css</exclude-pattern> <exclude-pattern>tailwind.config.js</exclude-pattern> <exclude-pattern>node_modules/*</exclude-pattern> <arg name="extensions" value="inc,install,module,php,profile,test,theme,yml,sfc"/> <arg name="extensions" value="inc,install,module,php,profile,test,theme,yml,sfc,js,css"/> <rule ref="PEAR.Commenting.FileComment.Missing"> <exclude-pattern>*sfc</exclude-pattern> </rule> Loading
sfc.libraries.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line watch_refresh: version: VERSION js: js/watch_refresh.js: {} dependencies: - core/drupalSettings
sfc.module +14 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ */ use Drupal\Core\Asset\AttachedAssetsInterface; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Drupal\sfc\ComponentNameHelper; Loading Loading @@ -144,3 +145,16 @@ function sfc_require($filename) { $cache = $new_vars; return $new_vars; } /** * Implements hook_page_attachments_alter(). */ function sfc_page_attachments_alter(array &$attachments) { if (Settings::get('sfc_watch_refresh', FALSE)) { /** @var \Drupal\Core\File\FileUrlGenerator $url_generator */ $url_generator = \Drupal::service('file_url_generator'); $attachments['#attached']['library'][] = 'sfc/watch_refresh'; $attachments['#attached']['drupalSettings']['sfc_watch_file'] = $url_generator->generate('public://sfc_watch_file.txt')->setAbsolute()->toString(); touch('public://sfc_watch_file.txt'); } }