Browse Source

browsersync reload via rest hooks

Replace the .bs-reload sentinel-file touch (+ its webpack file-watch) with a
direct, non-blocking ping to BrowserSync's reload endpoint on the REST save
events the block editor actually fires (rest_after_insert_page/post) plus
customizer + nav-menu saves. WP_DEBUG-gated. Removes the .bs-reload file and
its .gitignore entry.
windhamdavid 4 days ago
parent
commit
c736e7fa05
3 changed files with 17 additions and 10 deletions
  1. 0 1
      .gitignore
  2. 17 8
      functions.php
  3. 0 1
      webpack.config.js

+ 0 - 1
.gitignore

@@ -11,4 +11,3 @@ build/
 .vscode/
 
 _claude/scratch/
-.bs-reload

+ 17 - 8
functions.php

@@ -188,15 +188,24 @@ function dw_ask_widget() {
 }
 add_action( 'wp_footer', 'dw_ask_widget' );
 
-// Dev only: on a real post/page save (Gutenberg "Update"), touch a sentinel file that
-// BrowserSync watches, so the front-end preview reloads — DB content changes aren't
-// visible to a filesystem watcher otherwise. Gated to WP_DEBUG → no-op in production.
-function dw_bs_reload_on_save( $post_id ) {
-	if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) return;
-	if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) return;
-	@touch( get_template_directory() . '/.bs-reload' );
+// Dev only: on a real content save, ping BrowserSync's reload endpoint so the front-end
+// preview refreshes — DB content changes aren't visible to a filesystem watcher otherwise.
+// Hooks the REST save events the block editor actually fires (+ customizer + nav menus).
+// Gated to WP_DEBUG → no-op in production.
+if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
+	function dw_bs_reload_on_save( $post = null ) {
+		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
+		if ( $post && is_object( $post ) && $post->post_status === 'auto-draft' ) return;
+		wp_remote_get( 'https://daw.stu:81/__browser_sync__?method=reload', array(
+			'blocking'  => false,
+			'sslverify' => false,
+		) );
+	}
+	add_action( 'rest_after_insert_page', 'dw_bs_reload_on_save', 10, 3 );
+	add_action( 'rest_after_insert_post', 'dw_bs_reload_on_save', 10, 3 );
+	add_action( 'customize_save_after',   'dw_bs_reload_on_save', 10, 3 );
+	add_action( 'wp_update_nav_menu',     'dw_bs_reload_on_save', 10, 3 );
 }
-add_action( 'save_post', 'dw_bs_reload_on_save' );
 
 add_action('wp_footer', 'dw_analytics', 22);
 function dw_analytics() { ?>

+ 0 - 1
webpack.config.js

@@ -123,7 +123,6 @@ module.exports = {
         './parts/**/*.html',
         './patterns/**/*.php',
         './theme.json',
-        './.bs-reload', // touched by save_post (functions.php) → reload after Gutenberg "Update"
       ],
       ignore: ['node_modules', 'build', 'js/v4-*', '*.min.*'],
       open: 'external',