Skip to content

Commit

Permalink
Mitigate syntax errors in older browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Jul 13, 2021
1 parent 4cff3aa commit 8fd0fd5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/js/citeproc-wrapper.js
Expand Up @@ -338,6 +338,26 @@ const getCSL = async () => {
});
};

const getCiteprocRSLoader = async () => {
// Loading citeproc-rs involves using dynamic module import (await import(...)) which will
// trigger a syntax error while the code is being parsed in some older browsers even though zbib
// would work perfectly fine in such browser with citeprocJS. For this reason we host this code
// separately and load it here only in a scenario where citeproc-rs is to be used.
if('getCiteprocRS' in window) {
return Promise.resolve(window.getCiteprocRS);
}

return new Promise((resolve, reject) => {
load('/static/js/citeproc-rs-loader.js', { type: 'module' }, err => {
if (err) {
reject(err);
} else {
resolve(window.getCiteprocRS);
}
});
});
}

CiteprocWrapper.new = async ({ style, format = 'html', lang = null, wrap_url_and_doi = false }, useLegacy = null) => {
lang = lang ? lang : window ? window.navigator.userLanguage || window.navigator.language : null;
useLegacy = useLegacy === null ? !isWasmSupported : useLegacy;
Expand All @@ -351,7 +371,8 @@ CiteprocWrapper.new = async ({ style, format = 'html', lang = null, wrap_url_and
return new CiteprocWrapper(true, CSL, { style, format, lang, wrap_url_and_doi });
} else {
if(!Driver) {
const { default: init, Driver: CreateDriver } = await import("/static/js/citeproc-rs/citeproc_rs_wasm.js");
const citeprocLoader = await getCiteprocRSLoader();
const { init, CreateDriver } = await citeprocLoader();
Driver = CreateDriver;
await init();
}
Expand Down
4 changes: 4 additions & 0 deletions src/static/js/citeproc-rs-loader.js
@@ -0,0 +1,4 @@
window.getCiteprocRS = async () => {
const { default: init, Driver: CreateDriver } = await import('/static/js/citeproc-rs/citeproc_rs_wasm.js');
return { init, CreateDriver };
}

0 comments on commit 8fd0fd5

Please sign in to comment.