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

Add Lazy Loading for Homepage Images and Check script #5998

Open
wants to merge 3 commits into
base: main
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
28 changes: 28 additions & 0 deletions .github/workflows/knative-lazyload-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Image LazyLoad Check

on:
push:
branches:
- main

jobs:
run-tests:
runs-on: ubuntu-latest

steps:

Check failure on line 12 in .github/workflows/knative-lazyload-check.yaml

View workflow job for this annotation

GitHub Actions / style / suggester / github_actions

[actionlint] reported by reviewdog 🐶 could not parse as YAML: yaml: line 12: did not find expected '-' indicator [syntax-check] Raw Output: .github/workflows/knative-lazyload-check.yaml:12:0: could not parse as YAML: yaml: line 12: did not find expected '-' indicator [syntax-check]
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Set GITHUB_TOKEN environment variable
run: echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV

- name: Run Build
run: ./hack/docker/test.sh

- name: Run Python script
run: python check_img_lazy.py
49 changes: 49 additions & 0 deletions check_img_lazy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import sys
import subprocess
from bs4 import BeautifulSoup
Copy link
Member

Choose a reason for hiding this comment

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

Is it a possibility to use standard Python libraries for parsing HTML files?

What's your opinion here @shivamgupta2020 ? We need to instruct people to install some dependencies if we go with BeautifulSoup. However, we might have some non-standard HTML content that might not be parseable with standard Python libraries.

Let's ask @knative/productivity-leads and @cardil specifically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aliok, I experimented with HTMLParser and IXML for parsing HTML files, but encountered numerous errors and incomplete parsing. Therefore, I switched to BeautifulSoup. I also think we should gather opinions from others on this approach.

Copy link
Member

Choose a reason for hiding this comment

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


def check_img_tags(base_dir):
"""Recursively check HTML files for <img> tags without the lazy loading attribute."""
issues_found = False

for root, _, files in os.walk(base_dir):
for file in files:
if file.endswith('.html'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
soup = BeautifulSoup(content, 'html.parser')
for img in soup.find_all('img'):
if 'loading' not in img.attrs or img.attrs['loading'] != 'lazy':
issues_found = True
line_number = find_line_number(content, img)
print(f"File: {file_path}, Line: {line_number}, Missing lazy loading: {img}")

return issues_found

def find_line_number(content, tag):
"""Find the line number of the given tag in the content."""
content_lines = content.splitlines()


tag_attrs = {k: ' '.join(v) if isinstance(v, list) else v for k, v in tag.attrs.items()}

for i, line in enumerate(content_lines):

if all(str(attr) in line for attr in tag_attrs.values()):
return i + 1

return 'unknown'

if __name__ == "__main__":

base_dir = 'site'
Copy link
Member

Choose a reason for hiding this comment

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

Please make this configurable. Script should receive the directories to check HTML 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.

The function runs on the "site" folder because this folder is created after the build command. That's why I set base_dir = 'site.'
Why do we need to make it configurable?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. Let's leave as is. I need to check this later.

issues_found = check_img_tags(base_dir)


if issues_found:
sys.exit(1)
else:
print("All img tags have lazy loading attribute.")
sys.exit(0)
29 changes: 15 additions & 14 deletions overrides/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h1>Why serverless containers?</h1>
<div class="reason-img">
<img src="images/home-images/blue_simple_abstractions_icon.svg"
onmouseover="this.src='images/home-images/dkblue_simple_abstractions_icon.svg'"
onmouseout="this.src='images/home-images/blue_simple_abstractions_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_simple_abstractions_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Simpler Abstractions</h2>
Expand All @@ -72,7 +72,7 @@ <h2>Simpler Abstractions</h2>
<div class="reason-img">
<img src="images/home-images/blue_autoscaling_icon.svg"
onmouseover="this.src='images/home-images/dkblue_autoscaling_icon.svg'"
onmouseout="this.src='images/home-images/blue_autoscaling_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_autoscaling_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Autoscaling</h2>
Expand All @@ -85,7 +85,7 @@ <h2>Autoscaling</h2>
<div class="reason-img">
<img src="images/home-images/blue_progressive_rollouts_icon.svg"
onmouseover="this.src='images/home-images/dkblue_progressive_rollouts_icon.svg'"
onmouseout="this.src='images/home-images/blue_progressive_rollouts_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_progressive_rollouts_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Progressive Rollouts</h2>
Expand All @@ -100,7 +100,7 @@ <h2>Progressive Rollouts</h2>
<div class="reason-img">
<img src="images/home-images/blue_event_intergrations_icon.svg"
onmouseover="this.src='images/home-images/dkblue_event_intergrations_icon.svg'"
onmouseout="this.src='images/home-images/blue_event_intergrations_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_event_intergrations_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Event Integrations</h2>
Expand All @@ -113,7 +113,7 @@ <h2>Event Integrations</h2>
<div class="reason-img">
<img src="images/home-images/blue_handle_events_icon.svg"
onmouseover="this.src='images/home-images/dkblue_handle_events_icon.svg'"
onmouseout="this.src='images/home-images/blue_handle_events_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_handle_events_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Handle Events</h2>
Expand All @@ -126,7 +126,7 @@ <h2>Handle Events</h2>
<div class="reason-img">
<img src="images/home-images/blue_plugable_icon.svg"
onmouseover="this.src='images/home-images/dkblue_plugable_icon.svg'"
onmouseout="this.src='images/home-images/blue_plugable_icon.svg'" alt="" draggable="false">
onmouseout="this.src='images/home-images/blue_plugable_icon.svg'" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Plugable</h2>
Expand All @@ -148,7 +148,7 @@ <h1>Knative Components</h1>
Eventing work together to automate and manage tasks and applications.
</p>
<div class="components-content">
<img class="components-img" src="images/home-images/knative_flowchart_graphic.svg" alt="" draggable="false" />
<img class="components-img" src="images/home-images/knative_flowchart_graphic.svg" alt="" draggable="false" loading="lazy" />
<div class="text-table">
<p class="normal-text">
Run serverless containers in Kubernetes with ease. Knative takes care of the details of networking,
Expand All @@ -171,7 +171,7 @@ <h1>Trying it all together</h1>
<div class="img-row">
<div class="img-col">
<div class="reason-img">
<img src="images/home-images/dev_focus_code_icon.svg" alt="" draggable="false">
<img src="images/home-images/dev_focus_code_icon.svg" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Developers focus on code</h2>
Expand All @@ -184,7 +184,7 @@ <h2>Developers focus on code</h2>

<div class="img-col">
<div class="reason-img">
<img src="images/home-images/dkblue_scale_icon.svg" alt="" draggable="false">
<img src="images/home-images/dkblue_scale_icon.svg" alt="" draggable="false" loading="lazy">
</div>
<div class="reason-text">
<h2>Scale up and down</h2>
Expand All @@ -197,7 +197,7 @@ <h2>Scale up and down</h2>

<div class="img-col">
<div class="reason-img">
<img src="images/home-images/cloud_agnostic_icon.svg" alt="" draggable="false">
<img src="images/home-images/cloud_agnostic_icon.svg" alt="" draggable="false" loading="lazy" >
</div>
<div class="reason-text">
<h2>Cloud agnostic</h2>
Expand All @@ -224,7 +224,7 @@ <h1>
<div class="col">
<a href="{{ 'about/case-studies/sva/' | url }}">
<div class="logo">
<img class="sva" src="images/case-studies/sva.png" alt="" draggable="false" />
<img class="sva" src="images/case-studies/sva.png" alt="" draggable="false" loading="lazy" />
</div>
<hr>
<p class="normal-text">
Expand All @@ -236,7 +236,7 @@ <h1>
<div class="col">
<a href="{{ 'about/case-studies/ibm/' | url }}">
<div class="logo">
<img class="ibm" src="images/case-studies/ibm.png" alt="IBM logo" draggable="false" width="75" />
<img class="ibm" src="images/case-studies/ibm.png" alt="IBM logo" draggable="false" width="75" loading="lazy" />
</div>
<hr>
<p class="normal-text">
Expand All @@ -248,7 +248,7 @@ <h1>
<div class="col">
<a href="{{ 'about/case-studies/pnc/' | url }}">
<div class="logo">
<img class="pnc" src="images/case-studies/pnc_bank.png" alt="" draggable="false" />
<img class="pnc" src="images/case-studies/pnc_bank.png" alt="" draggable="false" loading="lazy" />
</div>
<hr>
<p class="normal-text">
Expand All @@ -263,7 +263,7 @@ <h1>

<section class="cncf-notice-container">
<h3>Knative is a <a href="https://www.cncf.io/">Cloud Native Computing Foundation</a> incubation project</h3>
<img class="cncf-logo" src="images/home-images/cncf-color.png" alt="" draggable="false" />
<img class="cncf-logo" src="images/home-images/cncf-color.png" alt="" draggable="false" loading="lazy" />
</section>

<section class="whats-next-container">
Expand Down Expand Up @@ -370,6 +370,7 @@ <h2>Follow us</h2>
adopterImg.src = adopter.logoPath;
adopterImg.alt = "Adopters";
adopterImg.classList.add("logo");
adopterImg.loading = "lazy";

adopterLink.appendChild(adopterImg);
container.appendChild(adopterLink);
Expand Down
Loading