Fixes #8 - Thumbnail not loading when media is private #9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Mirador Viewer uses img tags for thumbnails with direct links to the iiif server, so requests made to retrieve the thumbnail images do not contain authorization and token headers. This causes the thumbnails to not load when the media is private.
This PR uses a service worker to add the authorization and token headers to the img requests.
Resources
1. Creating the service worker file:
See: https://www.twelve21.io/how-to-access-images-securely-with-oauth-2-0/
js/service_worker.js
2. Setting the 'Service-Worker-Allowed' header
By default, the scope of the service worker must be within the directory containing the service worker script. (The scope refers to the pages making the requests that are then intercepted by the service worker). However, in order to set the scope to
/
, the service_worker.js file would need to be moved into the root directory of the site. To set the scope outside of the script's directory, theService-Worker-Allowed
header needs to be added to the HTTP response when the service worker script is requested. This was done by creating a controller that provides the script and appends the header in its response, and then creating a/islandora_mirador_service_worker
route which is used as the script URL when registering the service worker.See: https://drupal.stackexchange.com/questions/307079/how-to-return-service-worker-with-custom-http-header-from-custom-module
src/Controller/ServiceWorkerController.php
islandora_mirador.routing.yml
3. Registering the service worker
See: https://www.twelve21.io/how-to-access-images-securely-with-oauth-2-0/
js/mirador_viewer.js