Access cookies through an API on AMP page

2020-05-29 06:10发布

问题:

I am converting a page to google AMP and need to access cookies to set a view of a division. I am thinking of creating an API for this.

The API will just return all the cookies available on my domain in JSON format. I will hit the API using <amp-state> component and store the returned JSON. Then will take actions according to this state.

Is this a valid approach to use in AMP? Is there any security flow in this?

回答1:

Using amp-list is the right approach in this case. amp-list makes a request to your server, which can read the cookie and return an appropriate JSON response. You can render then the form / button inside the amp-list using amp-mustache.

This samples demonstrates how to do this: https://ampbyexample.com/advanced/favorite_button/.