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

feat(ssr): remove external files dependencies #10885

Merged
merged 21 commits into from
Apr 25, 2024
Merged

feat(ssr): remove external files dependencies #10885

merged 21 commits into from
Apr 25, 2024

Conversation

fiji-flo
Copy link
Contributor

@fiji-flo fiji-flo commented Apr 9, 2024

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.

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.
@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label Apr 9, 2024
@@ -47,6 +47,7 @@ yarn-error.log*
/server/*.js.map
/ssr/dist/
/ssr/*.js
!/ssr/mozilla.dnthelper.min.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the file moved.

Comment on lines 23 to 24
process.env.BASE_URL ||
"https://developer.mozilla.org/"
Copy link
Contributor Author

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" />
Copy link
Contributor Author

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);
Copy link
Contributor Author

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);
Copy link
Contributor Author

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.

@@ -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(
Copy link
Contributor Author

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.

@@ -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" />
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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",
Copy link
Contributor Author

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
Copy link
Contributor Author

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/,
Copy link
Contributor Author

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.

@fiji-flo fiji-flo marked this pull request as ready for review April 22, 2024 12:41
@fiji-flo fiji-flo requested review from mdn-bot and a team as code owners April 22, 2024 12:41
@fiji-flo fiji-flo requested a review from argl April 22, 2024 12:42
@LeoMcA LeoMcA requested review from LeoMcA and removed request for argl April 23, 2024 10:24
@LeoMcA LeoMcA self-assigned this Apr 23, 2024
caugner

This comment was marked as outdated.

Copy link
Member

@LeoMcA LeoMcA left a 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:

testing/tests/index.test.ts Outdated Show resolved Hide resolved
@fiji-flo fiji-flo merged commit 8fea595 into main Apr 25, 2024
16 checks passed
@fiji-flo fiji-flo deleted the ssr2.0 branch April 25, 2024 10:58
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants