Ben Carey
Ben Carey

Reputation: 16948

Hosting SPA with Static Website option on Azure Blob Storage (clean URLs and Deep links)

I have succesfully set up a static website on Azure Blob storage using the $web container as advised within the Microsoft documentation. In addition, I have defined index.html for both the 'Index Document Name' and the 'Error Document Name'. This is because I want my JavaScript application to handle the 404 responses.

However, when looking at the network tab in Chrome, I can see that a 404 is being thrown for the URL that I am requesting, despite the page actually working.

My theory is as follows:

The problem I have is that my URLs are clean urls, for example:

/activities/some-slug is never going to exist as a file, this just tells my JavaScript application where to route the request i.e. what view to load.

Just to be clear, the page does work because it is still routing 404 errors to index.html.

How can I tell Azure blob to just route absolutely everything to index.html and let my app throw the 404 if necessary?

We do have a Premium Verizon CDN set up already because I was not originally aware of the static website option on Azure Blob. If the CDN is the best route, please can you provide instructions on how to configure the CDN to provide what I am looking for?

What I have tried/researched

I have read numerous articles (specifically the comments in this one) and I believe that there may be various solutions i.e. Azure Functions, Proxy, CDN etc. That said, none provide clear instructions of what to actually do with those specific services.

I usually work on AWS so am not familiar with all of the Azure services and available configurations.

Upvotes: 22

Views: 8578

Answers (4)

vpetit
vpetit

Reputation: 1

It is now possible with the Static website configuration on Azure Storage Account In the Static website configuration of the a Storage account, you have to set index.html to both the "index document name" and "the error Document Path"

Static website configuration on Azure Storage Account

Indeed, you must delegate to your SPA (angular/react...) all the routing mecanism, including the page not found error

Upvotes: 0

Mark Wiemer
Mark Wiemer

Reputation: 50

Thanks @Dima Tisnek for the shorter answer. Here's the answer in plaintext (the edit queue is full for that answer):

Azure Endpoint Resource > Rules engine > Add rule

Name: spa

  1. If URL Path [Operator: Not begins with] [Value: /static/] [Case transform: No transform]
  2. then URL rewrite [Source pattern: /] [Destination: /index.html] [Preserve unmatched path: No]
  3. and then cache expiration [Cache behavior: Override] [Days: 0] [Hours: 0] [Minutes: 5] [Seconds: 0]

rules described above

Upvotes: 1

Dima Tisnek
Dima Tisnek

Reputation: 11781

Kudos to Andreas Wendl at https://github.com/MicrosoftDocs/azure-docs/issues/43257#issuecomment-580668444

Here's a simpler / possibly more correct solution:

enter image description here

  • leave /static/* be
  • rewrite everything else to /index.html

So, index.html is served in response to deep links like /users/ and let's say /users/alexa.siri.

Upvotes: 7

Andreas Wendl
Andreas Wendl

Reputation: 624

I think I found a solution

  1. Add a new rule
  2. Condition: URL file extension

    a. Operator: Less than

    b. Extension: 1

    c. Case transform: No transform

    [This basically means that the URL requested has no file (extension) but is a route of the app which will be taken care of by the app routing]

  3. Action: URL rewrite

    a. Source pattern: /

    b. Destination: /index.html

    c. Preserve unmatched path: No

  4. Save rule

Screenshot.

Upvotes: 40

Related Questions