Azure static website with Azure CDN shows white screen only

212 views Asked by At

I have hosted this angular website angular-tour-of-heroes in Azure storage. The storage link works as expected.

I configured a custom domain in Azure CDN to serve the Azure static website. The domain configuration works fine when you upload a simple index.html file. However, when you upload the angular project, it only shows a white screen. The angular project is built with the default configuration of the angular project. Additionally, if you serve it from the Azure CDN endpoint, it is not working and is the same as the custom domain.

ng build --configuration production

The content of the $web storage folder in the Azure storage is

  1. index.html
  2. favicon.ico
  3. main.*.js
  4. polyfills.*.js
  5. run.*.js
  6. styles.*.js

I realized in the F12 browser console that the Content-Type from Azure CDN is always text/html for all the files. However, the Content-Type when served from the Azure static website storage link is according to the $web folder content-type in the storage.

Content-Type in the Azure storage

Files with Content-Type in the Azure Storage

Content-Type in F12 browser console for Azure CDN configured F12 Console for Css file

F12 console for Js file

I also modified to Content-Security-Policy of the Azure CDN rule to allow inline-scripts/inline-styles, but doesn't seem to work.

Additionally, the console error is

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Content of index.html page

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>AngularTourOfHeroes</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="styles.ef46db3751d8e999.css">
</head>

<body>
  <app-root></app-root>
  <script src="runtime.6e50dac17b84e715.js" type="module"></script>
  <script src="polyfills.716cc9a230a87077.js" type="module"></script>
  <script src="main.93983fc3aa1a4437.js" type="module"></script>

  <!-- <script src="runtime.6e50dac17b84e715.js" type="application/javascript"></script>
  <script src="polyfills.716cc9a230a87077.js" type="application/javascript"></script>
  <script src="main.93983fc3aa1a4437.js" type="application/javascript"></script> -->

  <!-- <script src="runtime.6e50dac17b84e715.js" type="text/javascript"></script>
  <script src="polyfills.716cc9a230a87077.js" type="text/javascript"></script>
  <script src="main.93983fc3aa1a4437.js" type="text/javascript"></script> -->

  <!-- <script src="runtime.6e50dac17b84e715.js"></script>
  <script src="polyfills.716cc9a230a87077.js"></script>
  <script src="main.93983fc3aa1a4437.js"></script> -->

</body>

</html>

Error when changing the tag Error changing script tag

Are there any other configurations to be done in the Azure CDN to serve an Angular project or this is something related to Angular?

2

There are 2 answers

0
ARH On BEST ANSWER

Finally, I found the issue with the Azure CDN. I implemented the following rule in the Azure CDN which somehow is causing the error. I followed this rule to allow SPA refresh or URL routing, however, it ended up creating another problem.

SPA rule

1
SaiVamshiKrishna On

The error message "Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of 'text/html'" in the context of an Angular application usually indicates an issue with the server configuration not recognizing JavaScript files as such.

Here are steps to resolve this issue specifically for an Angular application hosted on Azure CDN:

Azure CDN Configuration: Ensure that your Azure CDN is configured to recognize JavaScript files with the correct MIME type. You need to set the appropriate Content-Type for JavaScript files.

Origin Server Headers: Check the headers that your Azure Storage (or any other origin server) is sending for JavaScript files. The Content-Type header for JavaScript files should be set to application/javascript or application/ecmascript. If your storage is configured to set headers, ensure that the correct MIME type is being sent.

Angular index.html Configuration: In your Angular index.html file, check if the <script> tags for your main, runtime, and polyfills JavaScript files are using the type="module" attribute. If they are, consider removing this attribute to see if it resolves the issue.

Angular Build Output: Run ng build --configuration production to ensure your Angular project is built correctly for production. Confirm that the Content-Type is correctly set in the Angular build output.

Example:

<script src="runtime.6e50dac17b84e715.js" type="module"></script>
<script src="polyfills.716cc9a230a87077.js" type="module"></script>
<script src="main.93983fc3aa1a4437.js" type="module"></script>

Change to:

<script src="runtime.6e50dac17b84e715.js"></script>
<script src="polyfills.716cc9a230a87077.js"></script>
<script src="main.93983fc3aa1a4437.js"></script>

Please refer to this

SO Link1

SO Link2