Google's AMP (Accelerated Mobile Pages) guidelines suggest that the loading of the AMP javascript should be async
:
<script async src="https://cdn.ampproject.org/v0.js"></script>
Is there a way to ensure a local fallback version of the javascript file is loaded, should the CDN fail to deliver?
Normally, I would add a js test immediately after the script is loaded to see if it loaded some constant (like jQuery
). However, when async
is used, there doesn't seem to be a simple way to determine if it will load since there's a potential race condition between the loading of the script and the test that determines if the script loaded.
Yes, there is a way to provide a local fallback:
<script src="https://cdn.ampproject.org/v0.js" onerror="document.write('<script src=\'v0.js\' async></script>')" async></script>
Tested in the latest Firefox.