Can you share the amp-state
code you’re using? It would only be stripping it out if it is invalid JSON. Make sure it is not malformed. I recommend printing out the JSON using wp_json_encode()
. So if you have $data
then do:
<amp-state id="myState"><script type="application/json"><?php echo wp_json_encode( $data ); ?></script></amp-state>
Yes, you’re right, I tested it was stripped out due to invalid JSON.
Thanks.
@andrei0x309 Glad we were able to help! Please do feel free to leave a review – we’d love to hear your feedback on the plugin.
I didn’t answer your initial question, however.
Passing data between amp-script
instances is not something I’ve done much of, but yes, using amp-state
as messaging protocol is one way I’m familiar with: https://amp.dev/documentation/components/amp-script/#referencing-amp-state
I was thinking there could be the possibility of using postMessage
to pass messages between workers, or perhaps use BroadcastChannel
or MessageChannel
. But none of these seem to be supported by WorkerDOM. This seems like a great question for AMP on StackOverflow: https://stackoverflow.com/questions/tagged/amp-html
I am not sure that localStorage is an improvement over using AMP-state.
So I guess I’ll stick setState()
/getState()
, in theory, exploiting exportFunction(n,f)
from the AMP flavor of WorkerDom should work somehow but I couldn’t manage to make a working example, anyway, I saw on GitHub somewhat related issues like worker-dom issue 1017 and worker-dom issue 839 so I’ll wait maybe something better will come up.
On my NON-AMP template, I had a simple custom WP-REST/FETCH commenting system that also integrated default WP comment-reply.js
I saw a git-pull on amp-wp amp-wp pull 4674 trying to use amp-script
for comment-reply.js
, I’ll try too to see if I can get anywhere with amp-script
for my commenting system.
Thanks again for the plugin and the help.