Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created September 3, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterbe/cd5320780944d4badfb881264cacfba6 to your computer and use it in GitHub Desktop.
Save peterbe/cd5320780944d4badfb881264cacfba6 to your computer and use it in GitHub Desktop.
diff --git a/kuma/javascript/src/article.jsx b/kuma/javascript/src/article.jsx
index 5f6d6d52d..36c5177f3 100644
--- a/kuma/javascript/src/article.jsx
+++ b/kuma/javascript/src/article.jsx
@@ -74,6 +74,8 @@ export default function Article({ document }: DocumentProps) {
const isArchive =
document.slug === 'Archive' || document.slug.startsWith('Archive/');
+ console.log("RENDERING ARTICLE", article);
+
return (
/*
* The "text-content" class and "wikiArticle" id are required
diff --git a/kuma/javascript/src/index.jsx b/kuma/javascript/src/index.jsx
index 172b97fb0..a1a533389 100644
--- a/kuma/javascript/src/index.jsx
+++ b/kuma/javascript/src/index.jsx
@@ -15,9 +15,14 @@ if (container) {
// with all the data needed to hydrate or render the UI.
let data = window._react_data;
- // Remove the global reference to this data object so that it can
- // be garbage collected once it is no longer in use.
- window._react_data = null; // eslint-disable-line camelcase
+ let quickLinksHTMLElement = document.querySelector('.sidebar .quick-links');
+ if (quickLinksHTMLElement) {
+ data.documentData.quickLinksHTML = quickLinksHTMLElement.innerHTML;
+ }
+ let wikiArticleElement = document.querySelector('#wikiArticle')
+ if (wikiArticleElement) {
+ data.documentData.bodyHTML = wikiArticleElement.innerHTML;
+ }
// Store the string catalog so that l10n.gettext() can do translations
localize(data.locale, data.stringCatalog, data.pluralFunction);
diff --git a/kuma/wiki/templatetags/ssr.py b/kuma/wiki/templatetags/ssr.py
index df1426400..38a81ecdc 100644
--- a/kuma/wiki/templatetags/ssr.py
+++ b/kuma/wiki/templatetags/ssr.py
@@ -62,6 +62,13 @@ def _render(component_name, html, state):
pluralExpression = state['pluralExpression']
del state['pluralExpression']
+ for key in ('quickLinksHTML', 'bodyHTML'):
+ if state.get('documentData', {}).get(key):
+ # state['documentData'].pop(key)
+ with open('culling.log', 'a') as f:
+ culled = state['documentData'].pop(key)
+ f.write("SAVED {:,} bytes by removing {!r}\n".format(len(culled), key))
+
# Serialize the state object to JSON and be sure the string
# "</script>" does not appear in it, since we are going to embed it
# within an HTML <script> tag.
@@ -133,29 +140,6 @@ def server_side_render(component_name, data):
# component_name is "document", we're going to make a copy of the
# data (because the original belongs to our caller) and delete those
# strings from the copy.
- #
- # WARNING: This optimization can save 20kb in data transfer
- # for typical pages, but it requires us to be very careful on
- # the frontend. If any components render conditionally based on
- # the state of bodyHTML, tocHTML or quickLinkHTML, then they will
- # render differently on the client than during SSR, and the hydrate
- # will not just work cleanly, and those components will re-render
- # with empty strings. This has already caused Bug 1558308, and
- # I've commented it out because the benefit in file size doesn't
- # seem worth the risk of client-side bugs.
- #
- # As an alternative, it ought to be possible to extract the HTML
- # strings from the SSR'ed document and rebuild the document object
- # on the client right before we call hydrate(). So if you uncomment
- # the lines below, you should also edit kuma/javascript/src/index.jsx
- # to extract the HTML from the document as well.
- #
- # if component_name == 'document':
- # data = data.copy()
- # data['documentData'] = data['documentData'].copy()
- # data['documentData'].update(bodyHTML='',
- # tocHTML='',
- # quickLinksHTML='')
return _render(component_name, response.text, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment