tfrere HF Staff commited on
Commit
f2312e5
·
1 Parent(s): 0094189

fix: always write published HTML locally before HF upload

Browse files

The publish pipeline was uploading to HF dataset and returning
immediately without updating the local copy. Since GET / serves the
local file, the old HTML persisted until a container restart.

Now the local copy is written first, so the published page updates
instantly. HF upload still happens afterwards for persistence.

Made-with: Cursor

Files changed (1) hide show
  1. backend/src/publisher/index.ts +22 -20
backend/src/publisher/index.ts CHANGED
@@ -272,7 +272,27 @@ export async function publishDocument(docName: string, token?: string): Promise<
272
  }
273
  }
274
 
275
- // Try uploading to HF dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  if (useHf) {
277
  try {
278
  const urls = await uploadPublishedAssets(docName, {
@@ -283,28 +303,10 @@ export async function publishDocument(docName: string, token?: string): Promise<
283
  }, token);
284
  return { ...urls, success: true };
285
  } catch (err) {
286
- console.warn("[publish] HF upload failed, falling back to local storage:", (err as Error).message);
287
  }
288
  }
289
 
290
- // Local storage (primary when HF disabled, fallback when HF fails)
291
- const { writeFileSync, mkdirSync } = await import("fs");
292
- const safeDirName = docName.replace(/[^a-zA-Z0-9_-]/g, "_");
293
- const publishDir = join(DATA_DIR, "published", safeDirName);
294
- mkdirSync(publishDir, { recursive: true });
295
-
296
- // For local storage, rewrite meta with local URLs
297
- if (thumbnail) meta.ogImage = `/published/${safeDirName}/thumb.jpg`;
298
- else delete meta.ogImage;
299
- if (pdf) meta.pdfUrl = `/published/${safeDirName}/article.pdf`;
300
- else delete meta.pdfUrl;
301
- html = renderArticleHTML(json, meta, css);
302
-
303
- writeFileSync(join(publishDir, "index.html"), html);
304
- if (pdf) writeFileSync(join(publishDir, "article.pdf"), pdf);
305
- if (thumbnail) writeFileSync(join(publishDir, "thumb.jpg"), thumbnail);
306
- writeFileSync(join(publishDir, "meta.json"), JSON.stringify(meta, null, 2));
307
-
308
  return {
309
  htmlUrl: `/published/${safeDirName}/index.html`,
310
  pdfUrl: pdf ? `/published/${safeDirName}/article.pdf` : null,
 
272
  }
273
  }
274
 
275
+ // Always save locally so GET / serves the fresh HTML immediately
276
+ const { writeFileSync: fsWrite, mkdirSync: fsMkdir } = await import("fs");
277
+ const safeDirName = docName.replace(/[^a-zA-Z0-9_-]/g, "_");
278
+ const publishDir = join(DATA_DIR, "published", safeDirName);
279
+ fsMkdir(publishDir, { recursive: true });
280
+
281
+ // Local copy uses relative URLs for PDF/thumbnail
282
+ const localMeta = { ...meta };
283
+ if (thumbnail) localMeta.ogImage = `/published/${safeDirName}/thumb.jpg`;
284
+ else delete localMeta.ogImage;
285
+ if (pdf) localMeta.pdfUrl = `/published/${safeDirName}/article.pdf`;
286
+ else delete localMeta.pdfUrl;
287
+ const localHtml = renderArticleHTML(json, localMeta, css);
288
+
289
+ fsWrite(join(publishDir, "index.html"), localHtml);
290
+ if (pdf) fsWrite(join(publishDir, "article.pdf"), pdf);
291
+ if (thumbnail) fsWrite(join(publishDir, "thumb.jpg"), thumbnail);
292
+ fsWrite(join(publishDir, "meta.json"), JSON.stringify(localMeta, null, 2));
293
+ console.log("[publish] local copy written to", publishDir);
294
+
295
+ // Also upload to HF dataset for persistence across container restarts
296
  if (useHf) {
297
  try {
298
  const urls = await uploadPublishedAssets(docName, {
 
303
  }, token);
304
  return { ...urls, success: true };
305
  } catch (err) {
306
+ console.warn("[publish] HF upload failed, local copy is still available:", (err as Error).message);
307
  }
308
  }
309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  return {
311
  htmlUrl: `/published/${safeDirName}/index.html`,
312
  pdfUrl: pdf ? `/published/${safeDirName}/article.pdf` : null,