• Hi,

    I’m developing a plugin that provide service worker for WordPress site. My question, is there a way to install my service worker through amp-install-serviceworker element? Will be great if it’s possible without use helper like Jetpack. I tried to hook it to amp_post_template_footer or amp_post_template_header but it doesn’t work.

    Thanks,

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    Please share the code you’re using to hook into amp_post_template_header and amp_post_template_footer.

    Thread Starter Yohan Totting

    (@tyohan)

    It’s like this. I have a class that call from main file of my plugin. Inside class contructor function I called through add_action.

    
    protected function __construct() {
            add_action('wp_print_footer_scripts', array($this,'registerSW'),1000);
            add_action( 'template_redirect', array( $this, 'renderSW' ), 2 );
            add_filter( 'query_vars', array( $this, 'registerQueryVar' ) );
            add_action( 'init', array( $this, 'registerRewriteRule' ) );
            
     
            $this->registerAMPServiceworker();
            
        }
    
    public function registerAMPServiceworker(){
            // AMP support
    		add_action( 'amp_post_template_head', array( $this, 'renderAMPSWScript' ) );
    		add_action( 'amp_post_template_footer', array( $this, 'renderAMPSWElement' ) );
        }
    
        public function renderAMPSWScript(){
            echo '<script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>';
            
        }
    
        public function renderAMPSWElement(){
            echo '<amp-install-serviceworker src="'.$this->getSWUrl().'" layout="nodisplay"></amp-install-serviceworker>';
        }
    • This reply was modified 7 years, 2 months ago by Yohan Totting.
    Plugin Author Weston Ruter

    (@westonruter)

    It would be helpful if you could provide snippet which can be run as-is. What you’ve shared is hard to debug. This works for me:

    <?php
    
    class Try_Service_Worker {
    
    	public function __construct() {
    		$this->registerAMPServiceworker();
    	}
    
    	public function registerAMPServiceworker(){
    		// AMP support
    		add_action( 'amp_post_template_head', array( $this, 'renderAMPSWScript' ) );
    		add_action( 'amp_post_template_footer', array( $this, 'renderAMPSWElement' ) );
    	}
    
    	public function renderAMPSWScript(){
    		echo '<script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>';
    
    	}
    
    	public function getSWUrl() {
    		return home_url( '/sw.js' );
    	}
    
    	public function renderAMPSWElement(){
    		echo '<amp-install-serviceworker src="'.$this->getSWUrl().'" layout="nodisplay"></amp-install-serviceworker>';
    	}
    }
    
    $try_service_worker = new Try_Service_Worker();
    Thread Starter Yohan Totting

    (@tyohan)

    This is solved. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use install service worker through action hook’ is closed to new replies.