Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ssr): use jsx in ssr/render.tsx #11672

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

chore(ssr): use jsx in ssr/render.tsx #11672

wants to merge 16 commits into from

Conversation

LeoMcA
Copy link
Member

@LeoMcA LeoMcA commented Aug 22, 2024

NB: first commit just renames ssr/render.ts to ssr/render.tsx, easiest to see changes in the second commit (with whitespace changes ignored)

Summary

https://mozilla-hub.atlassian.net/browse/MP-1445

First of many incremental improvements to the client build system.

Problem

  • We do hard to understand/modify manual string manipulation of a template to SSR our app.
  • We also do a bunch of weird custom asset handling/hashing/etc. which webpack is exceptionally good at due to our webpack config being previously hidden behind CRA.

Solution

  • Move our index.html template into ssr/render as JSX.
  • Generate CSP hash for inline assets in webpack
  • Remove manual asset hashing of icons etc.
  • Deliberately kept other changes in ssr/render minimal to reduce diff/make review easier - improvements/simplifications/etc. there are for further iterations

How did you test this change?

git checkout main
yarn && yarn build:prepare
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build:docs
yarn render:html
mv client/build ../build.main

# repeat for this branch
git checkout ssr-render-jsx
yarn && yarn build:prepare
BUILD_FOLDERSEARCH=web/javascript/reference/global_objects/array/foreach yarn build:docs
yarn render:html
mv client/build ../build.ssr-jsx

# manual diff (vscode gives nice inline diffs)
code --diff ../build.main/en-us/index.html ../build.ssr-jsx/en-us/index.html
code --diff ../build.main/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html ../build.ssr-jsx/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html

# generate usable diffs for below
npx prettier ../build.main/en-us/index.html ../build.ssr-jsx/en-us/index.html ../build.main/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html ../build.ssr-jsx/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html --write
diff -u ../build.main/en-us/index.html ../build.ssr-jsx/en-us/index.html
diff -u ../build.main/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html ../build.ssr-jsx/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html
--- ../build.main/en-us/index.html      2024-08-22 10:50:31.949840758 +0000
+++ ../build.ssr-jsx/en-us/index.html   2024-08-22 10:50:32.069847759 +0000
@@ -3,10 +3,19 @@
   <head>
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width,initial-scale=1" />
-    <link rel="icon" href="/favicon-48x48.cbbd161b.png" />
-    <link rel="apple-touch-icon" href="/apple-touch-icon.6803c6f0.png" />
+    <link
+      rel="icon"
+      href="https://developer.mozilla.org/favicon-48x48.bc390275e955dacb2e65.png"
+    />
+    <link
+      rel="apple-touch-icon"
+      href="https://developer.mozilla.org/apple-touch-icon.528534bba673c38049c2.png"
+    />
     <meta name="theme-color" content="#ffffff" />
-    <link rel="manifest" href="/manifest.56b1cedc.json" />
+    <link
+      rel="manifest"
+      href="https://developer.mozilla.org/manifest.f42880861b394dd4dc9b.json"
+    />
     <link
       rel="search"
       type="application/opensearchdescription+xml"
@@ -19,7 +28,7 @@
       as="font"
       type="font/woff2"
       href="/static/media/Inter.var.c2fe3cb2b7c746f7966a.woff2"
-      crossorigin
+      crossorigin=""
     />
     <link
       rel="alternate"
@@ -43,7 +52,7 @@
     />
     <meta
       property="og:image"
-      content="https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png"
+      content="https://developer.mozilla.org/mdn-social-share.d893525a4fb5fb1f67a2.png"
     />
     <meta property="og:image:type" content="image/png" />
     <meta property="og:image:height" content="1080" />
@@ -78,7 +87,7 @@
         border-left-width: 2px;
       }
     </style>
-    <script defer="defer" src="/static/js/main.8ef4b2e8.js"></script>
+    <script defer="" src="/static/js/main.8ef4b2e8.js"></script>
     <link href="/static/css/main.debc7a6f.css" rel="stylesheet" />
   </head>
   <body>
@@ -1066,7 +1075,7 @@
                       ></span
                     >
                   </p>
-                  <span class="request-date">14 minutes ago</span>
+                  <span class="request-date">34 minutes ago</span>
                 </li>
                 <li class="request-item">
                   <p class="request-title">
@@ -1080,7 +1089,7 @@
                       ></span
                     >
                   </p>
-                  <span class="request-date">35 minutes ago</span>
+                  <span class="request-date">an hour ago</span>
                 </li>
                 <li class="request-item">
                   <p class="request-title">
--- ../build.main/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html   2024-08-22 10:50:32.259858842 +0000
+++ ../build.ssr-jsx/en-us/docs/web/javascript/reference/global_objects/array/foreach/index.html        2024-08-22 10:50:32.399867009 +0000
@@ -3,10 +3,19 @@
   <head>
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width,initial-scale=1" />
-    <link rel="icon" href="/favicon-48x48.cbbd161b.png" />
-    <link rel="apple-touch-icon" href="/apple-touch-icon.6803c6f0.png" />
+    <link
+      rel="icon"
+      href="https://developer.mozilla.org/favicon-48x48.bc390275e955dacb2e65.png"
+    />
+    <link
+      rel="apple-touch-icon"
+      href="https://developer.mozilla.org/apple-touch-icon.528534bba673c38049c2.png"
+    />
     <meta name="theme-color" content="#ffffff" />
-    <link rel="manifest" href="/manifest.56b1cedc.json" />
+    <link
+      rel="manifest"
+      href="https://developer.mozilla.org/manifest.f42880861b394dd4dc9b.json"
+    />
     <link
       rel="search"
       type="application/opensearchdescription+xml"
@@ -67,7 +76,7 @@
       as="font"
       type="font/woff2"
       href="/static/media/Inter.var.c2fe3cb2b7c746f7966a.woff2"
-      crossorigin
+      crossorigin=""
     />
     <link
       rel="alternate"
@@ -99,7 +108,7 @@
     />
     <meta
       property="og:image"
-      content="https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png"
+      content="https://developer.mozilla.org/mdn-social-share.d893525a4fb5fb1f67a2.png"
     />
     <meta property="og:image:type" content="image/png" />
     <meta property="og:image:height" content="1080" />
@@ -137,7 +146,7 @@
         border-left-width: 2px;
       }
     </style>
-    <script defer="defer" src="/static/js/main.8ef4b2e8.js"></script>
+    <script defer="" src="/static/js/main.8ef4b2e8.js"></script>
     <link href="/static/css/main.debc7a6f.css" rel="stylesheet" />
   </head>
   <body>

As you can see, the changes are minimal:

  • pre-pending https://developer.mozilla.org to the icon/manifest paths
  • different hashing algo/length on icon/manifest paths
  • small html attribute syntax changes, e.g crossorigin -> crossorigin=""
  • not visible after running through prettier, but visible in the inline diff: self closing some tags which weren't before

@LeoMcA LeoMcA requested review from mdn-bot and a team as code owners August 22, 2024 11:12
@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label Aug 22, 2024
@LeoMcA LeoMcA marked this pull request as draft August 22, 2024 11:32
@LeoMcA
Copy link
Member Author

LeoMcA commented Aug 23, 2024

@fiji-flo as requested, benchmarking yarn render:html on main vs this branch for en-us content:

main:

$ cross-env NODE_ENV=production NODE_OPTIONS='--no-warnings=ExperimentalWarning --loader ts-node/esm' node build/ssr-cli.ts
WARNING: Skipped rendering HTML. Document is missing url: /workspace/yari/client/build/en-us/search-index.json
Rendered 12,794 pages in 22.7 seconds, at a rate of 562.7 documents per second.
Done in 24.71s.
yarn render:html  27.35s user 9.38s system 147% cpu 24.856 total

this branch:

$ cross-env NODE_ENV=production NODE_OPTIONS='--no-warnings=ExperimentalWarning --loader ts-node/esm' node build/ssr-cli.ts
WARNING: Skipped rendering HTML. Document is missing url: /workspace/yari/client/build/en-us/search-index.json
Rendered 12,794 pages in 22.6 seconds, at a rate of 566.9 documents per second.
Done in 24.63s.
yarn render:html  28.13s user 8.00s system 145% cpu 24.777 total

So it's almost exactly as fast.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Sep 5, 2024
Copy link
Contributor

github-actions bot commented Sep 5, 2024

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added merge conflicts 🚧 Please rebase onto or merge the latest main. and removed merge conflicts 🚧 Please rebase onto or merge the latest main. labels Sep 6, 2024
Copy link
Contributor

github-actions bot commented Sep 6, 2024

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added merge conflicts 🚧 Please rebase onto or merge the latest main. and removed merge conflicts 🚧 Please rebase onto or merge the latest main. labels Sep 6, 2024
Copy link
Contributor

github-actions bot commented Sep 9, 2024

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot removed the merge conflicts 🚧 Please rebase onto or merge the latest main. label Sep 10, 2024
@LeoMcA LeoMcA marked this pull request as ready for review September 10, 2024 10:12
Copy link
Contributor

@caugner caugner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, left some comments.

PS: To double-check, does this have any impact on localhost:3000 or (particularly) localhost:5042?

client/config/webpack.config.js Show resolved Hide resolved
client/public/index.html Show resolved Hide resolved
client/public/index.html Show resolved Hide resolved
ssr/prepare.ts Outdated Show resolved Hide resolved
ssr/theme.js Outdated Show resolved Hide resolved
testing/tests/index.test.ts Show resolved Hide resolved
testing/tests/csp.test.ts Show resolved Hide resolved
ssr/webpack.config.js Show resolved Hide resolved
ssr/.gitignore Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@LeoMcA
Copy link
Member Author

LeoMcA commented Sep 10, 2024

PS: To double-check, does this have any impact on localhost:3000 or (particularly) localhost:5042?

There's a change in localhost:3000, in that we're now using a very plain index.html template, rather than the one with all the dummy code in it for ssr/render to find and replace - but it still works as it did.

No change to localhost:5042, other than the changes to stage/prod as well: i.e. a few html formatting changes with self closing tags and empty attributes etc.

Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file merge conflicts 🚧 Please rebase onto or merge the latest main.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants