-1

I've deployed standard Angular apps to Azure. I just add an index.js file using express to the root, zip the dist folder and use web deploy to release the app.

The output for an Angular Universal app is different; there are two folders, one for the browser and one for the server.

What I've tried so far:

  • zipped the browser and server folders
  • added an index.js that sets main.js as the starting point

This just outputs the content of main.js.

How can I get the app to run using SSR?

aic
  • 145
  • 2
  • 13
  • I found a [similar issue](https://stackoverflow.com/a/53632633/13903626) and it has shared the answer, please check it and kindly share the result here – Vito Liu Aug 14 '20 at 10:28
  • Not get your latest information, is the ticket helpful for you? Or if you have any concern, feel free to share it here – Vito Liu Aug 19 '20 at 09:47
  • No this answer didn't work for me, I have found a solution which I will post later. – aic Aug 20 '20 at 10:52
  • Hi aic, Please kindly share you answer here and you could accept your answer, In this case, others could directly find the useful answer, thanks. – Vito Liu Aug 25 '20 at 07:45

1 Answers1

1

To get this working required changes to the Angular app and configuring the DevOps pipeline correctly.

The Angular app required 3 changes:

  1. Enable SSR

ng add @nguniversal/express-engine

  1. Add a wildcard route to app-routing.module.ts

const routes: Routes = [ { ... { path: "**", redirectTo: "" } ];

To ensure an App container is up and running, Azure will send requests to robots933456.txt. If that route isn’t handled the app won’t start.

  1. Update server.ts for Production

Replace this line in server.ts:

const distFolder = join(process.cwd(), 'dist/YOUR_APP_NAME/browser');

With:

let distFolder = join(process.cwd(), "browser");
if (!existsSync(distFolder)) {
 distFolder = join(process.cwd(), "dist/YOUR_APP_NAME/browser");
}

Why? Because when you build an Angular Universal app two folders are output:

  1. dist/YOUR_APP_NAME/browser
  2. dist/YOUR_APP_NAME/server

When your app is deployed to Azure, the contents of the server folder will be at the root directory, so to make sure your local build and production build both work, the dist folder needs to be configured differently for each environment.

That’s it for the Angular changes, the rest of the work is done in Azure DevOps.

Build Pipeline steps:

  1. npm step: install the Angular CLI on the build server

    install @angular/cli -g

  2. npm step: install node packages

    npm install

  3. Command line step: build the Angular app using SSR

    npm run build:ssr

4: Copy files step: copy the server folder contents to a new dist folder

Source:

dist/YOUR_APP_NAME/server

Target:

client/YOUR_APP_NAME/deploy-dist
  1. Copy files step: copy the browser folder to the new dist folder

Source:

client/browser/dist/YOUR_APP_NAME/browser

Target:

client/browser/deploy-dist/browser

Steps 4 & 5 are required to have the correct folder structure.

  1. Command line: rename main.js

    rename main.js index.js

index.js is required to get express running.

  1. Archive files step: zip the deploy-dist folder

  2. Publish Pipeline Artifacts step: publish the zip

Release Pipeline step: Deploy the artifact using Azure App Service deploy

aic
  • 145
  • 2
  • 13
  • Can you share Some More details, Facing the same issue. In the 8th step, I have a doubt. – Karnan Muthukumar Sep 17 '20 at 05:57
  • Can you share some screenshot for pipeline and the folder structure of angular application. – Karnan Muthukumar Sep 17 '20 at 06:00
  • Which part is not working, the release pipeline to deploy? – aic Sep 17 '20 at 12:37
  • Thanks for ur response. I need the web.config part can you share with me. Also, I have used the windows app service is it ok? else shall I change app service?. can you tell me – Karnan Muthukumar Sep 17 '20 at 15:19
  • If you're using a windows app service, look at this thread: https://stackoverflow.com/questions/53480198/deploying-angular-universal-to-azure/53632633#53632633. With Linux there is no web.config. – aic Sep 18 '20 at 09:05
  • Same content as https://bossprogrammer.medium.com/how-to-deploy-an-angular-10-universal-app-with-server-side-rendering-to-azure-a2b90df9ca64. If this is you, you should probably make note of that. – General Grievance May 10 '23 at 23:36
  • @GeneralGrievance Yes the blog post is mine. – aic May 11 '23 at 11:43