Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 Failed to load module script using AWS S3 and Cloudfront

I am hosting my Angular 9 application in AWS using S3 and CloudFront Service. After accessing the application I am getting following error in the browser:

  • Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. /polyfills-es2015.5af60e74e954ace452ee.js:1
  • Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. /main-es2015.9d477ae8338087e639b5.js:1
  • Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

My index.html:

<!doctype html>
<html>
<head>    
  <meta charset="utf-8">
  <title>ReachApp Success</title>
  <base href="/reach">
    <!-- TODo: check for common configuration for material icon -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src='https://www.google.com/recaptcha/api.js' async defer></script> 
<link rel="stylesheet" href="styles.7f06624db32e037ad1c6.css"></head>
<body>
  <reach-root></reach-root>
  <div id="displayError"></div> <!-- Display message if IE broswer-->
<script src="runtime-es2015.0dae8cbc97194c7caed4.js" type="module"></script><script src="runtime-es5.0dae8cbc97194c7caed4.js" nomodule defer></script><script src="polyfills-es5.352c85d9e468300d10fb.js" nomodule defer></script><script src="polyfills-es2015.27bdbf82a4d61d823e83.js" type="module"></script><script src="main-es2015.89b57a9808d25011d96d.js" type="module"></script><script src="main-es5.89b57a9808d25011d96d.js" nomodule defer></script></body>
</html>

I already tried different suggestions provided by others by replacing the type="module" to type="text/javascript" but still no luck. Could anyone suggest any possible fix for this issue?

like image 747
Mohit224 Avatar asked Nov 07 '22 04:11

Mohit224


1 Answers

After going through all the comments the most probable cause for this issue is below

Check

  1. If you are deploying in subfolder then build with base_href tag in ng build --prod command
  2. make sure to invalidate cloud front with /*
  3. In AWS cloudfront open redirect tab. Make sure you redirect the error pages(403) to index.html with 200 response

More info about BASE_HREF

--base-href If you deploy your Angular app to a subfolder, the ‘--base-href’ is important to generate the correct routes. This parameter will update the tag inside the index.html.

For example, if the index.html is on the server at /angularapp/index.html, the base href should be set to .

More information: https://angular.io/guide/deployment

ng build --prod --base-href /angularapp/
like image 169
San Avatar answered Nov 11 '22 13:11

San