Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork97
[experiment] Performance improvements for SSR#136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
developit wants to merge3 commits intomainChoose a base branch fromssr-performance-2
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletionssrc/ssr.js
This file contains hidden or 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,155 @@ | ||
| import { encodeEntities, styleObjToCss, assign } from './util'; | ||
| import { options } from 'preact'; | ||
| const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; | ||
| const UNSAFE_NAME = /[\s\n\\/='"\0<>]/; | ||
| /** | ||
| * Render Preact JSX + Components to an HTML string. | ||
| * @param {import('preact').VNode|string|number|null|boolean} vnode any renderable value | ||
| * @param {object} context context object, forks throughout the tree | ||
| * @param {any} selectValue the current select value, passed down through the tree to set <options selected> | ||
| */ | ||
| export default function renderToString(vnode, context, selectValue) { | ||
| if (vnode == null || vnode === false || vnode === true) { | ||
| return ''; | ||
| } | ||
| if (typeof vnode !== 'object') { | ||
| return encodeEntities(vnode); | ||
| } | ||
| context = context || {}; | ||
| if (Array.isArray(vnode)) { | ||
| let s = ''; | ||
| for (let i=0; i<vnode.length; i++) { | ||
| s += renderToString(vnode[i], context, selectValue); | ||
| } | ||
| return s; | ||
| } | ||
| let nodeName = vnode.type, | ||
| props = vnode.props; | ||
| // components | ||
| if (typeof nodeName === 'function') { | ||
| let rendered; | ||
| let c = vnode.__c = { __v: vnode, context, props: vnode.props, __h: [] }; | ||
| // options.render | ||
| if (options.__r) options.__r(vnode); | ||
| // Necessary for createContext api. Setting this property will pass | ||
| // the context value as `this.context` just for this component. | ||
| let cxType = nodeName.contextType; | ||
| let provider = cxType && context[cxType.__c]; | ||
| let cctx = cxType != null ? (provider ? provider.props.value : cxType.__) : context; | ||
| if (nodeName.prototype && nodeName.prototype.render) { | ||
| c = vnode.__c = new nodeName(props, cctx); | ||
| c.__v = vnode; | ||
| // turn off stateful re-rendering: | ||
| c._dirty = c.__d = true; | ||
| c.props = props; | ||
| if (c.state==null) c.state = {}; | ||
| if (c._nextState==null && c.__s==null) { | ||
| c._nextState = c.__s = c.state; | ||
| } | ||
| c.context = cctx; | ||
| if (nodeName.getDerivedStateFromProps) { | ||
| c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state)); | ||
| } | ||
| else if (c.componentWillMount) { | ||
| c.componentWillMount(); | ||
| } | ||
| // If the user called setState in cWM we need to flush pending, | ||
| // state updates. This is the same behaviour in React. | ||
| c.state = c._nextState !== c.state | ||
| ? c._nextState : c.__s!==c.state | ||
| ? c.__s : c.state; | ||
| rendered = c.render(c.props, c.state, c.context); | ||
| } | ||
| else { | ||
| // stateless functional components | ||
| rendered = nodeName.call(c, props, cctx); | ||
| } | ||
| if (c.getChildContext) { | ||
| context = assign(assign({}, context), c.getChildContext()); | ||
| } | ||
| return renderToString(rendered, context, selectValue); | ||
| } | ||
| if (UNSAFE_NAME.test(nodeName)) return ''; | ||
| let html = ''; | ||
| let s = '<'; | ||
| s += nodeName; | ||
| if (props) { | ||
| for (let name in props) { | ||
| let v = props[name]; | ||
| let type; | ||
| if (name === 'children' || name === 'key' || name === 'ref' || UNSAFE_NAME.test(name)) { | ||
| // skip | ||
| } | ||
| else if (nodeName === 'option' && name === 'value' && selectValue === v) { | ||
| s += ' selected'; | ||
| selectValue = undefined; | ||
| } | ||
| else if (nodeName === 'select' && name === 'value') { | ||
| selectValue = v; | ||
| } | ||
| else if (name === 'dangerouslySetInnerHTML') { | ||
| html = v && v.__html; | ||
| } | ||
| else if ((v || v===0) && (type = typeof v) !== 'function') { | ||
| if (name === 'style' && type === 'object') { | ||
| v = styleObjToCss(v); | ||
| } | ||
| else if (name.substring(0,5) === 'xlink') { | ||
| // this doesn't actually need to be limited to SVG, since attributes "xlinkHref" are invalid anyway | ||
| name = name.replace(/^xlink([A-Z])/, 'xlink:$1'); | ||
| } | ||
| s += ' '; | ||
| s += name; | ||
| if (v !== true && v !== '') { | ||
| s += '="'; | ||
| s += encodeEntities(v); | ||
| s += '"'; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| let isVoid = VOID_ELEMENTS.test(nodeName); | ||
| if (isVoid) { | ||
| s += ' />'; | ||
| } | ||
| else { | ||
| s += '>'; | ||
| } | ||
| if (html) { | ||
| s += html; | ||
| } | ||
| else if (props && props.children) { | ||
| s += renderToString(props.children, context, selectValue); | ||
| } | ||
| if (!isVoid) { | ||
| s += '</'; | ||
| s += nodeName; | ||
| s += '>'; | ||
| } | ||
| return s; | ||
| } |
16 changes: 11 additions & 5 deletionssrc/util.js
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.