What’s the Right Way to get and save remote data for a Gutenberg block?

Question

I’m writing a new block, where one of the pieces of input is a URL to a remote API. That remote API returns some data (in the first instance, a CSV file), which should be saved locally, and then is accessed by the front-end of the block (i.e., I have to get the name/URL of the local file to some front-end javascript).

The way I’m doing it right now is, I’ve implemented my own route and endpoint for the REST API, so that when the author inputs the URL in the editor, it does a PUT to my endpoint. The backend grabs the remote file from that URL, stores it locally, and returns the local file name. That response to the REST API (the local file name) is then stored in another attribute in the block.

Someone pointed out that this probably isn’t correct, as I probably shouldn’t be retrieving that remote data or saving anything until the post is saved. What’s the Right Way to do this?

0
philolegein 2 months 0 Answers 10 views 0

Leave an answer