Skip to content

Fix viewport size calculation on Firefox first load#1271

Open
BuildPushkar wants to merge 5 commits intokiwix:mainfrom
BuildPushkar:fix-homepage-results
Open

Fix viewport size calculation on Firefox first load#1271
BuildPushkar wants to merge 5 commits intokiwix:mainfrom
BuildPushkar:fix-homepage-results

Conversation

@BuildPushkar
Copy link
Copy Markdown

Fixes #1192

This fix a issue where the initial viewport size was calculated
incorrectly in Firefox when DevTools is open, resulting in fewer results.

The calculation is now based on clientWidth/clientHeight rather than
outerWidth for greater consistency.

Copy link
Copy Markdown
Collaborator

@kelson42 kelson42 left a comment

Choose a reason for hiding this comment

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

@BuildPushkar Thank you for your PR, without pre-judging any other feedback from my fellow reviewers, can you please first ensure a proper code formatting (spacing, indentation, lining)?

Please also update appropriate cache busting ID to fix the test. All the necessary information is in the README.md, at https://github.com/kiwix/libkiwix

@BuildPushkar
Copy link
Copy Markdown
Author

@BuildPushkar Thank you for your PR, without pre-judging any other feedback from my fellow reviewers, can you please first ensure a proper code formatting (spacing, indentation, lining)?

Please also update appropriate cache busting ID to fix the test. All the necessary information is in the README.md, at https://github.com/kiwix/libkiwix

Thank you for the feedback! @kelson42
I’ll review the spacing, indentation, and overall formatting properly nd I’ll also follow the README.md regarding cache busting and update the required IDs accordingly.
I’ll push the changes soon.

@kelson42
Copy link
Copy Markdown
Collaborator

@BuildPushkar thx
@rgaudin Does it solve the bug for you?

Comment on lines +125 to +129
let zoom = 1;

if (window.innerWidth) {
zoom = Math.floor((width / window.innerWidth) * 100) || 1;
}
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.

What is the meaning of the zoom variable? The presence of * 100 in the calculation implies that it is measured in % which is in contradiction with the default value of 1.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for pointing this out.
I’ve updated the logic to use a zoom ratio instead of a percentage and removed the *100 factor, so it now reflects the actual layout to viewport ratio.

Comment on lines +131 to +132
const rows = Math.floor(height / (3 * zoom) + 1);
const cols = Math.floor(width / (2.5 * zoom) + 1);
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.

I don't understand the logic of this calculation. IMHO, the formula for the number of rows/columns should involve the height/width of the items being laid out in a grid (and maybe vertical/horizontal spacing between them), but there are some magic numbers 3 and 2.5 which bear no meaning to me.

I see that it is carried over from the previous implementation, but I think that we should strive to bring more clarity into code that we have to modify for any reason.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, thanks.
These values were inherited from the previous implementation, but I’ve now extracted them into named constants with comments to make their purpose clearer. Let me know if you’d prefer a different approach.

Copy link
Copy Markdown
Collaborator

@veloman-yunkan veloman-yunkan Feb 23, 2026

Choose a reason for hiding this comment

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

Introduction of CARD_HEIGHT_FACTOR/CARD_WIDTH_FACTOR doesn't help much if at all. I'd rather want to see a formula that makes geometrical sense (something like height/(cardHeight + verticalSpacing).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you, I get your point.
The constants were mainly to keep the existing behavi0r for now. i agree that a formula based on actual card height and spacing would make more sense. I’ll try to rework this part using a geometry based approach

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.

Please don't take too much pain pursuing this improvement. If it doesn't come easy, just clean-up this PR so that the addressed bug is fixed with minimal changes to the existing code and the CI is green (unit tests pass).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Got it, thank you
I’ll keep this PR focused on fixing the Firefox viewport issue with minimal changes and make sure everything is clean.

@rgaudin
Copy link
Copy Markdown
Member

rgaudin commented Feb 23, 2026

@BuildPushkar thx @rgaudin Does it solve the bug for you?

Yes it does 👍

Comment on lines -120 to +145
function viewPortToCount(){
const zoom = Math.floor((( window.outerWidth - 10 ) / window.innerWidth) * 100);
return Math.floor((window.innerHeight/(3*zoom) + 1)*(window.innerWidth/(2.5*zoom) + 1));
function viewPortToCount() {
// Use stable viewport size instead of outerWidth (Firefox devtools issue)
const width =
document.documentElement.clientWidth || window.innerWidth;

const height =
document.documentElement.clientHeight || window.innerHeight;

// Zoom ratio btw layout width and viewport width
let zoomRatio = 1;

if (window.innerWidth) {
zoomRatio = width / window.innerWidth || 1;
}

// Estimated card size (in viewport units)
const CARD_HEIGHT_FACTOR = 3;
const CARD_WIDTH_FACTOR = 2.5;

const rows = Math.floor(
height / (CARD_HEIGHT_FACTOR * zoomRatio) + 1
);

const cols = Math.floor(
width / (CARD_WIDTH_FACTOR * zoomRatio) + 1
);
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.

I think this won't work. Before this change zoom used to be percentage, but the formulas for rows and cols have been actually preserved in spite of the fact that zoomRatio is now being used instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, I see what you mean.
The goal was to switch to a ratio while keeping the existing behavior stable but I agree this may not fully preserve the original scaling.
I’ll revisit the calculation and see how we can better align it with the new zoomRatio semantics.

@BuildPushkar
Copy link
Copy Markdown
Author

@kelson42 are there any updates regarding this PR?

i noticed the “changes requested” badge is still present.
Pls let me know if there’s anything else i should address.
Thanks!

@kelson42
Copy link
Copy Markdown
Collaborator

@BuildPushkar I was kind of waiting to you ;)

@BuildPushkar
Copy link
Copy Markdown
Author

@BuildPushkar I was kind of waiting to you ;)

Sorry for the waiting. My lap stopped working, so I had to borrow a friend’s lap to continue my work.

@BuildPushkar
Copy link
Copy Markdown
Author

BuildPushkar commented Feb 28, 2026

@kelson42 whenever you free, let me know should i make changes? i’ll be happy to address them.
thank you :)

@kelson42
Copy link
Copy Markdown
Collaborator

kelson42 commented Feb 28, 2026

@kelson42 whenever you free, let me know should i make changes? i’ll be happy to address them.
thank you!

I'm a kind of facilitator here.

Hard core reviewer is @veloman-yunkan.

But please remove the main merges. There are kind of taboo by us because they make the revision log really dirty.

Once the PR review over, we will do a rebase on main. Can you please do that?

@kelson42 kelson42 requested a review from veloman-yunkan March 1, 2026 00:20
@kelson42
Copy link
Copy Markdown
Collaborator

kelson42 commented Mar 1, 2026

@veloman-yunkan AFAIK we are read here for a new review pass

Comment on lines -120 to +145
function viewPortToCount(){
const zoom = Math.floor((( window.outerWidth - 10 ) / window.innerWidth) * 100);
return Math.floor((window.innerHeight/(3*zoom) + 1)*(window.innerWidth/(2.5*zoom) + 1));
function viewPortToCount() {
// Use stable viewport size instead of outerWidth (Firefox devtools issue)
const width =
document.documentElement.clientWidth || window.innerWidth;

const height =
document.documentElement.clientHeight || window.innerHeight;

// Zoom ratio btw layout width and viewport width
let zoomRatio = 1;

if (window.innerWidth) {
zoomRatio = width / window.innerWidth || 1;
}

// Estimated card size (in viewport units)
const CARD_HEIGHT_FACTOR = 3;
const CARD_WIDTH_FACTOR = 2.5;

const rows = Math.floor(
height / (CARD_HEIGHT_FACTOR * zoomRatio) + 1
);

const cols = Math.floor(
width / (CARD_WIDTH_FACTOR * zoomRatio) + 1
);
Copy link
Copy Markdown
Collaborator

@veloman-yunkan veloman-yunkan Mar 1, 2026

Choose a reason for hiding this comment

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

Has this code been tested? The concern raised during the previous has not been addressed, and therefore it looks like the functionality should be utterly broken unless the return value of viewPortToCount() (which is now ~10000 larger than before the change) doesn't matter at all (which would mean a different kind of defect of the implementation).

@veloman-yunkan
Copy link
Copy Markdown
Collaborator

Actually, it looks like no changes other than bringing updates from the main branch into this PR have been done since last review. I wonder why another review has been requested then.

@BuildPushkar
Copy link
Copy Markdown
Author

Apologies the branch update was accidental and did not include functional changes.

I’m currently re-evaluating the viewport calculation logic based on the review comments and will push an actual fix shortly.

@kelson42 whenever you free, let me know should i make changes? i’ll be happy to address them.
thank you!

I'm a kind of facilitator here.

Hard core reviewer is @veloman-yunkan.

But please remove the main merges. There are kind of taboo by us because they make the revision log really dirty.

Once the PR review over, we will do a rebase on main. Can you please do that?

yes, i’ll clean up the branch history and remove the merge commits. i’ll rebase properly once the review changes are addressed.

@BuildPushkar
Copy link
Copy Markdown
Author

Actually, it looks like no changes other than bringing updates from the main branch into this PR have been done since last review. I wonder why another review has been requested then.

i mistakenly updated the branch without functional changes that wasn’t intentional and re-requested the review for the understading where im doing wrong

@BuildPushkar
Copy link
Copy Markdown
Author

Actually, it looks like no changes other than bringing updates from the main branch into this PR have been done since last review. I wonder why another review has been requested then.

i mistakenly updated the branch without functional changes(that wasn’t intentional) and re-requested the review for the understading where im doing wrong

@BuildPushkar
Copy link
Copy Markdown
Author

image

i've reworked the function to derive rows and columns from actual card dimensions and margins instead of using zoombased scaling.
The calculation now follows:
viewport / (card size + spacing)
Sharing this implementation before pushing, does this align with what you had in mind? @veloman-yunkan

@veloman-yunkan
Copy link
Copy Markdown
Collaborator

@BuildPushkar That code makes sense, but please test it for various window sizes and zoom ratios.

@BuildPushkar
Copy link
Copy Markdown
Author

@BuildPushkar That code makes sense, but please test it for various window sizes and zoom ratios.

yeah sure

@BuildPushkar
Copy link
Copy Markdown
Author

BuildPushkar commented Mar 13, 2026

sorry for the delay, my life is giving me a lot of trouble :)

i'm having trouble reproducing the exact testing setup locally.
@veloman-yunkan could you clarify the recommended way to test the homepage result calculation locally?
i thought i could figure it out by myself, however I'm still a beginner.

Thanks!

@veloman-yunkan
Copy link
Copy Markdown
Collaborator

@BuildPushkar I am not sure that I understand the problems that you are having and the advice that you are seeking.

The simple flow is to build kiwix-tools (which is a separate project depending on libkiwix), run kiwix-serve and access its front-end via a web browser.

If you have to do changes in the JS code during testing there is a more efficient flow eliminating the need to build kiwix-tools every time you update the sources in libkiwix (see #779).

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.

Limited number of results on homepage

4 participants