-
Notifications
You must be signed in to change notification settings - Fork 509
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
feat(ssr): remove external files dependencies #10885
Conversation
Before the ssr dist had dependencies on external files (e.g. index.html). By in-lining those dependencies we can generate a portable standalone main.js.
Also fix PUBLIC_URL support
@@ -47,6 +47,7 @@ yarn-error.log* | |||
/server/*.js.map | |||
/ssr/dist/ | |||
/ssr/*.js | |||
!/ssr/mozilla.dnthelper.min.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the file moved.
client/config/paths.js
Outdated
process.env.BASE_URL || | ||
"https://developer.mozilla.org/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add fallback(s). We should replace PUBLIC_URL everywhere, but that's to much for this PR:
@@ -9,13 +9,13 @@ | |||
of the file that has a hash in it. | |||
--> | |||
|
|||
<link rel="icon" href="%PUBLIC_URL%/favicon-48x48.png" /> | |||
<link rel="icon" href="/favicon-48x48.png" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be safe. Those are relative right now because %PUBLIC_URL%
does not work. So we remove them until we want to make them absolute.
@@ -120,6 +121,17 @@ checkBrowsers(paths.appPath, isInteractive) | |||
} | |||
} | |||
) | |||
.then(async () => { | |||
const { results } = await hashSomeStaticFilesForClientBuild(paths.appBuild); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now where the old "optimizeClientBuild" runs. Just after we build the client.
@@ -75,7 +66,7 @@ export async function runOptimizeClientBuild(buildRoot) { | |||
const splitName = filePath.split(extName); | |||
const hashedFilePath = `${splitName[0]}.${hash}${extName}`; | |||
fs.copyFileSync(filePath, hashedFilePath); | |||
const hashedHref = filePathToHref(buildRoot, hashedFilePath); | |||
const hashedHref = filePathToHref(buildRoot, hashedFilePath, href); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ad the href to restore the domain.
client/config/webpack.config.js
Outdated
@@ -95,7 +95,10 @@ function config(webpackEnv) { | |||
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript. | |||
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz. | |||
// Get environment variables to inject into our app. | |||
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1)); | |||
const env = getClientEnvironment( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather remove trailing slash, than just removing the last char.
client/public/index.html
Outdated
@@ -58,7 +58,7 @@ | |||
property="og:description" | |||
content="The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps." | |||
/> | |||
<meta property="og:image" content="%PUBLIC_URL%/mdn-social-share.png" /> | |||
<meta property="og:image" content="%BASE_URL%/mdn-social-share.png" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a new %BASE_URL%
here because fixing %PUBLIC_URL%
is tricky and dangerous.
@@ -9,7 +9,7 @@ import path from "node:path"; | |||
import cheerio from "cheerio"; | |||
import md5File from "md5-file"; | |||
|
|||
export async function runOptimizeClientBuild(buildRoot) { | |||
export async function hashSomeStaticFilesForClientBuild(buildRoot) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this optimized in the past. Now it merely hashes files.
@@ -22,14 +22,15 @@ | |||
"build:curriculum": "cross-env NODE_ENV=production NODE_OPTIONS='--no-warnings=ExperimentalWarning --loader ts-node/esm' node build/build-curriculum.ts", | |||
"build:dist": "tsc -p tsconfig.dist.json", | |||
"build:glean": "cd client && cross-env VIRTUAL_ENV=venv glean translate src/telemetry/metrics.yaml src/telemetry/pings.yaml -f typescript -o src/telemetry/generated", | |||
"build:prepare": "yarn build:client && yarn build:ssr && yarn tool optimize-client-build && yarn tool google-analytics-code && yarn tool popularities && yarn tool spas && yarn tool gather-git-history && yarn tool build-robots-txt", | |||
"build:ssr": "cd ssr && webpack --mode=production", | |||
"build:prepare": "yarn build:client && yarn build:ssr && yarn tool popularities && yarn tool spas && yarn tool gather-git-history && yarn tool build-robots-txt", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optimize-client-build moved into the client build step.
google-analytics-code moved to SSR prepare.
ssr/prepare.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted code from ssr/render.ts
@@ -23,6 +23,10 @@ const config = { | |||
}, | |||
module: { | |||
rules: [ | |||
{ | |||
resourceQuery: /raw/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allows us to include the post processed index.html as string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a local diff between this and main, saw no differences other than those which were intentional. Looks good on staging.
Just one leftover console.log
:
Summary
Before the ssr dist had dependencies on external files (e.g. index.html).
By in-lining those dependencies we can generate a portable standalone main.js.
This also fixed our font preloading logic.
Note
This leaves most of the code as it was and we'll simplify it later. Just to minimize the risk.
Problem
Using the SSR main.js outside of yari failed because of external dependencies.
Solution
Add a build script and inline dependencies.
How did you test this change?
Locally and about to deploy to stage.