mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Remove deprecated Angular/React-specific prerendering code, since both are now handled by 'asp-pretender-module' in SpaServices
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Extensions;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.AspNet.Razor.TagHelpers;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
||||
namespace Microsoft.AspNet.AngularServices
|
||||
{
|
||||
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
|
||||
public class AngularPrerenderTagHelper : TagHelper
|
||||
{
|
||||
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
|
||||
|
||||
const string PrerenderModuleAttributeName = "asp-ng2-prerender-module";
|
||||
const string PrerenderExportAttributeName = "asp-ng2-prerender-export";
|
||||
|
||||
[HtmlAttributeName(PrerenderModuleAttributeName)]
|
||||
public string ModuleName { get; set; }
|
||||
|
||||
[HtmlAttributeName(PrerenderExportAttributeName)]
|
||||
public string ExportName { get; set; }
|
||||
|
||||
private IHttpContextAccessor contextAccessor;
|
||||
private INodeServices nodeServices;
|
||||
|
||||
public AngularPrerenderTagHelper(IServiceProvider serviceProvider, IHttpContextAccessor contextAccessor)
|
||||
{
|
||||
this.contextAccessor = contextAccessor;
|
||||
this.nodeServices = (INodeServices)serviceProvider.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
|
||||
|
||||
// Consider removing the following. Having it means you can get away with not putting app.AddNodeServices()
|
||||
// in your startup file, but then again it might be confusing that you don't need to.
|
||||
if (this.nodeServices == null) {
|
||||
var appEnv = (IApplicationEnvironment)serviceProvider.GetService(typeof (IApplicationEnvironment));
|
||||
this.nodeServices = fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
|
||||
HostingModel = NodeHostingModel.Http,
|
||||
ProjectPath = appEnv.ApplicationBasePath
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var result = await AngularRenderer.RenderToString(
|
||||
nodeServices: this.nodeServices,
|
||||
componentModuleName: this.ModuleName,
|
||||
componentExportName: this.ExportName,
|
||||
componentTagName: output.TagName,
|
||||
requestUrl: UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
|
||||
);
|
||||
output.SuppressOutput();
|
||||
output.PostElement.AppendHtml(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
|
||||
namespace Microsoft.AspNet.AngularServices
|
||||
{
|
||||
public static class AngularRenderer
|
||||
{
|
||||
private static StringAsTempFile nodeScript;
|
||||
|
||||
static AngularRenderer() {
|
||||
// Consider populating this lazily
|
||||
var script = EmbeddedResourceReader.Read(typeof (AngularRenderer), "/Content/Node/angular-rendering.js");
|
||||
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
|
||||
}
|
||||
|
||||
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string componentTagName, string requestUrl) {
|
||||
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
|
||||
moduleName = componentModuleName,
|
||||
exportName = componentExportName,
|
||||
tagName = componentTagName,
|
||||
requestUrl = requestUrl
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
var path = require('path');
|
||||
var ngUniversal = require('angular2-universal-preview');
|
||||
var ngUniversalRender = require('angular2-universal-preview/dist/server/src/render');
|
||||
var ngCore = require('angular2/core');
|
||||
var ngRouter = require('angular2/router');
|
||||
|
||||
function getExportOrThrow(moduleInstance, moduleFilename, exportName) {
|
||||
if (!(exportName in moduleInstance)) {
|
||||
throw new Error('The module "' + moduleFilename + '" has no export named "' + exportName + '"');
|
||||
}
|
||||
return moduleInstance[exportName];
|
||||
}
|
||||
|
||||
function findAngularComponent(options) {
|
||||
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
|
||||
var loadedModule = require(resolvedPath);
|
||||
if (options.exportName) {
|
||||
// If exportName is specified explicitly, use it
|
||||
return getExportOrThrow(loadedModule, resolvedPath, options.exportName);
|
||||
} else if (typeof loadedModule === 'function') {
|
||||
// Otherwise, if the module itself is a function, assume that is the component
|
||||
return loadedModule;
|
||||
} else if (typeof loadedModule.default === 'function') {
|
||||
// Otherwise, if the module has a default export which is a function, assume that is the component
|
||||
return loadedModule.default;
|
||||
} else {
|
||||
// Otherwise, guess the export name by converting tag-name to PascalCase
|
||||
var tagNameAsPossibleExport = options.tagName.replace(/(-|^)([a-z])/g, function (m1, m2, char) { return char.toUpperCase(); });
|
||||
return getExportOrThrow(loadedModule, resolvedPath, tagNameAsPossibleExport);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
renderToString: function(callback, options) {
|
||||
try {
|
||||
var component = findAngularComponent(options);
|
||||
var serverBindings = [
|
||||
ngRouter.ROUTER_BINDINGS,
|
||||
ngUniversal.HTTP_PROVIDERS,
|
||||
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
|
||||
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
|
||||
ngUniversal.SERVER_LOCATION_PROVIDERS
|
||||
];
|
||||
|
||||
return ngUniversalRender.renderToString(component, serverBindings).then(
|
||||
function(successValue) { callback(null, successValue); },
|
||||
function(errorValue) { callback(errorValue); }
|
||||
);
|
||||
} catch (synchronousException) {
|
||||
callback(synchronousException);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -29,8 +29,5 @@
|
||||
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha7",
|
||||
"Microsoft.AspNet.SpaServices": "1.0.0-alpha7-1"
|
||||
},
|
||||
"resource": [
|
||||
"Content/**/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var React = require('react');
|
||||
var ReactDOMServer = require('react-dom/server');
|
||||
var createMemoryHistory = require('history/lib/createMemoryHistory');
|
||||
var babelCore = require('babel-core');
|
||||
var babelConfig = {
|
||||
presets: ["es2015", "react"]
|
||||
};
|
||||
|
||||
var origJsLoader = require.extensions['.js'];
|
||||
require.extensions['.js'] = loadViaBabel;
|
||||
require.extensions['.jsx'] = loadViaBabel;
|
||||
|
||||
function findReactComponent(options) {
|
||||
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
|
||||
var loadedModule = require(resolvedPath);
|
||||
if (options.exportName) {
|
||||
// If exportName is specified explicitly, use it
|
||||
if (!(options.exportName in loadedModule)) {
|
||||
throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"');
|
||||
}
|
||||
return loadedModule[options.exportName];
|
||||
} else if (typeof loadedModule === 'function') {
|
||||
// Otherwise, if the module itself is a function, assume that is the component
|
||||
return loadedModule;
|
||||
} else if (typeof loadedModule.default === 'function') {
|
||||
// Otherwise, if the module has a default export which is a function, assume that is the component
|
||||
return loadedModule.default;
|
||||
} else {
|
||||
throw new Error('Cannot find React component, because no export name was specified, and the module "' + resolvedPath + '" has no default exported class.');
|
||||
}
|
||||
}
|
||||
|
||||
function loadViaBabel(module, filename) {
|
||||
// Assume that all the app's own code is ES2015+ (optionally with JSX), but that none of the node_modules are.
|
||||
// The distinction is important because ES2015+ forces strict mode, and it may break ES3/5 if you try to run it in strict
|
||||
// mode when the developer didn't expect that (e.g., current versions of underscore.js can't be loaded in strict mode).
|
||||
var useBabel = filename.indexOf('node_modules') < 0;
|
||||
if (useBabel) {
|
||||
var transformedFile = babelCore.transformFileSync(filename, babelConfig);
|
||||
return module._compile(transformedFile.code, filename);
|
||||
} else {
|
||||
return origJsLoader.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
renderToString: function(callback, options) {
|
||||
try {
|
||||
var component = findReactComponent(options);
|
||||
var history = createMemoryHistory(options.requestUrl);
|
||||
var reactElement = React.createElement(component, { history: history });
|
||||
var html = ReactDOMServer.renderToString(reactElement);
|
||||
callback(null, html);
|
||||
} catch (synchronousException) {
|
||||
callback(synchronousException);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -28,8 +28,5 @@
|
||||
"System.Threading": "4.0.11-beta-*"
|
||||
}
|
||||
}
|
||||
},
|
||||
"resource": [
|
||||
"Content/**/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user