0

I want to have an ASP.NET Server which serves an Angular SPA. The SPA should not be served at /, but on a subpath, say /spa. Basically that means I just have to put the angular build output results in the wwwroot folder (the public folder for serving assets), so that e.g. the index.html file can be found under wwwroot/spa/index.html, so that it will be accessible at /spa(/index.html).

This means all assets will also be served unter this subpath, e.g. /spa/polyfills.js. To make this work from the angular side, we need to set the html base tag accordingly to <base href="/spa/">. This means all asset requests will be prefixed with spa, and be therefore correct.

Now, when running Angular in development mode with ng serve, this doesn't work. The browser will make requests to e.g. /spa/polyfills.js, which gives me an error NS_ERROR_CORRUPTED_CONTENT, probably because Vite can't find the file, because it is not properly configured and would only find the file at /polyfills.js.

To fix this in Vite, the base option could be used in a vite.config.ts:

export default {
    base: '/spa/',
}

However, Angular doesn't let one configure the Vite configuration, and I can't find a setting in Angular to adjust this behaviour. How can I fix this?

3
  • 1
    The base path can be configured during build process by cli. so you don't need the update the html manually ... so you can use different path as on other stages. Commented Feb 8 at 14:56
  • 1
    Missed the link to documentation: angular.dev/cli/build the option is called base-href Commented Feb 8 at 14:57
  • I did stumble upon the --base-href option, but it is not available for ng serve, only for ng build. However, I now did find the --serve-path option for development, which solves the problem. I wonder why those are different options, since I usually want the same result when serving and building... 🤷 Thank you for pointing into the right direction again! Commented Feb 10 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.