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

WSTEAMA-749: Update service worker tests and URL generation logic #11595

Conversation

alex-magana
Copy link
Contributor

@alex-magana alex-magana commented May 6, 2024

Resolves JIRA [number]

Overall changes

Add a list of .

Code changes

public/sw.js

  • Limits .webp URLs pattern to only match images with .jpg and .png
    as the fallback image extension.

src/app/lib/utilities/ichefURL/index.js

  • Add a regex pattern to ensure .webp is only added to
/news/.../cpsprodpb/<image_name>.jpg
/news/.../cpsprodpb/<image_name>.png
/news/.../cpsdevpb/<image_name>.jpg
/news/.../cpsdevpb/<image_name>.png

/ace/ws/.../cpsprodpb/<image_name>.jpg
/ace/ws/.../cpsprodpb/<image_name>.png
/ace/ws/.../cpsdevpb/<image_name>.jpg
/ace/ws/.../cpsdevpb/<image_name>.png

/ace/standard/.../cpsprodpb/<image_name>.jpg
/ace/standard/.../cpsprodpb/<image_name>.png
/ace/standard/.../cpsdevpb/<image_name>.jpg
/ace/standard/.../cpsdevpb/<image_name>.png

/ace/ws/.../amz/worldservice/<image_name>.jpg
/ace/ws/.../amz/worldservice/<image_name>.png
/ace/standard/.../amz/worldservice/<image_name>.jpg
/ace/standard/.../amz/worldservice/<image_name>.png

and excluded from

/news/.../amz/worldservice/<image_name>.jpg
/news/.../amz/worldservice/<image_name>.png

src/app/lib/utilities/ichefURL/index.test.js & src/sw.test.js

  • Add the pertinent tests for the logic changes above.

Testing

  1. List the steps used to test this PR.

Helpful Links

Add Links to useful resources related to this PR if applicable.

Coding Standards

Repository use guidelines

@alex-magana alex-magana changed the title Wsteama 749 implement webp support url downgrade changes WSTEAMA-749: Update service worker tests and URL generation logic May 7, 2024
@@ -1,5 +1,13 @@
import { getEnvConfig } from '../getEnvConfig';

const webpSupportedPatterns = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this mean that according to '//ace/(?:standard|ws)/.+(?:/amz/worldservice/)?.*/', if the url has ace/standard and amz/worldservice then it will always have .webp added to the end, even if it already has. webp on the end? Do some of the ace/standard and amz/worldservice urls ever already have .webp on the end? Just because if we add .webp to one that already has .webp on the end it makes a bad url. Otherwise this looks good! But just checking that scenario. I see the other IRL in this array doesn't have that problem because it specifies .jpg or .png on the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just spoke to @eagerterrier and the URLs won't already have webp on. It is only added in this buildIChefUrl file and the service worker. So this should be ok!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Brill.
I've added this test to ensure we're not appending .webp if it's already in the URL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As regards this query, since the matching pattern checks that the URL ends with .jpg or .png as per (?:\.jpg|\.png), we wouldn't append .webp to the URL if already exists.
This is because this pattern will return false when we check for a match.

public/sw.js Outdated
@@ -12,7 +12,9 @@ self.addEventListener('install', event => {

const fetchEventHandler = async event => {
if (
/^https:\/\/ichef(\.test)?\.bbci\.co\.uk\/.+\.webp$/.test(event.request.url)
/^https:\/\/ichef(\.test)?\.bbci\.co\.uk\/(news|ace\/(standard|ws))\/.+(\.jpg|\.png).webp$/.test(
Copy link
Contributor

Choose a reason for hiding this comment

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

I am thinking about how there are some images with different URL types we may also need to put .webp on in other areas of the code, and then remove it here in cases of it not being supported:

Images like the podcast promo image for example like https://ichef.bbc.co.uk/images/ic/512x512/p0htdh6t.png which we want to be https://ichef.bbc.co.uk/images/ic/512x512/p0htdh6t.png.webp. This one wouldn't fall into this condition, and so we wouldn't remove .webp in the cases webp isn't supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To resolve this issues, do we want to use the less specific pattern below
/^https:\/\/ichef(\.test)?\.bbci\.co\.uk\/.+\.webp$/
or expand the URL checks with something along this lines to cover each
scenario we need to support?
The limitation presented by a less specific pattern is the likelihood of matching
.webp URLs that we aren't aware of i.e we don't know where the .webp is being set.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have looked throught the codebase and I don't see webp being set anywhere else, and it shouldn't be .webp coming from Ares. Maybe this will change when we do some of the work in the BFF to add webp in there, but for now it seems that it will be ok to make the pattern less specific to catch all ichef urls that have webp added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me.
Thanks for looking looking into this concern. With this knowledge, we know we won't
encounter any surprises.
I'll change the pattern to only check for .webp at the end.

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've changed this to (news|images|ace\/(standard|ws)) to

  • retain the rationale behind the specificity introduced to the matching pattern here
  • ensure we're able to keep out these wrong paths in the URL.
  • to ensure the regex pattern remains performant by being able to weed out unsupported URLs based on the image path.
    Do let me know if that's okay.

@LilyL0u LilyL0u merged commit 01aceaa into WSTEAMA-749-implement-webp-support-for-all-images-in-simorgh May 9, 2024
5 of 9 checks passed
@LilyL0u LilyL0u deleted the WSTEAMA-749-implement-webp-support-url-downgrade-changes branch May 9, 2024 16:27
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.

None yet

2 participants