1

I have an Angular app that uses an image in the nav bar like so: flag image

My problem is that whenever i enable production mode the site is not able to reach the image.

here is some context:

  • The image is located in a wwwroot folder: \wwwroot\Images\flags (the wwwroot is the default folder for c# web projects)
  • This is my environment.prod.ts:
export const environment = {
    production: true,
    apiUrl: '/api'
};
  • This is my environment.ts:
export const environment = {
    production: false,
    apiUrl: 'https://localhost:5569/api'
};
  • This is my lang.service.ts: from where i get the image:
getFlagUrl(fileName: string): string {
    const flag = fileName.split('-')[0];
    return `${environment.apiUrl.replace('api', '')}/Images/flags/${flag}.png`;
}

FYI

  • I use a back-end server with c# and i hit my API's with URL:PORTNAME/api
  • This is the error message when executing the app: image error code
  • In production mode i move my app into a local server with a different IP address than localhost. (I do not know the ip address, it should be dynamically gathered by the app itself no?)
  • if i use localhost/api in production mode i am able to see the flag in the local server but not from another computer, i am stuck at this lol)

Anyway i think it has something to do with the url in production?

devludo
  • 93
  • 10
  • "I do not know the ip address, it should be dynamically gathered by the app itself no?" no, unless by "dynamically gathered" you mean via DNS. How should it? If even you don't know where the server is located, how should your app? In actual production environments if the frontend is hosted standalone (i.e not by the same web server where your API is hosted) then frontend must be told where to look for the API, normally this is just a name (e.g `www.example.com`) and the frontend then asks a DNS server for the IP corresponding to that name, but it must be told where it can find the backend. – MindSwipe Sep 20 '22 at 06:41
  • Yes you are right i can access the site via `https://ipaddress:5569` this is the url that my backend server is hosting the app on... the angular web project is hosted by the server itself. That is why in debug mode i have to provide a URL because i have my Angular app (hosted on localhost:4200) and my back-end app hosted on (localhost:5569). When in production mode i let the backend host my app (transfering the dist folder into backend project), i am confused here... – devludo Sep 20 '22 at 07:02
  • One of 2 things, or you manually have to specify the prod url in the env.prod file, or some how get the url from the server. Right now its trying to connect to the https://images/flags/en.png with as you can see is not a correct url. – Dmyto Holota Sep 20 '22 at 07:11

1 Answers1

1

environment.apiUrl.replace('api', '') leads to:

  • https://localhost:5569//Images/flags/en.png for debug mode and to
  • //Images/flags/en.png in production.

Notice the double slash; while for the debug version, the browser fixes the error, in production an URL starting with a double slash is a valid URL that references another host, but uses the same protocol (http/https). See this answer for an explanation.

In order to make this work, you should get rid of the double dash, e.g. by using relative links

return `${environment.apiUrl}/../Images/flags/${flag}.png`;

or including the slash in the replace:

return `${environment.apiUrl.replace('/api', '')}/Images/flags/${flag}.png`;
Markus
  • 20,838
  • 4
  • 31
  • 55
  • Sorry i did't see your answer, it turns out you're right. Just added the `/` in the replace and now it works fine. Again sorry for such a late reply, i'll mark the aswer as correct! – devludo Sep 27 '22 at 15:10