Sounds like a threading issue where one thread is trying to loop through the bundle with a for each and the other one is trying to add items to it. Solution is to add locks both on writing and reading that collection in that case. Or set it up once and for all in application start where there is only one thread and cache the result...
Or use variables that are unique per request. Haven't checked but I guess that Bundles collection above is a static/cached collection that is shared between all incoming requests. Modifying it in one will affect all others resulting in random errors depending where in execution they are.
I have tried to implement something like dynamic bundling. The idea is that on one page I might have different blocks and each of them to require different script files. I want to combine them in one bundle, which is page specific. What I have implemented:
Then in everyblock, which requires custom scripts, I have the following
I combine all page specific scripts. Then, I have another method:
and in the _Root.cshtml I invoke it
At the end on every page I have the following result
... and so on for the rest of the pages. As you can see, I am using the page request path as a unique bundle identifier. It all worked well in our dev & test environments. However, when we deployed to production I started getting the following error:
Another weird thing: this is reproducible only if I copy and paste the link (for example the homepage) in a new browser tab and it happens randomly. If have the site already opened, and I click on the logo or other link pointing to the homepage, it is not reproducible.
I would appreaciate any help on this. Thank you in advance.