-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: break rendering pipeline into steps (#2)
- Loading branch information
1 parent
c3a7459
commit 0d854ff
Showing
5 changed files
with
60 additions
and
18 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,15 @@ | ||
const isDevServerEnabled = process.env.WEBPACK_ENABLE_DEV_SERVER === '1'; | ||
|
||
export function getAssets() { | ||
let scripts; | ||
|
||
if (isDevServerEnabled) { | ||
scripts = ['/assets/vendor.js', '/assets/app.js']; | ||
} else { | ||
scripts = ['/assets/app.js']; | ||
} | ||
|
||
return { | ||
scripts, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,25 +1,38 @@ | ||
function getAssets(isDevelopment) { | ||
let scripts; | ||
import { isPlainObject } from 'lodash'; | ||
|
||
if (isDevelopment) { | ||
scripts = ['/assets/vendor.js', '/assets/app.js']; | ||
} else { | ||
scripts = ['/assets/app.js']; | ||
import { getRenderingSteps } from './steps'; | ||
|
||
function extend(locals, val) { | ||
// FIXME: ensure `val` is non-object or plain object | ||
// (not an array or smth) statically to simplify the condition | ||
if (!isPlainObject(val)) { | ||
return locals; | ||
} | ||
|
||
return { | ||
scripts, | ||
}; | ||
return Object.assign({}, locals, val); | ||
} | ||
|
||
const isDevServerEnabled = process.env.WEBPACK_ENABLE_DEV_SERVER === '1'; | ||
function iterateRender(locals, steps, bag) { | ||
if (!steps.length) { | ||
return locals; | ||
} | ||
|
||
const res = steps[0](locals, bag); | ||
|
||
export function renderMiddleware(req, res, next) { | ||
if (res.headersSent) { | ||
return next(); | ||
if (res && typeof res.then === 'function') { | ||
return res.then(val => { | ||
return iterateRender(extend(locals, val), steps.slice(1), bag); | ||
}); | ||
} | ||
|
||
return res.status(200).render('index', { | ||
assets: getAssets(isDevServerEnabled), | ||
}); | ||
return iterateRender(extend(locals, res), steps.slice(1), bag); | ||
} | ||
|
||
export function createRenderMiddleware() { | ||
const steps = getRenderingSteps(); | ||
|
||
return (req, res, next) => { | ||
const initialLocals = res.locals; | ||
return iterateRender(initialLocals, steps, { req, res, next }); | ||
}; | ||
} |
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,3 @@ | ||
export function getRenderingSteps() { | ||
return [require('./no-ssr').render]; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/web-ssr-server/src/services/render/steps/no-ssr.js
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,11 @@ | ||
import { getAssets } from '../assets'; | ||
|
||
export function render(locals, bag) { | ||
if (bag.res.headersSent) { | ||
return; | ||
} | ||
|
||
return bag.res.status(200).render('index', { | ||
assets: getAssets(), | ||
}); | ||
} |