Angular2 base url and relative paths on gh pages

I'm trying to deploy my project site to gh pages, this is an angular2 application with webpack, I set the base url in my repo name and everything loads fine except for the relative paths in the templates, this is what I mean

import {Component} from '@angular/core'; @Component({ selector: 'header', template: ` <img class="fb-logo" [src]="fbLogo"/> <img class="ang-logo" [src]="angularLogo"/> ` }) export class Header{ angularLogo = '../../assets/img/angular-logo.png'; fbLogo = '../../assets/img/share/facebook.svg'; } 

This works well on the local dev, but when I click it on the gh-pages branch, it gives 404 because it tries to get it from the root server http://myAcc.imtqy.com/assets/img/fbLogo.svg instead of http://myAcc.imtqy.com/myRepo/assets/img/fbLogo.svg

I could not figure out how to fix this, so I tried using it as inline-svg using require

 angularLogo = require('../../assets/img/angular-logo.png'); fbLogo = require('../../assets/img/share/facebook.svg'); 

it works fine on local (with xss warnings), but when I deploy it to gh-pages , it refuses to run https.

How can I fix this problem?

+5
source share
2 answers

The problem is that your dev and your gh pages have different root levels, your dev root URL should be something like this: http://localhost:port , while your gh page root is http://myAcc.imtqy.com , why your relative icons do not work,

I assume you have different webpack configurations, change your production configuration to something like this:

 output: { path: 'something', filename: 'something.bundle.js', publicPath: '/myRepo/' // this will add /myRepo/ to all your assets } 

Here is a brief description of publicPath:

output.publicPath

publicPath indicates the common URL of the output files when linking in the browser. For loaders that insert <script> or <link> tags or <link> objects such as images, publicPath used as href or url() in the file if it differs from the location on the disk (as indicated by path ). This can be useful when you want some or all of the output files in another domain or on a CDN. Webpack Dev Server also uses this to determine the path where a message is expected from the output files. As with path , you can use the [hash] substitution for a better cache profile.

Read more about publicPath here

Update:

output.publicPath only works with assets that have already been declared, since you specified your img source dynamically, but this will not work. You can use the file loader for this, just change your loader to this:

 { test: /\.(jpg|png|gif)$/, loader: 'file?name=/myRepo/[name].[ext]' }, 

So whenever you need jpg|png|gif for your code, it adds the line /myRepo/ .

Another solution is to create a custom pipe , since you are using angular2 -webpack-starter, your building process will export your environment to varaible ENV so that you can use this for your custom channel, something like this

 import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'pathResolver'}) export class PathResolverPipe implements PipeTransform { transform(path: string): string { return ENV === 'production' ? path.replace(/assets/i, 'myRepo/assets') : path; } } 

And use it on your component as follows:

 import {Component} from '@angular/core'; import { PathResolverPipe } from 'path/to/pipe'; @Component({ selector: 'header', pipes: [PathResolverPipe], template: ` <img class="fb-logo" [src]="fbLogo | pathResolver"/> <img class="ang-logo" [src]="angularLogo | pathResolver"/> ` }) export class Header{ angularLogo = '../../assets/img/angular-logo.png'; fbLogo = '../../assets/img/share/facebook.svg'; } 
+3
source

For future visitors, the deployment of github pages has been removed from ng-cli and will be available as different npm, https://github.com/angular/angular-cli/pull/4385

But it looks like deploying an angular app for github pages has now become easier with https://github.com/angular-buch/angular-cli-ghpages

Using:

 ng build --prod --base-href "https://USERNAME.imtqy.com/REPOSITORY/" angular-cli-ghpages [OPTIONS] 

there is also a shorter ngh command

 ng build --prod --base-href "https://USERNAME.imtqy.com/REPOSITORY/" ngh [OPTIONS] 
+4
source

All Articles