Skip to content

Commit 84cf29d

Browse files
soumak77longgt
authored andcommitted
account for base href in deep linker
use APP_BASE_HREF when generating urls from within deep linker resolves ionic-team#10076 resolves ionic-team#10565
1 parent bd9d73c commit 84cf29d

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export class IonicModule {
446446
{ provide: ModuleLoader, useFactory: provideModuleLoader, deps: [NgModuleLoader, Injector]},
447447
{ provide: LocationStrategy, useFactory: provideLocationStrategy, deps: [ PlatformLocation, [new Inject(APP_BASE_HREF), new Optional()], Config ] },
448448
{ provide: UrlSerializer, useFactory: setupUrlSerializer, deps: [ App, DeepLinkConfigToken ] },
449-
{ provide: DeepLinker, useFactory: setupDeepLinker, deps: [ App, UrlSerializer, Location, ModuleLoader, ComponentFactoryResolver ] },
449+
{ provide: DeepLinker, useFactory: setupDeepLinker, deps: [ App, UrlSerializer, Location, ModuleLoader, ComponentFactoryResolver, [new Inject(APP_BASE_HREF), new Optional()] ] },
450450
]
451451
};
452452
}

src/navigation/deep-linker.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ComponentFactory, ComponentFactoryResolver } from '@angular/core';
22
import { Location } from '@angular/common';
3-
43
import { App } from '../components/app/app';
54
import { DIRECTION_BACK, NavLink, NavSegment, TransitionDoneFn, convertToViews, isNav, isTab, isTabs } from './nav-util';
65
import { ModuleLoader } from '../util/module-loader';
@@ -27,23 +26,24 @@ export class DeepLinker {
2726
public _serializer: UrlSerializer,
2827
public _location: Location,
2928
public _moduleLoader: ModuleLoader,
30-
public _baseCfr: ComponentFactoryResolver
29+
public _baseCfr: ComponentFactoryResolver,
30+
public _baseHref: string
3131
) {}
3232

3333
/**
3434
* @internal
3535
*/
3636
init() {
3737
// scenario 1: Initial load of all navs from the initial browser URL
38-
const browserUrl = normalizeUrl(this._location.path());
38+
const browserUrl = normalizeUrl(this._location.path(), this._baseHref);
3939
console.debug(`DeepLinker, init load: ${browserUrl}`);
4040

4141
// remember this URL in our internal history stack
4242
this._historyPush(browserUrl);
4343

4444
// listen for browser URL changes
4545
this._location.subscribe((locationChg: { url: string }) => {
46-
this._urlChange(normalizeUrl(locationChg.url));
46+
this._urlChange(normalizeUrl(locationChg.url, this._baseHref));
4747
});
4848
}
4949

@@ -120,7 +120,7 @@ export class DeepLinker {
120120

121121
getCurrentSegments(browserUrl?: string) {
122122
if (!browserUrl) {
123-
browserUrl = normalizeUrl(this._location.path());
123+
browserUrl = normalizeUrl(this._location.path(), this._baseHref);
124124
}
125125
return this._serializer.parse(browserUrl);
126126
}
@@ -290,7 +290,7 @@ export class DeepLinker {
290290
* @internal
291291
*/
292292
getSegmentByNavIdOrName(navId: string, name: string): NavSegment {
293-
const browserUrl = normalizeUrl(this._location.path());
293+
const browserUrl = normalizeUrl(this._location.path(), this._baseHref);
294294
const segments = this._serializer.parse(browserUrl);
295295
for (const segment of segments) {
296296
if (segment.navId === navId || segment.navId === name) {
@@ -433,19 +433,22 @@ export class DeepLinker {
433433
}
434434

435435

436-
export function setupDeepLinker(app: App, serializer: UrlSerializer, location: Location, moduleLoader: ModuleLoader, cfr: ComponentFactoryResolver) {
437-
const deepLinker = new DeepLinker(app, serializer, location, moduleLoader, cfr);
436+
export function setupDeepLinker(app: App, serializer: UrlSerializer, location: Location, moduleLoader: ModuleLoader, cfr: ComponentFactoryResolver, baseHref: string) {
437+
const deepLinker = new DeepLinker(app, serializer, location, moduleLoader, cfr, baseHref);
438438
deepLinker.init();
439439
return deepLinker;
440440
}
441441

442442

443-
export function normalizeUrl(browserUrl: string): string {
443+
export function normalizeUrl(browserUrl: string, baseHref: string = '/'): string {
444444
browserUrl = browserUrl.trim();
445445
if (browserUrl.charAt(0) !== '/') {
446446
// ensure first char is a /
447447
browserUrl = '/' + browserUrl;
448448
}
449+
if (!browserUrl.startsWith(baseHref)) {
450+
browserUrl = baseHref + browserUrl;
451+
}
449452
if (browserUrl.length > 1 && browserUrl.charAt(browserUrl.length - 1) === '/') {
450453
// ensure last char is not a /
451454
browserUrl = browserUrl.substr(0, browserUrl.length - 1);

src/util/mock-providers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export function mockDeepLinker(linkConfig: DeepLinkConfig = null, app?: App) {
394394
let serializer = new UrlSerializer(app, linkConfig);
395395
let location = mockLocation();
396396

397-
return new DeepLinker(app || mockApp(), serializer, location, null, null);
397+
return new DeepLinker(app || mockApp(), serializer, location, null, null, '/');
398398
}
399399

400400
export function mockNavController(): NavControllerBase {
@@ -451,7 +451,7 @@ export function mockOverlayPortal(app: App, config: Config, plt: MockPlatform):
451451
let gestureCtrl = new GestureController(app);
452452
let serializer = new UrlSerializer(app, null);
453453
let location = mockLocation();
454-
let deepLinker = new DeepLinker(app, serializer, location, null, null);
454+
let deepLinker = new DeepLinker(app, serializer, location, null, null, '/');
455455

456456
return new OverlayPortal(
457457
app,

src/util/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export function debounce(fn: Function, wait: number, immediate: boolean = false)
5353
* @hidden
5454
* Rewrites an absolute URL so it works across file and http based engines
5555
*/
56-
export function normalizeURL(url: string): string {
56+
export function normalizeURL(url: string, baseHref: string = '/'): string {
5757
const ionic = (<any>window)['Ionic'];
5858
if (ionic && ionic.normalizeURL) {
59-
return ionic.normalizeURL(url);
59+
return ionic.normalizeURL(url, baseHref);
6060
}
6161
return url;
6262
}

0 commit comments

Comments
 (0)