Skip to content
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
48 changes: 0 additions & 48 deletions layers/home/components/Carousel.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch, computed } from 'vue'
import { useHead, useImage } from '#imports'
import CarouselItemImage from './CarouselItemImage.vue'
import CarouselItemBlog from './CarouselItemBlog.vue'
import CarouselItemNews from './CarouselItemNews.vue'
Expand Down Expand Up @@ -63,53 +62,6 @@ const normalizedSlides = computed<NormalizedSlide[]>(() => normalizeSlides(props

const slidesCount = computed(() => normalizedSlides.value.length)
const lastIndex = computed(() => Math.max(0, slidesCount.value - 1))
const img = useImage()

// Preload the very first slide for faster LCP discovery
const preloadLink = computed(() => {
const slide = normalizedSlides.value[0]
if (!slide) return null

const src
= slide.type === 'image'
? slide.src
: slide.type === 'blog'
|| slide.type === 'news'
|| slide.type === 'event'
|| slide.type === 'poi'
|| slide.type === 'project'
? slide.image
: undefined

if (!src) return null

const widths = [640, 1280]
const srcset = widths
.map((w) => `${img(src, { width: w, format: 'avif', quality: 75 })} ${w}w`)
.join(', ')

return {
href: img(src, { width: 1280, format: 'avif', quality: 75 }),
imagesrcset: srcset,
imagesizes: '(max-width: 768px) 100vw, (max-width: 1024px) 100vw, 1280px'
}
})

useHead(() => {
if (!preloadLink.value) return {}
return {
link: [
{
rel: 'preload',
as: 'image',
fetchpriority: 'high',
href: preloadLink.value.href,
imagesrcset: preloadLink.value.imagesrcset,
imagesizes: preloadLink.value.imagesizes
}
]
}
})

// Whether the slide showing now is the one the user asked for. The live
// region reads it: the WAI-ARIA APG wants a carousel to announce changes the
Expand Down
5 changes: 3 additions & 2 deletions layers/home/components/CarouselItemBlog.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import {
CAROUSEL_CAPTION,
CAROUSEL_CAPTION_POSITION,
Expand Down Expand Up @@ -48,13 +49,13 @@ const dateLabel = computed(() => {
<NuxtPicture
:src="item.image"
:alt="item.alt || item.title"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
:placeholder="false"
format="avif,webp"
:loading="props.priority ? 'eager' : 'lazy'"
:preload="props.priority"
:preload="props.priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover',
fetchpriority: props.priority ? 'high' : undefined
Expand Down
5 changes: 3 additions & 2 deletions layers/home/components/CarouselItemEvent.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import {
CAROUSEL_CAPTION,
CAROUSEL_CAPTION_POSITION,
Expand Down Expand Up @@ -61,13 +62,13 @@ const dateBadgeClass
<NuxtPicture
:src="item.image"
:alt="item.alt || item.title"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
placeholder="empty"
format="avif,webp"
:loading="props.priority ? 'eager' : 'lazy'"
:preload="props.priority"
:preload="props.priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover',
fetchpriority: props.priority ? 'high' : undefined
Expand Down
5 changes: 3 additions & 2 deletions layers/home/components/CarouselItemImage.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import { CAROUSEL_SCRIM } from '../utils/carouselClasses'
// Image slide: full-bleed image with optional overlay text (note)

Expand Down Expand Up @@ -33,13 +34,13 @@ const notePositionClass
<NuxtPicture
:src="props.item.src"
:alt="props.item.alt"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
:placeholder="false"
format="avif,webp"
:loading="props.priority ? 'eager' : 'lazy'"
:preload="props.priority"
:preload="props.priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover object-center',
fetchpriority: props.priority ? 'high' : undefined
Expand Down
5 changes: 3 additions & 2 deletions layers/home/components/CarouselItemNews.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import {
CAROUSEL_CAPTION,
CAROUSEL_CAPTION_POSITION,
Expand Down Expand Up @@ -49,13 +50,13 @@ const isSvg = vComputed(() => props.item.image ? /\.svg(\?|$)/i.test(props.item.
<NuxtPicture
:src="item.image"
:alt="item.alt || item.title"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
:placeholder="false"
format="avif,webp"
:loading="props.priority ? 'eager' : 'lazy'"
:preload="props.priority"
:preload="props.priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover',
fetchpriority: props.priority ? 'high' : undefined
Expand Down
5 changes: 3 additions & 2 deletions layers/home/components/CarouselItemPoi.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import {
CAROUSEL_CAPTION,
CAROUSEL_CAPTION_POSITION,
Expand Down Expand Up @@ -54,13 +55,13 @@ const fallbackClass
v-if="item.image"
:src="item.image"
:alt="item.alt || item.title"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
:placeholder="false"
format="avif,webp"
:loading="priority ? 'eager' : 'lazy'"
:preload="priority"
:preload="priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover',
fetchpriority: priority ? 'high' : undefined
Expand Down
6 changes: 3 additions & 3 deletions layers/home/components/CarouselItemProject.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { CAROUSEL_IMAGE_SIZES } from '../utils/carouselImage'
import {
CAROUSEL_CAPTION,
CAROUSEL_CAPTION_POSITION,
Expand Down Expand Up @@ -51,13 +52,13 @@ const logoFrameClass
v-if="item.image && !item.logoOnly"
:src="item.image"
:alt="item.alt || item.title"
sizes="sm:100vw md:100vw lg:1280px"
:sizes="CAROUSEL_IMAGE_SIZES"
densities="1x 2x"
quality="75"
:placeholder="false"
format="avif,webp"
:loading="priority ? 'eager' : 'lazy'"
:preload="priority"
:preload="priority ? { fetchPriority: 'high' } : false"
:img-attrs="{
class: 'absolute inset-0 h-full w-full object-cover',
fetchpriority: priority ? 'high' : undefined
Expand All @@ -75,7 +76,6 @@ const logoFrameClass
:placeholder="false"
format="avif,webp"
:loading="priority ? 'eager' : 'lazy'"
:preload="priority"
:img-attrs="{
class: 'h-full w-full object-contain drop-shadow-lg',
fetchpriority: priority ? 'high' : undefined
Expand Down
3 changes: 3 additions & 0 deletions layers/home/utils/carouselImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// One source for the slide image's `sizes`, so the <source> and the preload
// NuxtPicture derives from it cannot disagree.
export const CAROUSEL_IMAGE_SIZES = 'sm:100vw md:100vw lg:1280px'
30 changes: 30 additions & 0 deletions tests/home/carousel-image-sizes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'

const dir = join(__dirname, '../../layers/home/components')
const items = readdirSync(dir).filter((f) => /^CarouselItem.+\.vue$/.test(f))
const SIZES_LITERAL = /sizes="sm:/

describe('carousel image sizes', () => {
it('matches a hand-written sizes literal', () => {
expect(SIZES_LITERAL.test(' sizes="sm:100vw md:100vw lg:1280px"')).toBe(true)
})

it('finds the six carousel items', () => {
expect(items).toHaveLength(6)
})

for (const file of items) {
it(`${file} takes its sizes from CAROUSEL_IMAGE_SIZES`, () => {
const src = readFileSync(join(dir, file), 'utf8')
expect(src).toContain(':sizes="CAROUSEL_IMAGE_SIZES"')
expect(SIZES_LITERAL.test(src.replace(/sizes="sm:320px md:384px"/, ''))).toBe(false)
})
}

it('Carousel.vue no longer hand-writes a preload', () => {
const src = readFileSync(join(dir, 'Carousel.vue'), 'utf8')
expect(src).not.toContain("rel: 'preload'")
})
})
45 changes: 45 additions & 0 deletions tests/home/carousel-preload.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @vitest-environment nuxt
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { describe, expect, it } from 'vitest'
import Carousel from '../../layers/home/components/Carousel.vue'
import CarouselItemProject from '../../layers/home/components/CarouselItemProject.vue'
import { CAROUSEL_IMAGE_SIZES } from '../../layers/home/utils/carouselImage'

// NuxtPicture writes its preload link only on the server, so this client-side
// environment checks what the slides hand it; the emitted <link> is checked in
// the built HTML.
const blog = (title: string) => ({
type: 'blog' as const, title, href: `/en/blog/${title}`, image: `/images/blog/${title}.webp`, alt: title
})
const project = (extra: Record<string, unknown> = {}) => ({
type: 'project' as const, title: 'ARCR', href: '/en/projects/arcr', image: '/images/projects/arcr.webp', alt: 'ARCR', ...extra
})

const pictures = (wrapper: Awaited<ReturnType<typeof mountSuspended>>) => wrapper.findAllComponents({ name: 'NuxtPicture' })

describe('Carousel image preload', () => {
it('asks for a high-priority preload on the first slide only', async () => {
const wrapper = await mountSuspended(Carousel, { props: { slides: [blog('one'), blog('two')], autoPlay: false }, route: '/en' })
const [first, second] = pictures(wrapper)
expect(first.props('preload')).toEqual({ fetchPriority: 'high' })
expect(second.props('preload')).toBeFalsy()
})

it('renders the source sizes from the shared constant', async () => {
const wrapper = await mountSuspended(Carousel, { props: { slides: [blog('one')], autoPlay: false }, route: '/en' })
expect(pictures(wrapper)[0].props('sizes')).toBe(CAROUSEL_IMAGE_SIZES)
expect(wrapper.find('source').attributes('sizes')).toBeTruthy()
})

it('preloads the main image of a project slide with high priority', async () => {
const wrapper = await mountSuspended(CarouselItemProject, { props: { item: project(), priority: true }, route: '/en' })
expect(pictures(wrapper)).toHaveLength(1)
expect(pictures(wrapper)[0].props('preload')).toEqual({ fetchPriority: 'high' })
})

it('does not preload the logo of a project slide', async () => {
const wrapper = await mountSuspended(CarouselItemProject, { props: { item: project({ logoOnly: true }), priority: true }, route: '/en' })
expect(pictures(wrapper)).toHaveLength(1)
expect(pictures(wrapper)[0].props('preload')).toBeFalsy()
})
})
Loading