Skip to content

Commit

Permalink
Merge pull request #82 from casper-network/seo-improvements
Browse files Browse the repository at this point in the history
MODIFIED: better alt and img dimension handling
  • Loading branch information
sd-ditoy authored Feb 7, 2024
2 parents 6a0a256 + 6053464 commit 5a0dc7f
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 96 deletions.
148 changes: 148 additions & 0 deletions src/components/MediaImage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<template>
<img
:src="computedUrl"
:width="computedWidth"
:height="computedHeight"
:alt="computedAlt"
/>
</template>

<script>
import config from '@/directus/config';
const { API_URL } = config;
export default {
name: 'MediaImage',
components: {
},
//---------------------------------------------------
//
// Properties
//
//---------------------------------------------------
props: {
asset: {
type: String,
default: null,
},
alt: {
type: String,
default: null,
},
width: {
type: [Number, String],
default: null,
},
height: {
type: [Number, String],
default: null,
},
},
//---------------------------------------------------
//
// Data model
//
//---------------------------------------------------
data() {
return {
metadata: {},
};
},
//---------------------------------------------------
//
// Computed Properties
//
//---------------------------------------------------
computed: {
computedUrl() {
const { asset } = this;
if (!this.isAbsoluteUrl) {
return `${API_URL}/assets/${asset}`;
}
if (this.isAbsoluteUrl) {
return asset;
}
return null;
},
isAbsoluteUrl() {
const { asset } = this;
return asset.indexOf('/') !== -1;
},
computedAlt() {
const { metadata } = this;
return this.alt || metadata?.description || metadata?.title;
},
computedWidth() {
const { metadata } = this;
return this.width || metadata?.width || 'auto';
},
computedHeight() {
const { metadata } = this;
return this.height || metadata?.height || 'auto';
},
},
//---------------------------------------------------
//
// Watch Properties
//
//---------------------------------------------------
watch: {
asset(newval, oldval) {
if (newval !== oldval) {
this.retrieveMetadata();
}
},
},
//---------------------------------------------------
//
// Filter Properties
//
//---------------------------------------------------
// filters: {},
//---------------------------------------------------
//
// Validation Properties
//
//---------------------------------------------------
// validations: {},
//---------------------------------------------------
//
// Vue Lifecycle
//
//---------------------------------------------------
// beforeCreate() {},
async created() {
this.retrieveMetadata();
},
// beforeMount() {},
// render(h) { return h(); },
// mounted() {},
// beforeUpdate() {},
// updated() {},
// beforeDestroy() {},
// destroyed() {},
//---------------------------------------------------
//
// Methods
//
//---------------------------------------------------
methods: {
async retrieveMetadata() {
this.metadata = {};
const { asset } = this;
if (asset && !this.isAbsoluteUrl) {
const response = await fetch(`${API_URL}/files/${asset}`);
const { data } = await response.json();
this.metadata = data;
}
},
//----------------------------------
// Event Handlers
//----------------------------------
},
};
</script>
<style lang="scss">
</style>
19 changes: 11 additions & 8 deletions src/components/atoms/Card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
<div>
<div class="card">
<a :href="`${cardData.url}`" target="_blank">
<img :src="cardImage" alt="">
<MediaImage
:asset="cardImage"
/>
</a>
<div class="card-header">
<img :src="logoImage" alt="" class="avatar">
<MediaImage
:asset="logoImage"
class="avatar"
/>
<div>
<a :href="`${cardData.url}`" target="_blank">
<h5>{{cardData.title}}</h5>
Expand All @@ -20,13 +25,11 @@
</template>

<script>
import config from '@/directus/config';
const { API_URL } = config;
import MediaImage from '@/components/MediaImage.vue';
export default {
name: 'Card',
components: {},
components: { MediaImage },
//---------------------------------------------------
//
// Properties
Expand All @@ -50,10 +53,10 @@ export default {
//---------------------------------------------------
computed: {
cardImage() {
return (this.cardData.image) ? `${API_URL}/assets/${this.cardData.image}` : '/img/mesh6.webp';
return (this.cardData.image) ? this.cardData.image : '/img/mesh6.webp';
},
logoImage() {
return (this.cardData.image) ? `${API_URL}/assets/${this.cardData.logo}` : '/img/mesh1.webp';
return (this.cardData.image) ? this.cardData.logo : '/img/mesh1.webp';
},
},
//---------------------------------------------------
Expand Down
22 changes: 15 additions & 7 deletions src/components/atoms/CarouselItem.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<template>
<div class="carousel-item">
<router-link :to="cardData.link.url" v-if="cardData.link.type === 'int'">
<img loading="lazy" :src="postImage" alt="">
<MediaImage
:asset="postImage"
width="378"
height="213"
loading="lazy"
/>
</router-link>
<a :href="cardData.link.url" v-if="cardData.link.type === 'ext'" target="_blank">
<img loading="lazy" :src="postImage" alt="">
<MediaImage
:asset="postImage"
width="378"
height="213"
loading="lazy"
/>
</a>
<router-link :to="cardData.link.url" v-if="cardData.link.type === 'int'">
<p class="h4" v-html="cardData.title"></p>
Expand All @@ -24,13 +34,11 @@
</template>

<script>
import config from '@/directus/config';
const { API_URL } = config;
import MediaImage from '@/components/MediaImage.vue';
export default {
name: 'CarouselItem',
components: {},
components: { MediaImage },
//---------------------------------------------------
//
// Properties
Expand All @@ -54,7 +62,7 @@ export default {
//---------------------------------------------------
computed: {
postImage() {
return `${API_URL}/assets/${this.cardData.image}`;
return this.cardData.image;
},
},
//---------------------------------------------------
Expand Down
15 changes: 9 additions & 6 deletions src/components/atoms/ClassicTeaser.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<template>
<div class="outer-wrap">
<div class="inner-wrap">
<img v-if="teaserImage" :src="teaserImage" alt="">
<MediaImage
v-if="teaserImage"
:asset="teaserImage"
width="562"
height="326"
/>
<div>
<h3>{{blockData.title}}</h3>
<p v-html="blockData.content"></p>
Expand All @@ -15,13 +20,11 @@
</template>

<script>
import config from '@/directus/config';
const { API_URL } = config;
import MediaImage from '@/components/MediaImage.vue';
export default {
name: 'ClassicTeaser',
components: {},
components: { MediaImage },
//---------------------------------------------------
//
// Properties
Expand All @@ -47,7 +50,7 @@ export default {
teaserImage() {
const media = this.blockData?.media || null;
if (media) {
return `${API_URL}/assets/${media}`;
return media;
}
return null;
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/atoms/PartnerCard.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="partnerCard">
<img src="/img/casperLabsLogo.png" alt="">
<img src="/img/casperLabsLogo.png" alt="Casper Labs Logo">
</div>
</template>

Expand Down
28 changes: 21 additions & 7 deletions src/components/atoms/PostItem.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<template>
<article>
<div v-if="type === 'news'" class="img-container">
<router-link :to="`/${$i18n.locale}/news/${postItemData.content[0].slug}`"><img :src="postImage" loading="lazy" alt=""></router-link>
<router-link :to="`/${$i18n.locale}/news/${postItemData.content[0].slug}`">
<MediaImage
:asset="postImage"
width="500"
height="250"
loading="lazy"
/>
</router-link>
</div>
<div v-if="type === 'casestudies'" class="img-container">
<router-link :to="`/${$i18n.locale}/case-studies/${postItemData.slug}`"><img :src="postImage" loading="lazy" alt=""></router-link>
<router-link :to="`/${$i18n.locale}/case-studies/${postItemData.slug}`">
<MediaImage
:asset="postImage"
width="500"
height="250"
loading="lazy"
/>
</router-link>
</div>
<div class="content" :class="`postType-${type}`">
<div class="time" v-if="type === 'news'">
Expand Down Expand Up @@ -37,15 +51,14 @@
import dayjs from 'dayjs';
import RelativeTime from 'dayjs/plugin/relativeTime';
import SVGClock from '@/assets/svg/icon-clock.svg?inline';
import config from '@/directus/config';
const { API_URL } = config;
import MediaImage from '@/components/MediaImage.vue';
dayjs.extend(RelativeTime);
export default {
name: 'PostItem',
components: {
MediaImage,
SVGClock,
},
//---------------------------------------------------
Expand Down Expand Up @@ -80,9 +93,9 @@ export default {
},
postImage() {
if (this.type === 'news') {
return `${API_URL}/assets/${this.postItemData.content[0].image}`;
return this.postItemData.content[0].image;
}
return `${API_URL}/assets/${this.postItemData.image}`;
return this.postItemData.image;
},
},
//---------------------------------------------------
Expand Down Expand Up @@ -184,6 +197,7 @@ article {
img {
max-width: 500px;
height: auto;
aspect-ratio: 16 / 8;
@include breakpoint('sm') {
Expand Down
16 changes: 9 additions & 7 deletions src/components/blocks/Glider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
<div class="glide__track" data-glide-el="track">
<ul class="glide__slides">
<li class="glide__slide" v-for="(item, i) in glideData" :key="`glide-slide-${i}`">
<img loading="lazy" width="1024" height="768" :src="slideImage(item)" alt="">
<MediaImage
:asset="item.id"
loading="lazy"
width="1024"
height="768"
:alt="item.text"
/>
<div v-if="item.text" v-html="item.text"></div>
</li>
</ul>
Expand All @@ -29,13 +35,12 @@
import Glide from '@glidejs/glide/dist/glide';
import ChevronLeft from '@/assets/svg/icon-chevron-left.svg?inline';
import ChevronRight from '@/assets/svg/icon-chevron-right.svg?inline';
import config from '@/directus/config';
const { API_URL } = config;
import MediaImage from '@/components/MediaImage.vue';
export default {
name: 'Glider',
components: {
MediaImage,
ChevronLeft,
ChevronRight,
},
Expand Down Expand Up @@ -105,9 +110,6 @@ export default {
//
//---------------------------------------------------
methods: {
slideImage(val) {
return `${API_URL}/assets/${val.id}`;
},
//----------------------------------
// Event Handlers
//----------------------------------
Expand Down
Loading

0 comments on commit 5a0dc7f

Please sign in to comment.