Skip to content

Commit

Permalink
perf: item page head link preconnects (#2465)
Browse files Browse the repository at this point in the history
* perf: item page head link preconnects

to origins of:
* IIIF Presentation manifest
* first displayable web resource

* chore: rm redundant array init

* test: spec preconnect links

* feat: preconnect to origin of media for current page
  • Loading branch information
rwd authored Oct 31, 2024
1 parent 89552a0 commit 7dc3bae
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/portal/src/pages/item/_.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
entities: [],
error: null,
fromTranslationError: null,
headLinkPreconnect: [],
identifier: `/${this.$route.params.pathMatch}`,
iiifPresentationManifest: null,
isShownAt: null,
Expand All @@ -204,6 +205,14 @@
}
},
head() {
return {
link: this.headLinkPreconnect.map((href) => ({ rel: 'preconnect', href })),
title: this.headTitle,
meta: this.headMeta
};
},
computed: {
webResources() {
return this.media.map((webResource) => new WebResource(webResource, this.identifier));
Expand Down Expand Up @@ -394,6 +403,18 @@
this.media = item.providerAggregation.displayableWebResources;
}
const preconnects = [
this.iiifPresentationManifest,
item.providerAggregation.displayableWebResources?.[(this.$route.query.page || 1) - 1]?.about
].filter(Boolean);
for (const preconnect of preconnects) {
try {
this.headLinkPreconnect.push((new URL(preconnect)).origin);
} catch {
// URL parsing error
}
}
this.entities = this.extractEntities(edm);
this.metadata = this.extractMetadata(edm);
Expand Down
38 changes: 37 additions & 1 deletion packages/portal/tests/unit/pages/item/_.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const apiResponse = () => ({
edmRights: { def: ['http://rightsstatements.org/vocab/InC/1.0/'] },
webResources: [
{
about: 'http://example.org/image.jpeg'
about: 'http://example.org/image.jpeg',
ebucoreHasMimeType: 'image/jpeg'
}
]
}
Expand Down Expand Up @@ -450,6 +451,32 @@ describe('pages/item/_.vue', () => {
});
});
});

describe('preconnect links', () => {
it('includes first displayable web resource origin', async() => {
const wrapper = factory();

await wrapper.vm.fetch();

expect(wrapper.vm.headLinkPreconnect.includes('http://example.org')).toBe(true);
});

it('includes IIIF Presentation manifest origin', async() => {
const wrapper = factory();
const response = apiResponse();
const manifest = 'https://iiif.example.org/presentation/123/abc/manifest';
response.object.aggregations[0].webResources[0].dctermsIsReferencedBy = manifest;
response.object.aggregations[0].webResources.push({
about: manifest,
rdfType: 'http://iiif.io/api/presentation/3#Manifest'
});
wrapper.vm.$apis.record.get.resolves(response);

await wrapper.vm.fetch();

expect(wrapper.vm.headLinkPreconnect.includes('https://iiif.example.org')).toBe(true);
});
});
});

describe('on errors', () => {
Expand Down Expand Up @@ -490,6 +517,15 @@ describe('pages/item/_.vue', () => {
});
});

describe('head', () => {
it('includes preconnect links', () => {
const origin = 'https://example.org';
const wrapper = factory({ data: { headLinkPreconnect: [origin] } });

expect(wrapper.vm.head().link).toContainEqual({ rel: 'preconnect', href: origin });
});
});

describe('mounted', () => {
describe('when fetch is still pending', () => {
const $fetchState = { pending: true };
Expand Down

0 comments on commit 7dc3bae

Please sign in to comment.