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

Strict SW catalog image checking. #2392

Merged
merged 4 commits into from
May 12, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ describe('Testing findSameOrLargerImage', () => {
expect(returnedResponse.url).toBe(expectedUrl);
});

test('Should not return response if URL for same filename is not available', async () => {
const fileName1 = 'prefixed-file-name.jpg';
const fileName2 = 'file-name.jpg';

mockMatchFn.mockReturnValue(
Promise.resolve({
url: `https://develop.pwa-venia.com/media/catalog/v/s/${fileName1}?auto=webp&format=pjpg&width=1600&height=2000`
})
);

const returnedResponse = await findSameOrLargerImage(
new URL(
`https://develop.pwa-venia.com/media/catalog/v/s/${fileName2}?auto=webp&format=pjpg&width=1600&height=2000`
)
);

expect(returnedResponse).toBeUndefined();
});

test('Should return the closest high resolution image response from cache for a given width', async () => {
const requestedUrl =
'https://develop.pwa-venia.com/media/catalog/v/s/vsk12-la_main_3.jpg?auto=webp&format=pjpg&width=800&height=1000';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export const findSameOrLargerImage = async url => {

const cache = await caches.open(CATALOG_CACHE_NAME);
const cachedURLs = await cache.keys();
const cachedSources = await cachedURLs.filter(({ url }) =>
url.includes(requestedFilename)
);
const cachedSources = await cachedURLs.filter(({ url }) => {
const cachedFileName = new URL(url).pathname.split('/').reverse()[0];

return cachedFileName === requestedFilename;
});

// Find the cached version of this image that is closest to the requested
// width without going under it.
Expand Down