Skip to content

Commit 3168daa

Browse files
Reduce page size (#1258)
* Efficient docstring handling Gather docstrings at the server level and only forward docstrings needed for the specific page. * Remove SiS version * Redirect old SiS paths to latest SiS version * Move docstring to import at top of slug page * Don't pipe versions list * Don't recompute latest version * Rename static load to slug * Don't pipe version list * Delete suggest edit * Fix version order * Clean up version context * Remove debug line * Unversioned -> Versioned page fix * Cleanup * Renaming typo * Review edits * Use replace to prevent cluttering browser history * Prevent page blip from reroute
1 parent 4ecc69d commit 3168daa

File tree

15 files changed

+430
-3875
lines changed

15 files changed

+430
-3875
lines changed

components/blocks/autofunction.js

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useEffect, useState, useRef } from "react";
2-
import reverse from "lodash/reverse";
32
import classNames from "classnames";
43
import Table from "./table";
54
import { H2, H3 } from "./headers";
@@ -14,32 +13,36 @@ import "prismjs/plugins/line-highlight/prism-line-highlight.css";
1413
import "prismjs/plugins/toolbar/prism-toolbar";
1514
import "prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard";
1615
import "prismjs/plugins/normalize-whitespace/prism-normalize-whitespace";
16+
import getConfig from "next/config";
17+
const { publicRuntimeConfig } = getConfig();
1718

1819
import styles from "./autofunction.module.css";
19-
import { name } from "file-loader";
20+
import { looksLikeVersionAndPlatformString } from "../../context/VersionContext";
21+
22+
const LATEST_VERSION = publicRuntimeConfig.LATEST_VERSION;
23+
const DEFAULT_VERSION = publicRuntimeConfig.DEFAULT_VERSION;
24+
const VERSIONS_LIST = publicRuntimeConfig.VERSIONS_LIST;
2025

2126
const cleanHref = (name) => {
2227
return String(name).replace(/\./g, "").replace(/\s+/g, "-");
2328
};
2429

2530
const Autofunction = ({
2631
version,
27-
versions,
2832
streamlitFunction,
29-
streamlit,
33+
docstrings,
3034
slug,
3135
hideHeader,
3236
deprecated,
3337
deprecatedText,
3438
oldStreamlitFunction,
39+
goToLatest,
3540
}) => {
3641
const blockRef = useRef();
3742
const router = useRouter();
38-
const maxVersion = versions[versions.length - 1];
3943
const [isHighlighted, setIsHighlighted] = useState(false);
40-
const [currentVersion, setCurrentVersion] = useState(
41-
version ? version : versions[versions.length - 1],
42-
);
44+
const currentNumericVersion =
45+
version == DEFAULT_VERSION ? LATEST_VERSION : version;
4346

4447
useEffect(() => {
4548
highlightWithPrism();
@@ -97,15 +100,9 @@ const Autofunction = ({
97100
setIsHighlighted(true);
98101
};
99102

100-
const VersionSelector = ({
101-
versionList,
102-
currentVersion,
103-
handleSelectVersion,
104-
}) => {
105-
const isSiS = currentVersion.startsWith("SiS") ? true : false;
106-
const selectClass = isSiS
107-
? "version-select sis-version"
108-
: currentVersion !== versionList[0]
103+
const VersionSelector = ({ currentNumericVersion, handleSelectVersion }) => {
104+
const selectClass =
105+
currentNumericVersion != LATEST_VERSION
109106
? "version-select old-version"
110107
: "version-select";
111108

@@ -114,17 +111,13 @@ const Autofunction = ({
114111
<label>
115112
<span className="sr-only">Streamlit Version</span>
116113
<select
117-
value={currentVersion}
114+
value={currentNumericVersion}
118115
onChange={handleSelectVersion}
119116
className={styles.Select}
120117
>
121-
{versionList.map((version, index) => (
118+
{VERSIONS_LIST.map((version, index) => (
122119
<option value={version} key={version}>
123-
{version == "SiS"
124-
? "Streamlit in Snowflake"
125-
: version.startsWith("SiS.")
126-
? version.replace("SiS.", "Streamlit in Snowflake ")
127-
: "Version " + version}
120+
{"Version " + version}
128121
</option>
129122
))}
130123
</select>
@@ -135,20 +128,17 @@ const Autofunction = ({
135128

136129
const handleSelectVersion = (event) => {
137130
const functionObject =
138-
streamlit[streamlitFunction] ?? streamlit[oldStreamlitFunction];
131+
docstrings[streamlitFunction] ?? docstrings[oldStreamlitFunction];
139132
const slicedSlug = slug.slice();
140133

141-
if (event.target.value !== currentVersion) {
142-
setCurrentVersion(event.target.value);
143-
if (event.target.value !== maxVersion) {
144-
let isnum = /^[\d\.]+$/.test(slicedSlug[0]);
145-
let isSiS = /^SiS[\d\.]*$/.test(slicedSlug[0]);
146-
if (isnum || isSiS) {
147-
slicedSlug[0] = event.target.value;
148-
} else {
149-
slicedSlug.unshift(event.target.value);
150-
}
151-
slug.unshift(event.target.value);
134+
if (event.target.value !== currentNumericVersion) {
135+
if (looksLikeVersionAndPlatformString(slicedSlug[0])) {
136+
slicedSlug.shift();
137+
}
138+
if (event.target.value !== LATEST_VERSION) {
139+
slicedSlug.unshift(event.target.value);
140+
} else {
141+
goToLatest();
152142
}
153143
}
154144

@@ -163,7 +153,6 @@ const Autofunction = ({
163153
const footers = [];
164154
const args = [];
165155
const returns = [];
166-
const versionList = reverse(versions.slice());
167156
let functionObject;
168157
let functionDescription;
169158
let header;
@@ -174,9 +163,9 @@ const Autofunction = ({
174163
let methods = [];
175164
let properties = [];
176165

177-
if (streamlitFunction in streamlit || oldStreamlitFunction in streamlit) {
166+
if (streamlitFunction in docstrings || oldStreamlitFunction in docstrings) {
178167
functionObject =
179-
streamlit[streamlitFunction] ?? streamlit[oldStreamlitFunction];
168+
docstrings[streamlitFunction] ?? docstrings[oldStreamlitFunction];
180169
isClass = functionObject.is_class;
181170
isAttributeDict = functionObject.is_attribute_dict ?? false;
182171
if (
@@ -208,20 +197,15 @@ const Autofunction = ({
208197
{streamlitFunction.replace("streamlit", "st")}
209198
</H2>
210199
<VersionSelector
211-
versionList={versionList}
212-
currentVersion={currentVersion}
200+
currentNumericVersion={currentNumericVersion}
213201
handleSelectVersion={handleSelectVersion}
214202
/>
215203
</div>
216204
<Warning>
217-
{version && version.startsWith("SiS") ? (
218-
<p>This method does not exist in Streamlit in Snowflake.</p>
219-
) : (
220-
<p>
221-
This method did not exist in version <code>{currentVersion}</code>{" "}
222-
of Streamlit.
223-
</p>
224-
)}
205+
<p>
206+
This method did not exist in version{" "}
207+
<code>{currentNumericVersion}</code> of Streamlit.
208+
</p>
225209
</Warning>
226210
</div>
227211
);
@@ -276,8 +260,7 @@ const Autofunction = ({
276260
>
277261
{headerTitle}
278262
<VersionSelector
279-
versionList={versionList}
280-
currentVersion={currentVersion}
263+
currentNumericVersion={currentNumericVersion}
281264
handleSelectVersion={handleSelectVersion}
282265
/>
283266
</div>

components/navigation/navChild.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ import React, { useState } from "react";
22
import Link from "next/link";
33
import classNames from "classnames";
44

5-
import useVersion from "../../lib/useVersion.js";
6-
75
import styles from "./navChild.module.css";
6+
import { looksLikeVersionAndPlatformString } from "../../context/VersionContext";
87

98
const NavChild = ({ slug, page, color, className }) => {
109
const [manualState, setManualState] = useState(null);
11-
const version = useVersion();
12-
13-
const isNum = /^[\d\.]+$/.test(slug[0]);
14-
const isSiS = /^SiS[\d\.]*$/.test(slug[0]);
15-
16-
if (isNum || isSiS) {
10+
if (looksLikeVersionAndPlatformString(slug[0])) {
1711
slug.shift();
1812
}
1913

@@ -89,13 +83,6 @@ const NavChild = ({ slug, page, color, className }) => {
8983
icon = <i className={styles.CrossLinkedIcon}>link</i>;
9084
}
9185

92-
if (page.isVersioned && version && (isRelativePath || isAbsolutePath)) {
93-
// We need to version this URL, check if the URL has a version for this version
94-
const newSlug = url.split("/");
95-
newSlug[0] = version;
96-
url = `/${newSlug.join("/")}`;
97-
}
98-
9986
if (isDivider && page.name == "---") {
10087
navElement = (
10188
<div className={styles.LinkContainer}>

components/utilities/breadCrumbs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { breadcrumbsForSlug } from "../../lib/utils.js";
33
import Link from "next/link";
44

55
import styles from "./breadCrumbs.module.css";
6+
import { looksLikeVersionAndPlatformString } from "../../context/VersionContext.js";
67

78
const BreadCrumbs = ({ slug, menu }) => {
89
const formatedTitle = (title) => {
@@ -43,9 +44,8 @@ const BreadCrumbs = ({ slug, menu }) => {
4344
title: "Home",
4445
});
4546

46-
const isnum = /^[\d\.]+$/.test(slug[0]);
47-
const isSiS = /^SiS[\d\.]*$/.test(slug[0]);
48-
if (isnum || isSiS) {
47+
// TODO: This may be unnecessary
48+
if (looksLikeVersionAndPlatformString(slug[0])) {
4949
paths = slug.slice(1).join("/");
5050
breadcrumbs.push({
5151
link: "#",

components/utilities/floatingNav.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useRef } from "react";
1+
import React, { useState, useEffect } from "react";
22
import { withRouter } from "next/router";
33

44
import styles from "./floatingNav.module.css";
@@ -9,8 +9,8 @@ const useHeadingsData = (slug) => {
99
useEffect(() => {
1010
const headingElements = Array.from(
1111
document.querySelectorAll(
12-
"article.leaf-page h1, article.leaf-page h2, article.leaf-page h3, article.leaf-page h4, article.leaf-page h5, article.leaf-page h6"
13-
)
12+
"article.leaf-page h1, article.leaf-page h2, article.leaf-page h3, article.leaf-page h4, article.leaf-page h5, article.leaf-page h6",
13+
),
1414
);
1515

1616
// If first heading is H1, remove since we don't want to show the main title on the TOC
@@ -69,8 +69,8 @@ const useIntersectionObserver = (slug) => {
6969
"article.leaf-page h4 a:first-of-type",
7070
"article.leaf-page h5 a:first-of-type",
7171
"article.leaf-page h6 a:first-of-type",
72-
].join(",")
73-
)
72+
].join(","),
73+
),
7474
);
7575

7676
// Function that will be called when the links enter/leave the screen.
@@ -147,7 +147,7 @@ const Headings = ({ headings, activeId }) => {
147147
heading.hierarchy = index;
148148
}
149149
return heading;
150-
})
150+
}),
151151
);
152152

153153
return (

context/AppContext.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)