Add Javascript in AMP Pages

前端 未结 5 1331
灰色年华
灰色年华 2020-12-06 02:37

Please help me understand how to add javascript in the AMP (Accelerated Mobile Pages). My requirement is I get an ID in URL. For example localhost:8080/home?id=1.I would lik

5条回答
  •  被撕碎了的回忆
    2020-12-06 03:07

    As far as I know, you can add Javascript to AMP by hosting the AMP script on your origin and intercept the request to get the script using a Service Worker. This technique is called "AMP as PWA". Here is the code

    function createCompleteResponse (header, body) {
      return Promise.all([
        header.text(),
        getTemplate(RANDOM STUFF AMP DOESN’T LIKE),
        body.text()
      ]).then(html => {
        return new Response(html[0] + html[1] + html[2], {
          headers: {
            'Content-Type': 'text/html'
          }
        });
      });
    }
    

    More explanation here: https://www.smashingmagazine.com/2016/12/progressive-web-amps/#amp-as-pwa

提交回复
热议问题