• Resolved andrei0x309

    (@andrei0x309)


    Is there a better way to share/pass/import functions between two or more amp-scripts besides using setState() and new Function("return" + "function string from state")() ?

    Also, I don’t know if it’s normal behavior but it seems that if you use the <amp-state> tag, the amp-wp plugin strips the <script type="application/json">...</script> that resides inside <amp-state>. This is not a big problem since you can set a somewhat initial state later from an <amp-script>.

    Thanks

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

    (@westonruter)

    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>

    Thread Starter andrei0x309

    (@andrei0x309)

    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.

    Thread Starter andrei0x309

    (@andrei0x309)

    Plugin Author Weston Ruter

    (@westonruter)

    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

    Plugin Support Milind More

    (@milindmore22)

    localStorage maybe?

    Thread Starter andrei0x309

    (@andrei0x309)

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘import/pass functions between amp-script components’ is closed to new replies.