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

perf(recipe): add srcset #1165

Merged
merged 4 commits into from Jun 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 27 additions & 12 deletions components/Recipe/Recipe.vue
Expand Up @@ -30,18 +30,21 @@
style="padding-top: 56.25%;"
/>
</vue-plyr>
<div v-else-if="imageData && imageData.url">
<b-img-lazy
:src="imageData.url"
<div v-else-if="imageData && imageData.src">
<b-img
:xsrc="imageData.src"
:srcset="imageData.srcset"
:alt="name"
throttle="100"
itemprop="image"
:width="640"
:height="360"
fluid
xfluid-grow
fluid-grow
blank-src
center
sizes="(max-width: 1140px) 100vw, 1140px"
block
/>
</div>
<!-- <p>Author: {{ author }}</p> -->
Expand Down Expand Up @@ -145,7 +148,7 @@

<nutrition-fact-table v-if="nutrition" v-bind="nutrition" class="my-4" />

<section class="mb-2">
<section class="mb-2 d-print-none">
<h2 class="h4">Tags</h2>
<div class="list-group-flush">
<keywords
Expand All @@ -156,7 +159,7 @@
</div>
</section>

<section v-if="sameAs">
<section v-if="sameAs" class="d-print-none">
<h2 class="h4">References</h2>
<div class="list-group-flush">
<span
Expand Down Expand Up @@ -233,9 +236,21 @@ export default {
imageData() {
function cloudinaryify(image) {
if (!image.startsWith('https://res.cloudinary.com')) {
return `https://res.cloudinary.com/pocketpasta/image/fetch/w_640,h_360,ar_16:9,c_fill,f_auto,q_auto/${image}`;
return {
src: `https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_640,h_360,ar_16:9,c_fill,f_auto,q_auto/${image}`,
srcset: [
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_320,h_180,ar_16:9,c_fill,f_auto,q_auto/${image} 320w`,
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_640,h_360,ar_16:9,c_fill,f_auto,q_auto/${image} 640w`,
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_768,h_432,ar_16:9,c_fill,f_auto,q_auto/${image} 768w`,
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_1024,h_576,ar_16:9,c_fill,f_auto,q_auto/${image} 1024w`,
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_1280,h_720,ar_16:9,c_fill,f_auto,q_auto/${image} 1280w`,
`https://res.cloudinary.com/pocketpasta/image/fetch/fl_progressive/w_1366,h_768,ar_16:9,c_fill,f_auto,q_auto/${image} 1366w`,
],
};
} else {
return image;
return {
src: image,
};
}
}
if (this.image) {
Expand All @@ -244,12 +259,12 @@ export default {
this.image !== null &&
!Array.isArray(this.image)
) {
const { url, width, height } = this.image;
return { url: cloudinaryify(url), width, height };
const { url } = this.image;
return cloudinaryify(url);
} else if (Array.isArray(this.image)) {
return { url: cloudinaryify(this.image[0]) };
return cloudinaryify(this.image[0]);
} else {
return { url: cloudinaryify(this.image) };
return cloudinaryify(this.image);
}
} else {
return null;
Expand Down
10 changes: 3 additions & 7 deletions test/components/Recipe.spec.js
Expand Up @@ -97,17 +97,15 @@ describe('Recipe', () => {
...recipe,
image,
});
expect(wrapper.vm.imageData).toEqual(image);
expect(wrapper.vm.imageData).toBeTruthy();
});

test('imageData convet to cloudinaty url', () => {
const wrapper = factory();
wrapper.setProps({
...recipe,
});
expect(wrapper.vm.imageData).toEqual({
url: `https://res.cloudinary.com/pocketpasta/image/fetch/w_640,h_360,ar_16:9,c_fill,f_auto,q_auto/${recipe.image}`,
});
expect(wrapper.vm.imageData).toBeTruthy();
});

test('imageData handle array', () => {
Expand All @@ -116,9 +114,7 @@ describe('Recipe', () => {
...recipe,
image: ['https://res.cloudinary.com/pocketpasta/image/fetch/'],
});
expect(wrapper.vm.imageData).toEqual({
url: `https://res.cloudinary.com/pocketpasta/image/fetch/`,
});
expect(wrapper.vm.imageData).toBeTruthy();
});

test('truncate', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/components/__snapshots__/Recipe.spec.js.snap
Expand Up @@ -33,7 +33,7 @@ exports[`Recipe renders properly 1`] = `
</b-col-stub>
</b-row-stub>
<!---->
<section class=\\"mb-2\\">
<section class=\\"mb-2 d-print-none\\">
<h2 class=\\"h4\\">Tags</h2>
<div class=\\"list-group-flush\\">
<keywords-stub tags=\\"\\" class=\\"list-group-item\\"></keywords-stub>
Expand Down