I have some customer orgs that still work on Classic and didn't enable MyDomain. This causes strange errors. Is there a way I can check with Apex if an org has a MyDomain enabled?
I would just check:
Boolean isMyDomainEnabled = URL.getSalesforceBaseUrl().getHost().endsWith('my.salesforce.com');
Boolean isMyDomainEnabled = URL.getSalesforceBaseUrl().getHost().contains('--');
That should return true for all orgs with MyDomain enabled, as the url format there would be
domain--sandbox.cs42.my.salesforce.com. In orgs without, it would just be
URL.getSalesforceBaseUrl().getHost();. In some cases you might also get useful info out of a query such as
[SELECT fullPhotoUrl FROM User WHERE userType = 'Standard' LIMIT 1].
Credit to Cristophe Coenrats for both. His repo here https://github.com/ccoenraets/lightning-component-apex-continuation contains code that allows Apex continuations in Lightning, and as a result his Visualforce and Lightning components need to successfully guess each other's base URLs in order to communicate successfully.