REST Resource asset - multiple request URLs


(Tom Stringer) #1

Hi all

I’m trying to build a Content Container Template that will allow an editor to specify up to 10 Instagram posts to show in a styled timeline.

The Instagram API provides an endpoint to fetch specific posts with a basic request (see: https://developers.facebook.com/docs/instagram/embedding#sample-request).

Ideally, I’d like to use a REST Resource asset to dynamically call the posts as provided by the editor, looping through and returning the data for each one, but if I try to print the results of a REST Resource asset call in an SSJS loop, it only returns the first result.

For example, the following has three requests, but only returns the first post:

<script runat="server">
	var rawPostURLs = "%globals_asset_metadata_instaPosts%",
			postURLs = rawPostURLs.split(';');

	for (i=0; i < postURLs.length; i++){
		print('%' + 'globals_asset_contents_raw:3387544^with_get:postURL' + postURLs[i] + '%');
	}
</script>

Whereas this returns all three:

<script runat="server">
	var rawPostURLs = "%globals_asset_metadata_instaPosts%",
			postURLs = rawPostURLs.split(';');

	print('%' + 'globals_asset_contents_raw:3387544^with_get:postURL' + postURLs[0] + '%');
	print('%' + 'globals_asset_contents_raw:3387544^with_get:postURL' + postURLs[1] + '%');
	print('%' + 'globals_asset_contents_raw:3387544^with_get:postURL' + postURLs[2] + '%');
</script>

If an editor can specify up to 10, 15 or 20 posts, then the second method becomes unworkable pretty rapidly.

It might be that the best solution is to loop through the requests with an AJAX call (or some other pure JS method), but the REST Resource asset provides some useful functionality and would be our preferred method if possible.


(Bart Banda) #2

Hey Tom,
Your best bet would def be to loop it, but if you are making the requests server-side, you’d pretty quickly get a timeout based on how fast the Instagram API is at returning the posts. It’d be a very slow un-cached page load as well.

I’d def recommend to do this client-side to speed up the page rendering and reduce the load on the server. Especially if the user adds multiple of these content templates on the same page, could result in houndreds of server-side GET calls being made…


(Tom Stringer) #3

Thanks Bart - good advice.

Just curious though that attempting that loop server-side only returns the first result - is that expected?


(Bart Banda) #4

No that’s not expected. I’d say it’s most likely related to caching. But it’s weird that you get a different result of you loop it vs hardcoding it. Maybe Matrix looks at the single print JS statement as a single cache entry where as the other multiple ones it treats as separate ones? You could try and turn off Matrix caching for this asset and the REST asset to see if that makes a difference?