Skip to content

fix: Resolve bulk downloads, PostHog crash, and missing file UX - #544

Open
Rithish-2914 wants to merge 4 commits into
CodeChefVIT:stagingfrom
Rithish-2914:fix/zip-and-posthog-errors
Open

Rithish-2914 wants to merge 4 commits into
CodeChefVIT:stagingfrom
Rithish-2914:fix/zip-and-posthog-errors

Conversation

@Rithish-2914

Copy link
Copy Markdown
Contributor

Heellooo guys, this PR squashes multiple bugs and drastically improves the UX when interacting with "ghost" papers (papers where the file was deleted from Google Cloud Storage but the record still exists in the DB):

  1. Fixes [BUG] Zip download missing extension, silent fail on empty #523 (Bulk ZIP Extension): Downloading multiple papers at once was returning a blank file because the code forgot to append the .zip extension. It now correctly saves as a standard .zip file!
  2. PostHog Server Crash: Next.js Webpack has been mangling the posthog-node library during production builds, throwing a PostHog is not a constructor error on uploads. I added a bulletproof workaround using a dynamic namespace import to ensure analytics track cleanly.
  3. Missing File UX (Download Button): If a user clicks download on a missing paper, the browser used to silently fail in the background. It now gracefully catches the 404 error and displays a clean toast notification: "This paper is no longer available on the server."
  4. Missing File UX (PDF Viewer): Clicking to view a missing paper used to result in an infinite loading spinner because the PDF engine ignored the 404 error. The viewer now performs a lightning-fast pre-flight HEAD check. If the file is missing, it immediately aborts the PDF engine and displays a clean "Paper Not Found" UI overlay.

@Rithish-2914
Rithish-2914 force-pushed the fix/zip-and-posthog-errors branch from 4d54012 to 6223596 Compare October 5, 2026 14:50
@Rithish-2914
Rithish-2914 force-pushed the fix/zip-and-posthog-errors branch from 6223596 to 48bfb89 Compare October 5, 2026 14:54
Comment thread src/lib/posthog-server.ts
}
posthogClient ??= new PostHog(token, {

const phModule = require("posthog-node");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it gets build on every upload or an api call...may be do something like If(posthogClient)return it ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch brooooo You're totally right, it was hitting that require on every single upload(i didnt see tht)
I just pushed a commit that adds an early return at the very top so it caches the client immediately after the first call.

Comment thread src/components/newPdfViewer.tsx Outdated

useEffect(() => {
let isMounted = true;
fetch(url, { method: "HEAD" })

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already handle that error, still do we need this ? will it delay the paper fetching by any chance?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually still need the HEAD request! The issue is that the PDF library's built-in error handler completely fails when GCS returns a 404 (because GCS returns an XML page, and the viewer gets stuck trying to parse it as a PDF, causing an infinite loading spinner). The HEAD request is just a tiny pre-flight check that catches the 404 instantly before the viewer can hang.
And don't worry about delays! Because it's a HEAD request, it only grabs a few bytes of server headers without downloading the actual PDF payload. It takes less than 50ms and happens in the background before the heavy WASM engine even initializes, so there's zero noticeable delay in paper fetching so no worries

Comment thread src/lib/utils/download.ts Outdated
} catch (error) {
console.error("Download failed:", error);
console.error("Download failed (File missing or network error):", error);
toast.error("This paper is no longer available on the server.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this error is shown only for a particular error response status, else even for a no internet situation, this might be shown to the uses..kind of misleading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! If their internet drops, they shouldn't see the "paper is missing" error. I just updated the catch block to check navigator.onLine. Now, if it's an actual network failure, it gives a specific "You are offline" toast instead!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Zip download missing extension, silent fail on empty

2 participants