Skip to content

Commit

Permalink
refactor subapp-sever and index-page to remove old SSR content
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jul 16, 2020
1 parent 04bd4c2 commit 6a06efb
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 166 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions packages/subapp-server/lib/fastify-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion packages/subapp-server/lib/register-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
2 changes: 0 additions & 2 deletions packages/subapp-server/lib/routes-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ function setupRouteTemplate({ subAppsByPath, srcDir, routeOptions }) {
});
}

// const useStream = routeOptions.useStream !== false;

const routeHandler = templateRouting.makeRouteTemplateSelector(routeOptions);

return routeHandler;
Expand Down
6 changes: 1 addition & 5 deletions packages/subapp-server/lib/setup-hapi-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
3 changes: 2 additions & 1 deletion packages/xarc-index-page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"xstdout": "^0.1.1"
},
"files": [
"dist"
"dist",
"src"
],
"nyc": {
"extends": [
Expand Down
97 changes: 0 additions & 97 deletions packages/xarc-index-page/src/content.ts

This file was deleted.

89 changes: 35 additions & 54 deletions packages/xarc-index-page/src/token-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down Expand Up @@ -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,
Expand All @@ -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
};
Expand Down
16 changes: 16 additions & 0 deletions packages/xarc-index-page/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ ${errMsg()}
</pre></body></html>`;
}

/**
*
* @param scripts
* @param scriptNonce
*/
const htmlifyScripts = (scripts, scriptNonce) => {
return scripts
.map(x =>
typeof x === "string"
? `<script${scriptNonce || ""}>${x}</script>\n`
: x.map(n => `<script src="${n.src}"></script>`).join("\n")
)
.join("\n");
};

/**
* @param chunkNames
* @param routeData
Expand Down Expand Up @@ -364,6 +379,7 @@ export {
getCriticalCSS,
getStatsPath,
resolvePath,
htmlifyScripts,
htmlifyError,
getDevCssBundle,
getDevJsBundle,
Expand Down

0 comments on commit 6a06efb

Please sign in to comment.