3

The final app I'm building will live at /subdirectory on a server.

Setting html5mode to true for angularjs so I lose the # sign works fine. But I also need to set

<base href="/subdirectory/"></base>

During development, I can't have the 'base' element set as when I run grunt serve the server cannot find most of the files.

What is the best way to develop and then maybe set the 'base' element when I do a grunt build ?

Thanks.

magician11
  • 4,234
  • 6
  • 24
  • 39

3 Answers3

6

I used the grunt task usemin:

index.html

<!-- build:base /subdirectory/ -->
<base href="">
<!-- endbuild -->

Gruntfile.js

usemin: {
    html: ['<%= yeoman.dist %>/{,*/}*.html'],
    css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
    options: {
        dirs: ['<%= yeoman.dist %>'],
        blockReplacements: {
            base: function (block) {
                return ['<base href="', block.dest, '">'].join('');
            }
        }
    }
}

Basically I defined a custom build block named base and this way it's using "" during development (or whatever you set there) and for production the value from the build block config.

roberkules
  • 6,557
  • 2
  • 44
  • 52
0

Please read here. I avoid using base tag. To solve the problem you have (which I also had) please do two things:

  1. In index.html specify all js/css path as relation to index.html path, so it works in both prod/dev

  2. write an http interceptor which while loading html/api calls append the base to the url

    myapp.factory('MyHttpInterceptor',[ "$log", function ($log) {
    
        var requestInterceptor = function (config) {
            var prefix = document.location.pathname.substring(0, document.location.pathname.length - 1);
            if (config.url.indexOf(".html") !== -1) {
                config.url = prefix + config.url;
            }
            if (config.url.indexOf("/api/") !== -1) {
                config.url = prefix + config.url;
            }
            return config || $q.when(config);
        };
    
        return {
            request: requestInterceptor,
        };
    }
      ]);
    
Community
  • 1
  • 1
harishr
  • 17,807
  • 9
  • 78
  • 125
-1

Use in inside head tag <base href="/your MainDomain Name">

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <base href="/your MainDomain Name">
</head>
Harutyun Abgaryan
  • 2,013
  • 1
  • 12
  • 15
  • Yes thanks I understand where to put the 'base' tag. I just cannot have it there during development as running grunt serve then fails to load the app. – magician11 Oct 25 '14 at 08:01
  • @magician11 Can you please help me in resolving this? I am unable to add the following in my index.html at dist folder : after tag – Smitha Jul 29 '15 at 06:00