Commit 8e67e807 authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Fix JS browser compatibility on dev

Because the code in `dev-server/static/scripts/util.js` is not
transpiled by Babel, it needs to be compatible with all the browsers
versions in `browserslist` (`package.json`).

Optional chaining `(?.)` is for example not support in Chrome versions <
80.

This is only an issue when testing `localhost:3000` on these supported,
but older browser versions.
parent 91957f48
...@@ -29,11 +29,12 @@ function renderScript(context) { ...@@ -29,11 +29,12 @@ function renderScript(context) {
const scriptTemplate = ` const scriptTemplate = `
{{{hypothesisConfig}}} {{{hypothesisConfig}}}
<script type="module"> <script src="/scripts/util.js"></script>
import { loadClient } from '/scripts/util.js'; <script>
(function(){
const clientUrl = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname); let clientUrl = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname);
loadClient(clientUrl); loadClient(clientUrl);
})();
</script> </script>
`; `;
return Mustache.render(scriptTemplate, context); return Mustache.render(scriptTemplate, context);
......
// Code for controls on the dev server homepage. /**
* Because the code in this file is not transpiled by Babel, it must be compatible
* with all the supported browsers version (see `browserlist` in `package.json`)
* without transpilation. Do not include latest EcmaScript features as these
* will cause exceptions while working on dev (`localhost:3000`) on slightly
* older, yet supported browser versions.
*/
import { activeClientUrl, loadClient, unloadClient } from './util.js'; // Code for controls on the dev server homepage.
const toggleClientButton = document.querySelector('.js-toggle-client'); (function () {
toggleClientButton.textContent = 'Unload client'; let toggleClientButton = document.querySelector('.js-toggle-client');
const clientUrl = activeClientUrl; toggleClientButton.textContent = 'Unload client';
let clientUrl = activeClientUrl;
toggleClientButton.onclick = () => { toggleClientButton.onclick = () => {
if (activeClientUrl) { if (activeClientUrl) {
unloadClient(); unloadClient();
toggleClientButton.textContent = 'Load client'; toggleClientButton.textContent = 'Load client';
...@@ -14,5 +21,5 @@ toggleClientButton.onclick = () => { ...@@ -14,5 +21,5 @@ toggleClientButton.onclick = () => {
loadClient(clientUrl); loadClient(clientUrl);
toggleClientButton.textContent = 'Unload client'; toggleClientButton.textContent = 'Unload client';
} }
}; };
})();
/**
* Because the code in this file is not transpiled by Babel, it must be compatible
* with all the supported browsers version (see `browserlist` in `package.json`)
* without transpilation. Do not include latest EcmaScript features as these
* will cause exceptions while working on dev (`localhost:3000`) on slightly
* older, yet supported browser versions.
*/
/** @type {string|null} */ /** @type {string|null} */
export let activeClientUrl; let activeClientUrl;
/** /**
* Load the Hypothesis client into the page. * Load the Hypothesis client into the page.
* *
* @param {string} clientUrl * @param {string} clientUrl
*/ */
export function loadClient(clientUrl) { function loadClient(clientUrl) {
const embedScript = document.createElement('script'); let embedScript = document.createElement('script');
embedScript.src = clientUrl; embedScript.src = clientUrl;
document.body.appendChild(embedScript); document.body.appendChild(embedScript);
activeClientUrl = clientUrl; activeClientUrl = clientUrl;
...@@ -18,8 +26,11 @@ export function loadClient(clientUrl) { ...@@ -18,8 +26,11 @@ export function loadClient(clientUrl) {
* *
* This uses the same method as the browser extension does to deactivate the client. * This uses the same method as the browser extension does to deactivate the client.
*/ */
export function unloadClient() { function unloadClient() {
const annotatorLink = document.querySelector('link[type="application/annotator+html"]'); let annotatorLink = document.querySelector('link[type="application/annotator+html"]');
annotatorLink?.dispatchEvent(new Event('destroy'));
if (annotatorLink) {
annotatorLink.dispatchEvent(new Event('destroy'));
}
activeClientUrl = null; activeClientUrl = null;
} }
...@@ -49,6 +49,6 @@ ...@@ -49,6 +49,6 @@
<li><a href="https://h.readthedocs.io/en/latest/api/">Hypothesis API documentation</a></li> <li><a href="https://h.readthedocs.io/en/latest/api/">Hypothesis API documentation</a></li>
</ul> </ul>
{{{hypothesisScript}}} {{{hypothesisScript}}}
<script type="module" src="/scripts/index.js"></script> <script src="/scripts/index.js"></script>
</body> </body>
</html> </html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment