Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit e65eceb

Browse files
Make templates work with nonempty baseUrls (e.g., IIS virtual directories)
1 parent bb0727c commit e65eceb

File tree

31 files changed

+65
-44
lines changed

31 files changed

+65
-44
lines changed

templates/AngularSpa/ClientApp/app/app.module.browser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { AppComponent } from './components/app/app.component';
1010
AppModuleShared
1111
],
1212
providers: [
13-
{ provide: 'ORIGIN_URL', useFactory: getOriginUrl }
13+
{ provide: 'BASE_URL', useFactory: getBaseUrl }
1414
]
1515
})
1616
export class AppModule {
1717
}
1818

19-
export function getOriginUrl() {
20-
return location.origin;
19+
export function getBaseUrl() {
20+
return document.getElementsByTagName('base')[0].href;
2121
}

templates/AngularSpa/ClientApp/app/components/fetchdata/fetchdata.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { Http } from '@angular/http';
88
export class FetchDataComponent {
99
public forecasts: WeatherForecast[];
1010

11-
constructor(http: Http, @Inject('ORIGIN_URL') originUrl: string) {
12-
http.get(originUrl + '/api/SampleData/WeatherForecasts').subscribe(result => {
11+
constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {
12+
http.get(baseUrl + 'api/SampleData/WeatherForecasts').subscribe(result => {
1313
this.forecasts = result.json() as WeatherForecast[];
1414
}, error => console.error(error));
1515
}

templates/AngularSpa/ClientApp/boot.server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'reflect-metadata';
22
import 'zone.js';
33
import 'rxjs/add/operator/first';
4+
import { APP_BASE_HREF } from '@angular/common';
45
import { enableProdMode, ApplicationRef, NgZone, ValueProvider } from '@angular/core';
56
import { platformDynamicServer, PlatformState, INITIAL_CONFIG } from '@angular/platform-server';
67
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
@@ -11,7 +12,8 @@ enableProdMode();
1112
export default createServerRenderer(params => {
1213
const providers = [
1314
{ provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
14-
{ provide: 'ORIGIN_URL', useValue: params.origin }
15+
{ provide: APP_BASE_HREF, useValue: params.baseUrl },
16+
{ provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
1517
];
1618

1719
return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {

templates/AngularSpa/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (env) => {
1313
resolve: { extensions: [ '.js', '.ts' ] },
1414
output: {
1515
filename: '[name].js',
16-
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
16+
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
1717
},
1818
module: {
1919
rules: [

templates/AngularSpa/webpack.config.vendor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = (env) => {
3636
]
3737
},
3838
output: {
39-
publicPath: '/dist/',
39+
publicPath: 'dist/',
4040
filename: '[name].js',
4141
library: '[name]_[hash]'
4242
},

templates/AureliaSpa/ClientApp/app/components/fetchdata/fetchdata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class Fetchdata {
66
public forecasts: WeatherForecast[];
77

88
constructor(http: HttpClient) {
9-
http.fetch('/api/SampleData/WeatherForecasts')
9+
http.fetch('api/SampleData/WeatherForecasts')
1010
.then(result => result.json() as Promise<WeatherForecast[]>)
1111
.then(data => {
1212
this.forecasts = data;

templates/AureliaSpa/ClientApp/boot.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'isomorphic-fetch';
22
import { Aurelia, PLATFORM } from 'aurelia-framework';
3+
import { HttpClient } from 'aurelia-fetch-client';
34
import 'bootstrap/dist/css/bootstrap.css';
45
import 'bootstrap';
56
declare const IS_DEV_BUILD: boolean; // The value is supplied by Webpack during the build
@@ -11,5 +12,10 @@ export function configure(aurelia: Aurelia) {
1112
aurelia.use.developmentLogging();
1213
}
1314

15+
new HttpClient().configure(config => {
16+
const baseUrl = document.getElementsByTagName('base')[0].href;
17+
config.withBaseUrl(baseUrl);
18+
});
19+
1420
aurelia.start().then(() => aurelia.setRoot(PLATFORM.moduleName('app/components/app/app')));
1521
}

templates/AureliaSpa/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (env) => {
1414
},
1515
output: {
1616
path: path.resolve(bundleOutputDir),
17-
publicPath: '/dist/',
17+
publicPath: 'dist/',
1818
filename: '[name].js'
1919
},
2020
module: {

templates/AureliaSpa/webpack.config.vendor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = ({ prod } = {}) => {
3838
},
3939
output: {
4040
path: path.join(__dirname, 'wwwroot', 'dist'),
41-
publicPath: '/dist/',
41+
publicPath: 'dist/',
4242
filename: '[name].js',
4343
library: '[name]_[hash]',
4444
},

templates/KnockoutSpa/ClientApp/boot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import * as ko from 'knockout';
44
import './webpack-component-loader';
55
import AppRootComponent from './components/app-root/app-root';
66
const createHistory = require('history').createBrowserHistory;
7+
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
8+
const basename = baseUrl.substring(0, baseUrl.length - 1); // History component needs no trailing slash
79

810
// Load and register the <app-root> component
911
ko.components.register('app-root', AppRootComponent);
1012

1113
// Tell Knockout to start up an instance of your application
12-
ko.applyBindings({ history: createHistory() });
14+
ko.applyBindings({ history: createHistory({ basename }), basename });
1315

1416
// Basic hot reloading support. Automatically reloads and restarts the Knockout app each time
1517
// you modify source files. This will not preserve any application state other than the URL.

0 commit comments

Comments
 (0)