-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor functions out of handler file
remove slash
- Loading branch information
Showing
7 changed files
with
308 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use strict"; | ||
const HttpStatusCodes = require("http-status-codes"); | ||
|
||
const HTTP_ERROR_500 = 500; | ||
|
||
async function getContent(renderSs, options, context) { | ||
let userContent = options.content; | ||
|
||
// prepare user content for container of SSR output | ||
|
||
if (typeof userContent === "string") { | ||
return { status: 200, html: userContent }; | ||
} | ||
|
||
if (typeof userContent !== "function") return userContent; | ||
|
||
if (!renderSs) return { status: 200, html: "<!-- noss mode -->" }; | ||
|
||
// 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.then) { | ||
try { | ||
// user function needs to generate the content async, so wait for it. | ||
return await userContent; | ||
} catch (err) { | ||
if (!err.status) err.status = HTTP_ERROR_500; | ||
throw err; | ||
} | ||
} | ||
|
||
return userContent; | ||
} | ||
|
||
function transformOutput(result, context) { | ||
const content = context.user.content; | ||
if (content && content.status !== HttpStatusCodes.OK) { | ||
return { | ||
status: content.status, | ||
path: content.path, | ||
store: content.store, | ||
html: result | ||
}; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
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"); | ||
}; | ||
|
||
module.exports = { | ||
getContent, | ||
transformOutput, | ||
htmlifyScripts | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.