Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix favicon "not found" error on non-homepage pages #617

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link id="defaultIcon1" rel="shortcut icon" href="{{ site.baseurl }}assets/favicon_06.png" />
<link id="defaultIcon1" rel="shortcut icon" href="{{ site.baseurl }}assets/favicon_06.png" />
<title>{{ page.title }}</title>
<link href="{{ site.baseurl }}/css/googleFonts.css" rel="stylesheet" type="text/css" >
<meta name="robots" content="index,follow,NOODP" />
Expand Down
11 changes: 10 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ $(document).ready(function () {
if (logoID < 10) {
logoID = "0" + logoID;
}
document.querySelector('#defaultIcon1').href = 'https://www.sugarlabs.org/assets/favicon_' + logoID + '.png';
Copy link
Contributor

Choose a reason for hiding this comment

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

Code you are removing has nothing to do with the issue; the issue and my tests of master branch just now show the error to be a reference to press/assets/favicon_06.png and this line you are removing is an absolute link without press.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have simplified the favicon logic by making the path relative to /press/, removing the full domain URL, and eliminating unnecessary existence checks, as shown in the following code:

var defaultIcon = document.querySelector('#defaultIcon1');
if (defaultIcon) {
    var logoID = colorIndex + 1;
    if (logoID < 10) {
        logoID = "0" + logoID;
    }
    defaultIcon.href = '/press/assets/favicon_' + logoID + '.png';
}

Also, I have removed the testing and existence checks. Is this approach fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

You've lost me here. Why would the path be made absolute from /press/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I think relative path will be better for this like:

var defaultIcon = document.querySelector('#defaultIcon1');
if (defaultIcon) {
    var logoID = colorIndex + 1;
    if (logoID < 10) {
        logoID = "0" + logoID;
    }
    defaultIcon.href = 'assets/favicon_' + logoID + '.png';
}

Now it's pointing to the assets/ directory relative to the current location, which should be more flexible.


var defaultIcon = document.querySelector('#defaultIcon1');
if (defaultIcon) {
var logoID = colorIndex + 1;
if (logoID < 10) {
logoID = "0" + logoID;
}
defaultIcon.href = 'assets/favicon_' + logoID + '.png';
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still taking a relative path?

I ask because I still see things like this in the logs:

Screenshot from 2025-01-04 16-25-26

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, is use of relative path. Oops.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this still taking a relative path?

I ask because I still see things like this in the logs:

Screenshot from 2025-01-04 16-25-26

Yes, it is now taking relative paths as intended. Previously, the code was referencing the favicon like this:

document.querySelector('#defaultIcon1').href = 'https://www.sugarlabs.org/assets/favicon_' + logoID + '.png';

However, I have updated it to use relative paths, as shown here:

defaultIcon.href = 'assets/favicon_' + logoID + '.png';

After this change, I am no longer seeing any errors in my logs, whether on Firefox or other browsers.

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, relative or absolute, doesn't matter, as long as the directory that contains the icons is accessed on both the root and lower pages. It works for the root page, but as @pikurasa said it doesn't work on lower pages, such as about-us/. That's what you need to fix now.

Copy link
Contributor

Choose a reason for hiding this comment

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

(a relative access from about-us/ would be to ../assets/)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, relative or absolute, doesn't matter, as long as the directory that contains the icons is accessed on both the root and lower pages. It works for the root page, but as @pikurasa said it doesn't work on lower pages, such as about-us/. That's what you need to fix now.

In the _layouts/base.html, the previous line was:

<link id="defaultIcon1" rel="shortcut icon" href="{{ site.baseurl }}assets/favicon_06.png" /> 

I changed it to:

<link id="defaultIcon1" rel="shortcut icon" href="{{ site.baseurl }}/assets/favicon_06.png" /> 

The issue was that there was no forward slash between {{ site.baseurl }} and assets, which caused incorrect path resolution when navigating between pages like /about-us/. Now, the problem of the favicon "not found" error on non-homepage pages is not there in Firefox.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I know, but I was not talking about that part of the change. I was talking about the part of the change that this discussion thread is linked to; js/main.js line 26, which loads a relative path into the element href, without dereferencing the current document's relative path, which is why the browser then reports failures to find the png files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I know, but I was not talking about that part of the change. I was talking about the part of the change that this discussion thread is linked to; js/main.js line 26, which loads a relative path into the element href, without dereferencing the current document's relative path, which is why the browser then reports failures to find the png files.

@quozl The change that I made for this is as follows:

var defaultIcon = document.querySelector('#defaultIcon1');
if (defaultIcon) {
    var logoID = colorIndex + 1;
    if (logoID < 10) {
        logoID = "0" + logoID;
    }

    // Extract the base path from the current favicon href
    var currentHref = defaultIcon.getAttribute('href');
    var basePath = currentHref.substring(0, currentHref.lastIndexOf('/') + 1);
    
    // Update only the filename portion while keeping the original path
    defaultIcon.href = basePath + 'favicon_' + logoID + '.png';
}

And in _layouts/base.html:

<link id="defaultIcon1" rel="shortcut icon" href="{{ site.baseurl }}/assets/favicon_06.png" />

This approach resolves the issue of subdirectories by dynamically extracting the base path from the current favicon and then updating only the filename. By preserving the base path that starts from {{ site.baseurl }}, this ensures that the favicon works across all pages, regardless of how deep they are within the site's directory structure.

Would this be a good approach to resolve the issue? I’d also appreciate any suggestions you might have to improve it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hope so. Commit the change and push to the pull request branch so that I can see it in context and we can test.

}

var h = document.querySelector('.logo1').innerHTML;
h = h.replace(/033cd2/g, selectedColors[0]);
h = h.replace(/78e600/g, selectedColors[1]);
Expand Down