Attached JS Libraries Not Working When Caching Turned Off On Block

PROBLEM
Came across a weird one yesterday. I had just finished touches on a HighCharts block. I noticed that it wasn’t refreshing with updated content. Well I know what to do about that.

I added the getCacheMaxAge(). But then! Oh no!

I got a JS error saying HighCharts wasn’t defined. What the? And viewing the source, the two libraries I was including in the block were not included. What happened?

SOLUTION
I’m not fully sure why this happened the way it did. I tried several other things like adding the kill switch (\Drupal::service("page_cache_kill_switch")->trigger();) and adding "#cache" => array("max-age" => 0) to the returned array in the build method.

I then just decided to try a shot in the dark and instead of making the cached age 0, I made it 1 (as in one second)… So just this in the build method:

return array(
	...
	"#cache" => array(
		"max-age" => 1
	),
	...

So… whatever Drupal. It works… until it doesn’t.