Basically that won't work besides making your CDN somehow proxy requests for which not static file exists.
(In case it’s not clear, Fastly is also caching and serving these requests as a CDN.)
In a lot of sensitive businesses that wouldn't be allowed.
You can't guarantee the integrity and privacy of the full request and response chain.
This goes against various security standards and ISOs.
You might be able to get away with it and trust the CDN but that's an awful lot of trust.
A lot of people seem to think that there is a big difference between paying a lessor (e.g. Hetzner) for a server on which you terminate TLS, paying a cloud host (e.g. Amazon) to terminate TLS, and paying a CDN (e.g. Fastly) to terminate TLS. Legally there is no difference aside from the specific language of the contracts, which you can review and negotiate in advance.
The difference security-wise is entirely down to the operations of each company, which again you can review and discuss in advance. Strictly speaking a CDN should have lower risk than a host since they are not persisting sensitive data (if you set your cache headers correctly). And as discussed above, using one domain helps avoids cross-domain security concerns.
Given the fact I have control over domain(s), I'd have http://domain.tld/api for API and serve static js from http://domain.tld/static/*.js
Given the fact I have the need for CDN, it means I've got enough traffic that justifies the bill incurred
What is the benefit or purpose of doing this?
I think we may be talking past each other and one of us is misunderstanding OP.
Say you stand up a website with an API at https://example.com. You host your static assets on the CDN, at https://example.myfastcdn.com/. So example.com's home page looks like:
<head>
<script src="https://example.myfastcdn.com/app.js"></script>
</head>
<body>
<div id="site-root"></div><!-- app.js renders some application here -->
</body>
The origin when you load your site is https://example.com, so the scripts hosted on the CDN can still make API requests to https://example.com.What am I missing here?
It's like saying that a house built without a lock is a made up issue and that no lock pr door is needed.
Public CDN should never be trusted. If you use a CDN in the first place and have strict security requirements, then you create your own private CDN. And if you can control that private CDN, you have all the ingredients to avoid CORS.
It's really that simple. No one is saying you are wrong, but you're refusing to look at the entire picture and you focus only on a subset, in which - of course - your argument works.
I'm sorry but that's simply not an acceptable constraint.
No one is telling you not to deal with CORS your way. Fact of the matter is that you can avoid it, but you're making up reasons why you can't. The only reason you can't is because you won't. You're free to use whatever approach you like, there's no police here, just don't state that I or anyone else wrote what we didn't. It'd be grown up thing to do. Thanks and best of success with your projects.
It's a very common case.