18
votes

I have a SPA (built with angular) and deployed to Azure Blob Storage. Everything works fine and well as you go from the default domain but the moment I refresh any of the pages/routes, index.html no longer gets loaded and instead getting the error "the requested content does not exist"

Googling that term results in 3 results total so I'm at a loss trying to diagnose & fix this.

6

6 Answers

27
votes

You can simply configure the error page to index.html in your static website:

enter image description here

5
votes

Actually the issue was I didn't have 404.html defined -- the blob storage for SPA doesn't understand what file to serve for any other routes than the root one. So every other route will go to the 404 file. But in a SPA even the 404 goes through the index file. So all I did is mention index.html as my 404 file and all is well.

3
votes

For me adding the index.html page as the Error document page did not help when navigating by url as it would still reload the app. I posted an answer elsewhere relating to rather using the Angular HashLocationStrategy and that does not cause a page reload when changing the URL manually.

Answer on other SO question

2
votes

There is a new static Webapps solution by Microsoft. It is currently in preview mode but I think it is the most convenient way to use/deploy a SPA in the Azure infrastructure. You can use your custom domain with free SSL, version control, and set up a route to redirect everything to the index.html (fallback routes: https://docs.microsoft.com/en-us/azure/static-web-apps/routes) for example. see more details here: https://docs.microsoft.com/en-us/azure/static-web-apps/

1
votes

Generally, you've created a CDN profile and an endpoint, but your content doesn't seem to be available on the CDN. Users who attempt to access your content via the CDN URL receive an HTTP 404 status code. You can follow these methods in troubleshooting Azure CDN endpoints that return a 404 status code

There are several possible causes, including:

The file's origin isn't visible to the CDN. The endpoint is misconfigured, causing the CDN to look in the wrong place. The host is rejecting the host header from the CDN. The endpoint hasn't had time to propagate throughout the CDN.

With CDN, At the initial request, the client directly accesses to the origin server, afterward, at the following request, when you refresh the page, the client requests to the CDN cache server until their time-to-live (TTL) elapses. See Manage expiration of Azure Blob storage in Azure CDN and Control Azure CDN caching behavior with caching rules.

In this case, you may ensure websites blob content is publicly available on the Internet. After that, you may verify that your origin settings are properly configured. Verify that the values of the Origin type and Origin hostname are correct. Verify HTTP and HTTPS ports is represented as your static website is listening on. Kindly you could get more details from that troubleshooting link.

0
votes

TL/DR

You could set the error document (404) to also be your index.html

This is a quick fix that will still return 404, however will also actually follow your deep link.

This isn't a 'fix'. It's more of a hack - the real fix is to add a CDN with some URL redirect rules on your hosting server. here is a great guide: https://antbutcher.medium.com/hosting-a-react-js-app-on-azure-blob-storage-azure-cdn-for-ssl-and-routing-8fdf4a48feeb

Rule itself

But to save you the click, the CDN rule using standard microsoft CDN (the cheaper one) is something like this:

(add the condition with the '+ condition' button)
If URL file extension > less than 1 extension > no case transform

(add the action with '+ Add action' button)
source pattern: '/' > Destination: '/index.html' > preserve unmatched path: no

Explanation

Ill attempt to add an explanation that I think nobody else did nicely.

What this rule will do is say any URL request that isn't for a direct file, eg.

example.com/xyz
example.com/user/xyz
example.com/tabs/post/12345

Or ANYTHING without a direct file extension (like '.png' or '.pdf' or '.html')

Then we will rewrite the URL to be 'index.html' this is the host file where the SPA has javascript to handle deep links for paths like in the example - therefore you will not get a 404 and the code will handle gracefully.