Dough Git

commit

Split the viewer bundle and preload the bucket listing

commit
ee66320dc8f792a6929425bf8ba7c0206913a22f
author
Clove Twilight <[email protected]>
date
2026-11-13 19:45
Svelte, marked and three each get their own chunk, and the Licence,
Preview and Uploader views load on demand. The first page now needs
about 26 KB of JS (gzipped) in two parallel files instead of 41 KB in one.

index.html preloads /api/list so the listing starts with the HTML
rather than after the app has loaded and run.

Co-Authored-By: Sandrone <[email protected]>

diff --git a/vite.config.ts b/vite.config.ts
index 66a90cb..cd7af33 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,3 +1,5 @@
+/* vite.config.ts */
+
 // Builds the viewer (web/) into dist/, which wrangler serves as static assets.
 // public/ is copied in as-is.
 
@@ -21,5 +23,30 @@ export default defineConfig({
     emptyOutDir: true,
     // Kept under one reserved prefix so the bundle can't shadow a folder.
     assetsDir: "_app",
+    rolldownOptions: {
+      output: {
+        // Libraries get their own chunks, so they load in parallel and stay
+        // cached across app deploys. Each one still loads only where it's used.
+        codeSplitting: {
+          groups: [
+            {
+              name: "three",
+              test: /[\\/]node_modules[\\/]three[\\/]/,
+              priority: 30,
+            },
+            {
+              name: "marked",
+              test: /[\\/]node_modules[\\/]marked[\\/]/,
+              priority: 20,
+            },
+            {
+              name: "svelte",
+              test: /[\\/]node_modules[\\/](svelte|esm-env|clsx)[\\/]/,
+              priority: 10,
+            },
+          ],
+        },
+      },
+    },
   },
 });
diff --git a/web/index.html b/web/index.html
index a415b80..a83fa75 100644
--- a/web/index.html
+++ b/web/index.html
@@ -20,6 +20,8 @@
   <link rel="icon" href="/favicon.png" type="image/png" sizes="512x512">
   <link rel="apple-touch-icon" href="/favicon.png">
   <link rel="stylesheet" href="/style.css">
+  <!-- Starts the bucket listing now, instead of after the JS has loaded and run -->
+  <link rel="preload" href="/api/list" as="fetch" crossorigin>
 </head>
 <body>
   <div id="app" style="display: contents"></div>
diff --git a/web/src/App.svelte b/web/src/App.svelte
index 1bf5d7b..cca8df6 100644
--- a/web/src/App.svelte
+++ b/web/src/App.svelte
@@ -4,9 +4,12 @@
   import Header from "./lib/Header.svelte";
   import { route } from "./lib/router.svelte";
   import Browse from "./routes/Browse.svelte";
-  import Licence from "./routes/Licence.svelte";
   import NotFound from "./routes/NotFound.svelte";
-  import Preview from "./routes/Preview.svelte";
+
+  // Split into their own chunks: most visits only ever browse.
+  // Licence pulls in marked, Preview the media and text viewers.
+  const loadLicence = () => import("./routes/Licence.svelte");
+  const loadPreview = () => import("./routes/Preview.svelte");
 
   let listing = $state<Listing | null>(null);
   let failed = $state(false);
@@ -42,7 +45,8 @@
       (entry) => entry.path === target || entry.path.startsWith(`${target}/`),
     );
   }
-  load();
+  // Fetches the Preview chunk once the list is in, so opening a file doesn't wait on it
+  load().then(() => loadPreview()).catch(() => {});
   fetchMe().then((m) => (me = m));
 
   // "/img/avatars/" → "img/avatars"; null if the path doesn't decode.
@@ -57,11 +61,19 @@
 <Header {me} />
 <main class="wrap">
   {#if route.path === "/licence"}
-    <Licence />
+    {#await loadLicence() then { default: Licence }}
+      <Licence />
+    {:catch}
+      <p class="empty">Couldn't load this page. Try reloading.</p>
+    {/await}
   {:else if path === null}
     <NotFound />
   {:else if file && listing}
-    <Preview {file} raw={listing.raw} canEdit={Boolean(me)} onchanged={() => load(true)} />
+    {#await loadPreview() then { default: Preview }}
+      <Preview {file} raw={listing.raw} canEdit={Boolean(me)} onchanged={() => load(true)} />
+    {:catch}
+      <p class="empty">Couldn't load the preview. Try reloading.</p>
+    {/await}
   {:else}
     <Browse prefix={path} {listing} {failed} canEdit={Boolean(me)} onchanged={() => load(true)} />
   {/if}
diff --git a/web/src/lib/files.ts b/web/src/lib/files.ts
index 43542c6..616c624 100644
--- a/web/src/lib/files.ts
+++ b/web/src/lib/files.ts
@@ -23,10 +23,12 @@ export function encodePath(path: string): string {
 }
 
 export async function fetchList(fresh = false): Promise<Listing> {
-  // ?fresh=1 makes the worker skip its edge cache too
+  // ?fresh=1 makes the worker skip its edge cache too.
+  // The plain fetch keeps the default cache mode so it picks up the
+  // <link rel="preload"> in index.html; max-age=0 still revalidates it.
   const res = fresh
     ? await fetch("/api/list?fresh=1", { cache: "no-store" })
-    : await fetch("/api/list", { cache: "no-cache" });
+    : await fetch("/api/list");
   if (!res.ok) throw new Error(`list failed: ${res.status}`);
   return res.json();
 }
diff --git a/web/src/routes/Browse.svelte b/web/src/routes/Browse.svelte
index 4532818..8f09bb9 100644
--- a/web/src/routes/Browse.svelte
+++ b/web/src/routes/Browse.svelte
@@ -6,12 +6,14 @@
   import Crumbs from "../lib/Crumbs.svelte";
   import Hero from "../lib/Hero.svelte";
   import Icon from "../lib/Icon.svelte";
-  import Uploader from "../lib/Uploader.svelte";
   import { apiWrite, encodePath, fmtSize, type Listing } from "../lib/files";
   import { link, navigate } from "../lib/router.svelte";
   import { FOLDER_MARKER } from "../../../shared/folders";
   import NotFound from "./NotFound.svelte";
 
+  // Only signed-in visitors see the uploader, so everyone else skips its chunk
+  const loadUploader = () => import("../lib/Uploader.svelte");
+
   let {
     prefix,
     listing,
@@ -134,7 +136,9 @@
         {#if error}<span class="status err">{error}</span>{/if}
       </div>
 
-      <Uploader folder={prefix} {onchanged} />
+      {#await loadUploader() then { default: Uploader }}
+        <Uploader folder={prefix} {onchanged} />
+      {/await}
     {/if}
 
     <input class="filter" type="search" placeholder="Filter this folder…" autocomplete="off" bind:value={filter} />