I have a custom block
'#attached' => [
'drupalSettings' => [
'myblock' => $settings
],
...
When I do not turn off cache, the settings are properly written into JS variables and I can fetch them in frontend. But I need to work with images on pages and when this block is cached, it is not displayed per page, but there is the first data loaded (so page 1 has the same data as page 2, although I need different data and I fetch different data).
This can be solved by turning off the cache
public function getCacheMaxAge() {
return 0;
}
But then the variable in JS is not set and I cannot fetch it in frontend. Why?