Search code examples
angularangular2-routingsystemjs

Angular2 Deep Linking Issue


Deep linking works when I request a url like this my-domain.com/my-appname/aRoute, but when i go to my-domain.com/my-appname/aRoute/ system.js tries to look for my main.js file under my-domain.com/my-appname/aRoute/app/main.js when it resides here: my-domain.com/my-appname/app/main.js so a 404 is produced and angular never loads.

Here is how i attempt to import my app in system.js

<script>
    System.import('app')
        .catch(function (err) { console.error(err); });

</script>

I'm dynamically setting my base root element like this:

<script>
        // this function is needed to dynamically determine the root url
        // the angular2 router uses this to support deep linkinkg.
        function getDocumentBase() {
            let loc = document.location.href;
            let locLower = loc.toLowerCase();

            let ind = locLower.indexOf('myappnamehere/')

            let newLoc = loc.substring(0, (ind + 14));
            return newLoc;
        }
        document.write('<base href="' + getDocumentBase() + '" />');
    </script>

but it seems that system.js doesn't use the base element as the root of the app when trying to load the app module.


Solution

  • Ok turns out that me knowing nothing of systemjs was the issue. I had to change my systemjs.config.js file as follows:

    (function (global) {
        System.config({
    
            baseURL: getDocumentBase(),
            paths: {
                // paths serve as alias
                'npm:': 'Scripts/npmlibs/'
            },
            // map tells the System loader where to look for things
            map: {
                // our app is within the app folder
                app: 'app',
                // angular bundles
                '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
                '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
                '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
                '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
                '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
                '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
                '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
                '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
                // other libraries
                'rxjs': 'npm:rxjs',
                'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
                'lodash': 'npm:lodash/lodash.js'
            },
            // packages tells the System loader how to load when no filename and/or no extension
            packages: {
                app: {
                    main: './main.js',
                    defaultExtension: 'js'
                },
                rxjs: {
                    defaultExtension: 'js'
                },
                'angular2-in-memory-web-api': {
                    main: './index.js',
                    defaultExtension: 'js'
                },
                'lodash': { defaultExtension: 'js' }
            }
        });
    })(this);
    

    The key part was setting baseURL, it invokes this function in my index.html file:

    <script>
        // this function is needed to dynamically determine the root url
        // the angular2 router uses this to support deep linkinkg.
        function getDocumentBase() {
            let loc = document.location.href;
            let locLower = loc.toLowerCase();
    
            let ind = locLower.indexOf('my-app-name-here/')
    
            // trim off anything after appname as that is truely the root of the app (its a deep link to a route)
            let newLoc = loc.substring(0, (ind + 17));
            return newLoc;
        }
    </script>
    

    and then i use that same function to render the base element like this:

    <script>
        document.write('<base href="' + getDocumentBase() + '" />');
    </script>