Compare commits

...

3 Commits

Author SHA1 Message Date
SteveSandersonMS
bd42deef7e Publish templates v0.6.2 2016-11-29 19:12:00 +00:00
SteveSandersonMS
939e0aeee0 Make 'fetchdata' components in templates compatible with latest TypeScript compiler 2016-11-29 19:10:48 +00:00
SteveSandersonMS
28b8136fa6 Detect legacy aspnet-prerendering mode earlier to fix #470 2016-11-29 17:23:14 +00:00
8 changed files with 58 additions and 18 deletions

View File

@@ -71,7 +71,8 @@
// a certain flag is attached to the function instance.
function renderToStringImpl(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds) {
try {
var renderToStringFunc = findRenderToStringFunc(applicationBasePath, bootModule);
var forceLegacy = isLegacyAspNetPrerendering();
var renderToStringFunc = !forceLegacy && findRenderToStringFunc(applicationBasePath, bootModule);
var isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer'];
if (isNotLegacyMode) {
// Current (non-legacy) mode - we invoke the exported function directly (instead of going through aspnet-prerendering)
@@ -81,9 +82,9 @@
}
else {
// Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
renderToStringFunc = __webpack_require__(3).renderToString;
if (renderToStringFunc) {
renderToStringFunc(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
var aspNetPrerenderingV1RenderToString = __webpack_require__(3).renderToString;
if (aspNetPrerenderingV1RenderToString) {
aspNetPrerenderingV1RenderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
}
else {
callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
@@ -141,6 +142,24 @@
}
return renderToStringFunc;
}
function isLegacyAspNetPrerendering() {
var version = getAspNetPrerenderingPackageVersion();
return version && /^1\./.test(version);
}
function getAspNetPrerenderingPackageVersion() {
try {
var packageEntryPoint = require.resolve('aspnet-prerendering');
var packageDir = path.dirname(packageEntryPoint);
var packageJsonPath = path.join(packageDir, 'package.json');
var packageJson = require(packageJsonPath);
return packageJson.version.toString();
}
catch (ex) {
// Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
// which will be the case in production based on latest templates).
return null;
}
}
/***/ },

View File

@@ -1,6 +1,7 @@
/// <reference path="../npm/aspnet-prerendering/src/PrerenderingInterfaces.d.ts" />
import * as url from 'url';
import * as path from 'path';
import * as fs from 'fs';
declare var __non_webpack_require__;
// Separate declaration and export just to add type checking on function signature
@@ -22,7 +23,8 @@ export const renderToString: RenderToStringFunc = renderToStringImpl;
// a certain flag is attached to the function instance.
function renderToStringImpl(callback: RenderToStringCallback, applicationBasePath: string, bootModule: BootModuleInfo, absoluteRequestUrl: string, requestPathAndQuery: string, customDataParameter: any, overrideTimeoutMilliseconds: number) {
try {
let renderToStringFunc = findRenderToStringFunc(applicationBasePath, bootModule);
const forceLegacy = isLegacyAspNetPrerendering();
const renderToStringFunc = !forceLegacy && findRenderToStringFunc(applicationBasePath, bootModule);
const isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer'];
if (isNotLegacyMode) {
@@ -32,9 +34,9 @@ function renderToStringImpl(callback: RenderToStringCallback, applicationBasePat
renderToStringFunc.apply(null, arguments);
} else {
// Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
renderToStringFunc = require('aspnet-prerendering').renderToString;
if (renderToStringFunc) {
renderToStringFunc(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
const aspNetPrerenderingV1RenderToString = require('aspnet-prerendering').renderToString;
if (aspNetPrerenderingV1RenderToString) {
aspNetPrerenderingV1RenderToString(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds);
} else {
callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
+ 'Either update your boot module code, or revert to aspnet-prerendering version 1.x');
@@ -92,3 +94,22 @@ function findRenderToStringFunc(applicationBasePath: string, bootModule: BootMod
return renderToStringFunc;
}
function isLegacyAspNetPrerendering() {
const version = getAspNetPrerenderingPackageVersion();
return version && /^1\./.test(version);
}
function getAspNetPrerenderingPackageVersion() {
try {
const packageEntryPoint = __non_webpack_require__.resolve('aspnet-prerendering');
const packageDir = path.dirname(packageEntryPoint);
const packageJsonPath = path.join(packageDir, 'package.json');
const packageJson = __non_webpack_require__(packageJsonPath);
return packageJson.version.toString();
} catch(ex) {
// Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
// which will be the case in production based on latest templates).
return null;
}
}

View File

@@ -10,7 +10,7 @@ export class FetchDataComponent {
constructor(http: Http) {
http.get('/api/SampleData/WeatherForecasts').subscribe(result => {
this.forecasts = result.json();
this.forecasts = result.json() as WeatherForecast[];
});
}
}

View File

@@ -11,9 +11,9 @@ export class Fetchdata {
constructor(http: HttpClient) {
http.fetch('/api/SampleData/WeatherForecasts')
.then(result => result.json())
.then(result => result.json() as Promise<WeatherForecast[]>)
.then(data => {
this.forecasts = data as any;
this.forecasts = data;
});
}
}

View File

@@ -13,8 +13,8 @@ class FetchDataViewModel {
constructor() {
fetch('/api/SampleData/WeatherForecasts')
.then(response => response.json())
.then((data: WeatherForecast[]) => {
.then(response => response.json() as Promise<WeatherForecast[]>)
.then(data => {
this.forecasts(data);
});
}

View File

@@ -46,8 +46,8 @@ export const actionCreators = {
// Only load data if it's something we don't already have (and are not already loading)
if (startDateIndex !== getState().weatherForecasts.startDateIndex) {
let fetchTask = fetch(`/api/SampleData/WeatherForecasts?startDateIndex=${ startDateIndex }`)
.then(response => response.json())
.then((data: WeatherForecast[]) => {
.then(response => response.json() as Promise<WeatherForecast[]>)
.then(data => {
dispatch({ type: 'RECEIVE_WEATHER_FORECASTS', startDateIndex: startDateIndex, forecasts: data });
});

View File

@@ -12,8 +12,8 @@ export class FetchData extends React.Component<any, FetchDataExampleState> {
this.state = { forecasts: [], loading: true };
fetch('/api/SampleData/WeatherForecasts')
.then(response => response.json())
.then((data: WeatherForecast[]) => {
.then(response => response.json() as Promise<WeatherForecast[]>)
.then(data => {
this.setState({ forecasts: data, loading: false });
});
}

View File

@@ -1,6 +1,6 @@
{
"name": "generator-aspnetcore-spa",
"version": "0.6.1",
"version": "0.6.2",
"description": "Single-Page App templates for ASP.NET Core",
"author": "Microsoft",
"license": "Apache-2.0",