Skip to content

Commit 8b86fd8

Browse files
authored
Reduce minified JS bundle size from 189kb to 73kb (github#17552)
1 parent 2572d7b commit 8b86fd8

File tree

11 files changed

+56
-95
lines changed

11 files changed

+56
-95
lines changed

includes/scripts.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<script id="search-options" type="application/json">{{ searchOptions }}</script>
12
<script src="{{ builtAssets.main.js }}"></script>

javascripts/copy-code.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import Clipboard from 'clipboard'
2-
31
export default () => {
4-
const clipboard = new Clipboard('button.js-btn-copy')
2+
const buttons = Array.from(document.querySelectorAll('button.js-btn-copy'))
3+
4+
if (!buttons) return
5+
6+
buttons.forEach(button =>
7+
button.addEventListener('click', async evt => {
8+
const text = button.dataset.clipboardText
9+
await navigator.clipboard.writeText(text)
510

6-
clipboard.on('success', evt => {
7-
const btn = evt.trigger
8-
const beforeTooltip = btn.getAttribute('aria-label')
9-
btn.setAttribute('aria-label', 'Copied!')
11+
const beforeTooltip = button.getAttribute('aria-label')
12+
button.setAttribute('aria-label', 'Copied!')
1013

11-
setTimeout(() => {
12-
btn.setAttribute('aria-label', beforeTooltip)
13-
}, 2000)
14-
})
14+
setTimeout(() => {
15+
button.setAttribute('aria-label', beforeTooltip)
16+
}, 2000)
17+
})
18+
)
1519
}

javascripts/display-platform-specific-content.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
const { getPlatformFromUserAgent } = require('platform-utils')
1+
import parseUserAgent from './user-agent'
22
const supportedPlatforms = ['mac', 'windows', 'linux']
33
const detectedPlatforms = new Set()
44

55
// Emphasize content for the visitor's OS (inferred from user agent string)
66

77
export default function displayPlatformSpecificContent () {
8-
let platform = getDefaultPlatform() || getPlatformFromUserAgent()
8+
let platform = getDefaultPlatform() || parseUserAgent().os
99

1010
// adjust platform names to fit existing mac/windows/linux scheme
1111
if (!platform) platform = 'mac' // default to 'mac' on mobile
1212
if (platform === 'darwin') platform = 'mac'
13+
if (platform === 'ios') platform = 'mac'
14+
if (platform === 'android') platform = 'linux'
1315
if (platform.startsWith('win')) platform = 'windows'
1416

1517
const platformsInContent = findPlatformSpecificContent(platform)

javascripts/search.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { tags } from './hyperscript'
22
import { sendEvent } from './events'
3-
const searchWithYourKeyboard = require('search-with-your-keyboard')
4-
const truncate = require('html-truncate')
5-
const languages = require('../lib/languages')
6-
const allVersions = require('../lib/all-versions')
7-
const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
3+
import searchWithYourKeyboard from 'search-with-your-keyboard'
4+
import truncate from 'html-truncate'
85

9-
const languageCodes = Object.keys(languages)
106
const maxContentLength = 300
117

128
let $searchInputContainer
@@ -29,8 +25,13 @@ export default function search () {
2925
$searchOverlay = document.querySelector('.search-overlay-desktop')
3026

3127
// There's an index for every version/language combination
32-
version = deriveVersionFromPath()
33-
language = deriveLanguageCodeFromPath()
28+
const {
29+
languages,
30+
versions,
31+
nonEnterpriseDefaultVersion
32+
} = JSON.parse(document.getElementById('search-options').text)
33+
version = deriveVersionFromPath(versions, nonEnterpriseDefaultVersion)
34+
language = deriveLanguageCodeFromPath(languages)
3435

3536
// Find search placeholder text in a <meta> tag, falling back to a default
3637
const $placeholderMeta = document.querySelector('meta[name="site.data.ui.search.placeholder"]')
@@ -109,23 +110,16 @@ function closeSearch () {
109110
onSearch()
110111
}
111112

112-
function deriveLanguageCodeFromPath () {
113+
function deriveLanguageCodeFromPath (languageCodes) {
113114
let languageCode = location.pathname.split('/')[1]
114115
if (!languageCodes.includes(languageCode)) languageCode = 'en'
115116
return languageCode
116117
}
117118

118-
function deriveVersionFromPath () {
119+
function deriveVersionFromPath (allVersions, nonEnterpriseDefaultVersion) {
119120
// fall back to the non-enterprise default version (FPT currently) on the homepage, 404 page, etc.
120121
const versionStr = location.pathname.split('/')[2] || nonEnterpriseDefaultVersion
121-
const versionObject = allVersions[versionStr] || allVersions[nonEnterpriseDefaultVersion]
122-
123-
// if GHES, returns the release number like 2.21, 2.22, etc.
124-
// if FPT, returns 'dotcom'
125-
// if GHAE, returns 'ghae'
126-
return versionObject.plan === 'enterprise-server'
127-
? versionObject.currentRelease
128-
: versionObject.miscBaseName
122+
return allVersions[versionStr] || allVersions[nonEnterpriseDefaultVersion]
129123
}
130124

131125
// Wait for the event to stop triggering for X milliseconds before responding

javascripts/wrap-code-terms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import escape from 'lodash/escape'
12
const wordsLongerThan18Chars = /[\S]{18,}/g
23
const camelCaseChars = /([a-z])([A-Z])/g
34
const underscoresAfter12thChar = /([\w:]{12}[^_]*?)_/g
45
const slashChars = /([/\\])/g
5-
const { escape } = require('lodash')
66

77
// This module improves table rendering on reference pages by inserting a <wbr>
88
// tag in code terms that use camelcase, slashes, or underscores, inspired by

lib/search/versions.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const allVersions = require('../all-versions')
22

3-
module.exports = new Set(
4-
Object.values(allVersions)
5-
.map(version =>
3+
module.exports = Object.fromEntries(
4+
Object.entries(allVersions)
5+
.map(([versionStr, versionObject]) => [
6+
versionStr,
67
// if GHES, resolves to the release number like 2.21, 2.22, etc.
78
// if FPT, resolves to 'dotcom'
89
// if GHAE, resolves to 'ghae'
9-
version.plan === 'enterprise-server'
10-
? version.currentRelease
11-
: version.miscBaseName
12-
)
10+
versionObject.plan === 'enterprise-server'
11+
? versionObject.currentRelease
12+
: versionObject.miscBaseName
13+
])
1314
)

middleware/context.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const productNames = require('../lib/product-names')
88
const warmServer = require('../lib/warm-server')
99
const featureFlags = Object.keys(require('../feature-flags'))
1010
const builtAssets = require('../lib/built-asset-urls')
11+
const searchVersions = require('../lib/search/versions')
12+
const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
1113

1214
// Supply all route handlers with a baseline `req.context` object
1315
// Note that additional middleware in middleware/index.js adds to this context object
@@ -46,5 +48,12 @@ module.exports = async function contextualize (req, res, next) {
4648
// JS + CSS asset paths
4749
req.context.builtAssets = builtAssets
4850

51+
// Languages and versions for search
52+
req.context.searchOptions = JSON.stringify({
53+
languages: Object.keys(languages),
54+
versions: searchVersions,
55+
nonEnterpriseDefaultVersion
56+
})
57+
4958
return next()
5059
}

middleware/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require('express')
22
const languages = new Set(Object.keys(require('../lib/languages')))
3-
const versions = require('../lib/search/versions')
3+
const versions = new Set(Object.values(require('../lib/search/versions')))
44
const loadLunrResults = require('../lib/search/lunr-search')
55
const loadAlgoliaResults = require('../lib/search/algolia-search')
66

package-lock.json

Lines changed: 5 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"browser-date-formatter": "^3.0.3",
3232
"change-case": "^3.1.0",
3333
"cheerio": "^1.0.0-rc.3",
34-
"clipboard": "^2.0.6",
3534
"compression": "^1.7.4",
3635
"connect-datadog": "0.0.9",
3736
"connect-slashes": "^1.4.0",
@@ -73,7 +72,6 @@
7372
"morgan": "^1.9.1",
7473
"node-fetch": "^2.6.1",
7574
"parse5": "^6.0.1",
76-
"platform-utils": "^1.2.0",
7775
"port-used": "^2.0.8",
7876
"rate-limit-redis": "^2.0.0",
7977
"react": "^17.0.1",

0 commit comments

Comments
 (0)