diff --git a/package.json b/package.json index b39c80441..33ef1e5d2 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "devDependencies": { "bluebird": "^3.5.0", - "fynpo": "^0.1.26", + "fynpo": "^0.1.27", "lerna": "^2.0.0", "lodash": "^4.17.10", "semver": "^5.3.0", diff --git a/packages/subapp-server/lib/fastify-plugin.js b/packages/subapp-server/lib/fastify-plugin.js index 587b7d216..48d5c1e99 100644 --- a/packages/subapp-server/lib/fastify-plugin.js +++ b/packages/subapp-server/lib/fastify-plugin.js @@ -33,11 +33,7 @@ function makeRouteHandler({ path, routeRenderer, routeOptions }) { return async (request, reply) => { try { const context = await routeRenderer({ - content: { - html: "", - status: HttpStatusCodes.OK, - useStream - }, + useStream, mode: "", request }); diff --git a/packages/subapp-server/lib/register-routes.js b/packages/subapp-server/lib/register-routes.js index f0d91f6e6..faff7392a 100644 --- a/packages/subapp-server/lib/register-routes.js +++ b/packages/subapp-server/lib/register-routes.js @@ -43,7 +43,7 @@ module.exports = function registerRoutes({ routes, topOpts, server }) { const handler = async (request, h) => { try { const context = await routeHandler({ - content: { html: "", status: HttpStatusCodes.OK, useStream }, + useStream, mode: "", request }); diff --git a/packages/subapp-server/lib/routes-from-file.js b/packages/subapp-server/lib/routes-from-file.js index 4c59c0947..51141555c 100644 --- a/packages/subapp-server/lib/routes-from-file.js +++ b/packages/subapp-server/lib/routes-from-file.js @@ -37,8 +37,6 @@ function setupRouteTemplate({ subAppsByPath, srcDir, routeOptions }) { }); } - // const useStream = routeOptions.useStream !== false; - const routeHandler = templateRouting.makeRouteTemplateSelector(routeOptions); return routeHandler; diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 0ca7eeda4..3623bb988 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -73,11 +73,7 @@ async function registerRoutesFromFile({ server, srcDir, routes, topOpts }) { const handler = async (request, h) => { try { const context = await routeRenderer({ - content: { - html: "", - status: 200, - useStream - }, + useStream, mode: "", request }); diff --git a/packages/xarc-index-page/package.json b/packages/xarc-index-page/package.json index ab7185fed..1c2b7db06 100644 --- a/packages/xarc-index-page/package.json +++ b/packages/xarc-index-page/package.json @@ -56,7 +56,8 @@ "xstdout": "^0.1.1" }, "files": [ - "dist" + "dist", + "src" ], "nyc": { "extends": [ diff --git a/packages/xarc-index-page/src/content.ts b/packages/xarc-index-page/src/content.ts deleted file mode 100644 index 369b736ff..000000000 --- a/packages/xarc-index-page/src/content.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as Fs from "fs"; -import * as Path from "path"; - -const HTTP_ERROR_500 = 500; -const HTTP_OK = 200; -/** - * @param renderSs - * @param options - * @param context - */ -function getContent(renderSs, options, context) { - let userContent = options.content; - - // prepare user content for container of SSR output - - if (typeof userContent === "string") { - return Promise.resolve({ status: 200, html: userContent }); - } - - if (typeof userContent !== "function") return Promise.resolve(userContent); - - if (!renderSs) return Promise.resolve({ status: 200, html: "" }); - - // invoke user content as a function, which could return any content - // as static html or generated from react's renderToString - userContent = userContent(options.request, options, context); - - if (userContent.catch) { - // user function needs to generate the content async, so wait for it. - return userContent.catch(err => { - if (!err.status) err.status = HTTP_ERROR_500; - throw err; - }); - } - - return Promise.resolve(userContent); -} - -/** - * @param result - * @param context - */ -function transformOutput(result, context) { - const content = context.user.content; - if (content && content.status !== HTTP_OK) { - return { - verbatim: content.verbatim, - status: content.status, - path: content.path, - store: content.store, - html: result - }; - } - - return result; -} - -const htmlifyScripts = (scripts, scriptNonce) => { - return scripts - .map(x => - typeof x === "string" - ? `${x}\n` - : x.map(n => ``).join("\n") - ) - .join("\n"); -}; - -const loadElectrodeDllAssets = routeOptions => { - const tag = process.env.NODE_ENV === "production" ? "" : ".dev"; - try { - const file = Path.resolve( - routeOptions.electrodeDllAssetsPath || `dist/electrode-dll-assets${tag}.json` - ); - return JSON.parse(Fs.readFileSync(file).toString()); - } catch (err) { - return {}; - } -}; - -const makeElectrodeDllScripts = (dllAssets, nonce = "") => { - const scripts = []; - for (const modName in dllAssets) { - const cdnMapping = dllAssets[modName].cdnMapping; - for (const bundle in cdnMapping) { - scripts.push({ src: cdnMapping[bundle] }); - } - } - - return htmlifyScripts([scripts], nonce); -}; -export { - getContent, - transformOutput, - htmlifyScripts, - loadElectrodeDllAssets, - makeElectrodeDllScripts -}; diff --git a/packages/xarc-index-page/src/token-handlers.ts b/packages/xarc-index-page/src/token-handlers.ts index ff0bd7ad9..6a31c2cf7 100644 --- a/packages/xarc-index-page/src/token-handlers.ts +++ b/packages/xarc-index-page/src/token-handlers.ts @@ -7,23 +7,18 @@ import { getDevJsBundle, getProdBundles, processRenderSsMode, - getCspNonce, - isReadableStream + getCspNonce } from "./utils"; -import { getContent, transformOutput } from "./content"; - import prefetchBundles from "./handlers/prefetch-bundles"; export const tokens = { INITIALIZE: "INITIALIZE", - SSR_CONTENT: "SSR_CONTENT", PREFETCH_BUNDLES: "PREFETCH_BUNDLES", META_TAGS: "META_TAGS", CRITICAL_CSS: "CRITICAL_CSS", HEAD_INITIALIZE: "HEAD_INITIALIZE", HEAD_CLOSED: "HEAD_CLOSED", - AFTER_SSR_CONTENT: "AFTER_SSR_CONTENT", BODY_CLOSED: "BODY_CLOSED", HTML_CLOSED: "HTML_CLOSED" }; @@ -66,56 +61,43 @@ export default function setup(handlerContext /*, asyncTemplate*/) { const mode = options.mode; const renderSs = processRenderSsMode(request, RENDER_SS, mode); - return getContent(renderSs, options, context).then(content => { - if (content.render === false || content.html === undefined) { - return context.voidStop(content); - } - - const chunkNames = chunkSelector(request); - - const devCSSBundle = getDevCssBundle(chunkNames, routeData); - const devJSBundle = getDevJsBundle(chunkNames, routeData); - - const { jsChunk, cssChunk } = getProdBundles(chunkNames, routeData); - const { scriptNonce, styleNonce } = getCspNonce(request, routeOptions.cspNonceValue); - - const renderJs = RENDER_JS && mode !== "nojs"; - - context.user = { - request: options.request, - response: { - headers: {} - }, - routeOptions, - routeData, - content, - mode, - renderJs, - renderSs, - scriptNonce, - styleNonce, - chunkNames, - devCSSBundle, - devJSBundle, - jsChunk, - cssChunk - }; - - if (content.useStream || isReadableStream(content.html)) { - context.setMunchyOutput(); - } - - context.setOutputTransform(transformOutput); - - return context; - }); + const chunkNames = chunkSelector(request); + + const devCSSBundle = getDevCssBundle(chunkNames, routeData); + const devJSBundle = getDevJsBundle(chunkNames, routeData); + + const { jsChunk, cssChunk } = getProdBundles(chunkNames, routeData); + const { scriptNonce, styleNonce } = getCspNonce(request, routeOptions.cspNonceValue); + + const renderJs = RENDER_JS && mode !== "nojs"; + + context.user = { + request: options.request, + response: { + headers: {} + }, + routeOptions, + routeData, + mode, + renderJs, + renderSs, + scriptNonce, + styleNonce, + chunkNames, + devCSSBundle, + devJSBundle, + jsChunk, + cssChunk + }; + + if (context.options.useStream) { + context.setMunchyOutput(); + } + + return context; }; const tokenHandlers = { - [tokens.SSR_CONTENT]: context => { - return (context.user.content && context.user.content.html) || ""; - }, - [tokens.PREFETCH_BUNDLES]: prefetchBundles, [tokens.META_TAGS]: iconStats, @@ -127,7 +109,6 @@ export default function setup(handlerContext /*, asyncTemplate*/) { [tokens.INITIALIZE]: Initialize, [tokens.HEAD_INITIALIZE]: null, [tokens.HEAD_CLOSED]: null, - [tokens.AFTER_SSR_CONTENT]: null, [tokens.BODY_CLOSED]: null, [tokens.HTML_CLOSED]: null }; diff --git a/packages/xarc-index-page/src/utils.ts b/packages/xarc-index-page/src/utils.ts index 82e16815f..bd2fecccf 100644 --- a/packages/xarc-index-page/src/utils.ts +++ b/packages/xarc-index-page/src/utils.ts @@ -142,6 +142,21 @@ ${errMsg()} `; } +/** + * + * @param scripts + * @param scriptNonce + */ +const htmlifyScripts = (scripts, scriptNonce) => { + return scripts + .map(x => + typeof x === "string" + ? `${x}\n` + : x.map(n => ``).join("\n") + ) + .join("\n"); +}; + /** * @param chunkNames * @param routeData @@ -364,6 +379,7 @@ export { getCriticalCSS, getStatsPath, resolvePath, + htmlifyScripts, htmlifyError, getDevCssBundle, getDevJsBundle,