1
votes

I have a multi component application in one root module in Agular 9.

The problem is when I deploy it via ng build --prod and copy it from the dist-folder to my local apache XAMPP server everything works except for the images.

The error message is:

Failed to load resource: the server responded with a status of 404 (Not Found) I have tried changing the path of the images and it didn`t work.

What am I doing wrong?

In the index.html I set <base href="/travelAgency/">

And this is my angular.json where I added "baseHref" : "/travelAgency/", "deployUrl": "/travelAgency/"

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1, 
  "newProjectRoot": "projects",
  "projects": {
    "travelAgency": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "baseHref" : "/travelAgency/",
            "deployUrl": "/travelAgency/",
            "outputPath": "dist/travelAgency",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "travelAgency:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "travelAgency:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "travelAgency:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "travelAgency:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "travelAgency:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "travelAgency"
}
1
Maybe add what the url of the image look like in prod mod (i.e. the url giving 404) and your apache config - David
Thanks for the suggestion!!! When i inspect the deployed element it in the browser it is url(/assets/hero-image.jpg) just like I have in my source code. Then in the chrome inspector i deleted the first / he found the image immediately. Problem is when I delete this first slash in prod mode he gives me an error. Can`t resolve url("assets/hero-image.jpg"). - Fabian Andiel

1 Answers

1
votes

i faced that before, change the url of your assets, if you are using

src="./assets/images/image.png"

change it to

src="assets/images/logo.png"

and it should work