Skip to content

Conversation

@Avinash99b
Copy link
Contributor

@Avinash99b Avinash99b commented Sep 2, 2025

fix(printer-service): ensure PDF renders correctly by caching resume data before reload

Previously used evaluateOnNewDocument, which caused localStorage to be set too late.
Now the resume data is cached via evaluate() before reload, ensuring the page
loads with correct data and improving reliability and efficiency.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed intermittent cases where the artboard preview or print output could load with missing, outdated, or blank content by changing the initialization sequence to ensure injected data is applied before rendering.
  • Performance & Stability

    • Improved reliability by waiting for the first page and any profile image resources to finish loading before continuing, reducing race conditions and improving rendering consistency.

@coderabbitai
Copy link

coderabbitai bot commented Sep 2, 2025

📝 Walkthrough

Walkthrough

The printer service changes page initialization from pre-document evaluateOnNewDocument injection to a post-navigation flow: navigate to /artboard/preview, inject resume data via page.evaluate, then reload; it also waits for the first page selector and conditionally waits for profile image load. Exported/public interfaces unchanged.

Changes

Cohort / File(s) Summary of changes
Printer preview load flow
apps/server/src/printer/printer.service.ts
Replaced pre-document localStorage injection (evaluateOnNewDocument) with sequence: page.goto('/artboard/preview', { waitUntil: 'domcontentloaded' })page.evaluate(set localStorage resume data)page.reload({ waitUntil: 'load' }); added awaiting page.waitForSelector('[data-page="1"]', { timeout: 15000 }) in parallel with reload; added conditional image-load synchronization when resume.data.basics.picture.url exists (wait for profile image selector and for all document.images to finish loading/errored); PDF generation and storage flow unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant PrinterService
  participant BrowserPage as Browser Page

  Client->>PrinterService: requestPrint(resumeData)
  PrinterService->>BrowserPage: goto('/artboard/preview') (domcontentloaded)
  Note over BrowserPage: initial DOM parsed
  PrinterService->>BrowserPage: evaluate(() => localStorage.setItem(...resumeData))
  Note over BrowserPage: resume data written to localStorage
  PrinterService->>BrowserPage: reload() (load)
  par wait for page and selector
    BrowserPage-->>PrinterService: load event
    BrowserPage-->>PrinterService: '[data-page="1"]' selector present
  end
  alt profile picture present
    PrinterService->>BrowserPage: wait for profile image selector
    PrinterService->>BrowserPage: evaluate(() => await all document.images to load/error)
  end
  PrinterService->>BrowserPage: proceed with CSS, buffering, PDF generation
  BrowserPage-->>PrinterService: PDF blob
  PrinterService->>Client: return result / store PDF
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hopped to preview, then I set the scene,
A stash in localStorage—quick and clean.
A little reload, the canvas wakes,
The pages line up, the profile takes.
With whiskers twitching, I print—ta-da! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/server/src/printer/printer.service.ts (1)

136-146: Wait for the app to render after reload; load is too early and can yield zero-sized pages.

After reload, querying [data-page] immediately may race the React render, producing width/height = 0 and flaky PDFs. Wait for DOM readiness and for page elements to exist.

-      await page.reload({ waitUntil: "load" });
+      await Promise.all([
+        page.reload({ waitUntil: "domcontentloaded" }),
+        // Wait until first page is present before proceeding
+        page.waitForSelector('[data-page="1"]', { timeout: 15000 }),
+      ]);
🧹 Nitpick comments (1)
apps/server/src/printer/printer.service.ts (1)

127-129: Comment is inaccurate: using localStorage, not session storage.

Update for clarity.

-      // Set the data of the resume to be printed in the browser's session storage
+      // Set the data of the resume to be printed in the browser's localStorage
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b995a6b and 8e9b409.

📒 Files selected for processing (1)
  • apps/server/src/printer/printer.service.ts (1 hunks)
🔇 Additional comments (1)
apps/server/src/printer/printer.service.ts (1)

132-135: LGTM: set localStorage before applying via reload.

This fixes the timing issue by caching data pre‑reload.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
apps/server/src/printer/printer.service.ts (1)

130-135: Good fix: earlier-load via evaluate + reload; domcontentloaded avoids SPA hangs.

This addresses the late localStorage injection and the prior networkidle0 concern. 👍

🧹 Nitpick comments (2)
apps/server/src/printer/printer.service.ts (2)

136-141: Avoid race between reload and waitForSelector; add explicit readiness (fonts, pages).

Waiting for the selector while navigating can sporadically fail on nav context swaps. Sequential waits + a fonts/page-count check are more robust.

-      await Promise.all([
-        page.reload({ waitUntil: "load" }),
-        // Wait until first page is present before proceeding
-        page.waitForSelector('[data-page="1"]', { timeout: 15_000 }),
-      ]);
+      await page.reload({ waitUntil: "domcontentloaded" });
+      // App ready: ensure first page + expected page count are present
+      await page.waitForSelector('[data-page="1"]', { timeout: 15_000 });
+      await page.waitForFunction(
+        (expected) => document.querySelectorAll('[data-page]').length === expected,
+        {},
+        numberPages,
+      );
+      // Ensure web fonts are loaded before PDF rendering
+      await page.evaluate(async () => {
+        if (document.fonts?.ready) await document.fonts.ready;
+      });

127-129: Comment mismatch: using localStorage, not session storage.

Update the comment for clarity.

-      // Set the data of the resume to be printed in the browser's session storage
+      // Cache resume data in window.localStorage for the reload to pick up
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 8e9b409 and d8e0ced.

📒 Files selected for processing (1)
  • apps/server/src/printer/printer.service.ts (1 hunks)

@AmruthPillai AmruthPillai merged commit c7f8daa into AmruthPillai:main Oct 1, 2025
1 check passed
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.

2 participants