Speed up creating a new document (#13310)

* perf: Preload editor chunks when creating a new document

The document scene, multiplayer editor, and editor itself each sit behind
a separate lazy boundary, so their chunks were downloaded one after the
other and only once the create request had resolved. Start them in
parallel on intent instead.

* perf: Don't wait for collaborative sync to edit an empty document

A newly created document has no cached content, so the read-only cache
editor shown while syncing renders identically to the live one – yet the
editable document stayed hidden until the websocket had connected,
authenticated and loaded. Only show the cache when there is content.

* fix: Right sidebar flashing open when a document loads

A history panel left in the persisted UI state is cleared in an effect,
by which point the sidebar has already rendered open for a frame and has
to animate away again. Derive the open state from the panel that will
actually be displayed instead.
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-05 19:08:21 -04:00
1 parent a30aef7129
commit fed1d4c869
5 files changed
+56 -3

No files matched your search

+8 -1
View File
@@ -6,6 +6,7 @@ import Button from "~/components/Button";
import Tooltip from "~/components/Tooltip";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import { preloadEditor } from "~/routes/scenes";
import { newDocumentPath } from "~/utils/routeHelpers";
function NewDocumentMenu() {
@@ -19,7 +20,13 @@ function NewDocumentMenu() {
return (
<Tooltip content={t("New document")} shortcut="n" placement="bottom">
<Button as={Link} to={newDocumentPath()} icon={<PlusIcon />}>
<Button
as={Link}
to={newDocumentPath()}
icon={<PlusIcon />}
onPointerEnter={preloadEditor}
onFocus={preloadEditor}
>
{t("New doc")}
</Button>
</Tooltip>
+12
View File
@@ -12,3 +12,15 @@ export const Drafts = lazy(() => import("~/scenes/Drafts"));
export const Home = lazy(() => import("~/scenes/Home"));
export const Search = lazy(() => import("~/scenes/Search"));
export const Trash = lazy(() => import("~/scenes/Trash"));
/**
* Warms the chunks required to render a document with an editable editor. Each
* of these is behind a separate lazy boundary that would otherwise only begin
* downloading once the previous one has rendered, so preloading them in
* parallel removes a request waterfall from the critical path.
*/
export function preloadEditor() {
void Document.preload();
void import("~/scenes/Document/components/MultiplayerEditor");
void import("~/editor");
}
@@ -1,5 +1,6 @@
import { HocuspocusProvider, WebSocketStatus } from "@hocuspocus/provider";
import { throttle } from "es-toolkit/compat";
import { Node as ProsemirrorNode } from "prosemirror-model";
import {
useState,
useLayoutEffect,
@@ -17,6 +18,7 @@ import * as Y from "yjs";
import { EditorUpdateError } from "@shared/collaboration/CloseEvents";
import History from "@shared/editor/extensions/History";
import EDITOR_VERSION from "@shared/editor/version";
import { ProsemirrorDataHelper } from "@shared/utils/ProsemirrorDataHelper";
import { supportsPassiveListener } from "@shared/utils/browser";
import type { Props as EditorProps } from "~/components/Editor";
import Editor from "~/components/Editor";
@@ -328,7 +330,8 @@ function MultiplayerEditor(
// while the collaborative document is loading, we render a version of the
// document from the last text cache in read-only mode if we have it.
const isLocalReady = !hasLocalPersistence || isLocalSynced;
const showCache = !isLocalReady && !isRemoteSynced;
const showCache =
!isLocalReady && !isRemoteSynced && hasContent(props.defaultValue);
return (
<>
@@ -365,4 +368,28 @@ function MultiplayerEditor(
);
}
/**
* Whether the last known content of the document is worth displaying while the
* collaborative document loads. An empty document, such as one that was just
* created, renders identically either way so there is nothing to wait for.
*
* @param value the cached editor content.
* @returns true if the content is non-empty.
*/
function hasContent(value: Props["defaultValue"]): boolean {
if (!value) {
return false;
}
if (typeof value === "string") {
return !!value.trim();
}
if (value instanceof ProsemirrorNode) {
return !!value.textContent.trim();
}
return !ProsemirrorDataHelper.isEmpty(value);
}
export default forwardRef<SharedEditor, Props>(MultiplayerEditor);
@@ -91,7 +91,10 @@ export default function useDocumentSidebar() {
path: matchDocumentHistory,
});
const panel = ui.getRightSidebar(pane);
const isOpen = panel !== null;
// A history panel outside of a history route is closed by the effect below,
// so it is treated as closed here – otherwise the sidebar renders open for a
// frame and then animates away again.
const isOpen = panel !== null && (panel !== "history" || isHistoryRoute);
const wasOpenRef = React.useRef(isOpen);
React.useEffect(() => {
+4
View File
@@ -11,6 +11,7 @@ import PlaceholderDocument from "~/components/PlaceholderDocument";
import useCurrentUser from "~/hooks/useCurrentUser";
import useQuery from "~/hooks/useQuery";
import useStores from "~/hooks/useStores";
import { preloadEditor } from "~/routes/scenes";
import { documentEditPath, documentPath } from "~/utils/routeHelpers";
function DocumentNew() {
@@ -25,6 +26,9 @@ function DocumentNew() {
const id = match.params.collectionSlug || query.get("collectionId");
useEffect(() => {
// Download the editor while the document is being created on the server
preloadEditor();
async function createDocument() {
const index = parseInt(query.get("index") || "0", 10);
const parentDocumentId = query.get("parentDocumentId") ?? undefined;