Commit 9420276f authored by Samuel Mortenson's avatar Samuel Mortenson Committed by Sam Mortenson
Browse files

Issue #3280601 by samuel.mortenson: Add simple method to refresh the page when editing *.sfc files

parent e5c92289
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -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

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);
+1 −1
Original line number Diff line number Diff line
@@ -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>

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
+14 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
 */

use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\sfc\ComponentNameHelper;

@@ -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