We've patched a race condition in the embed script that caused redundant highlight.js library loads, improving performance when embedding multiple code snippets.
> impact
We have updated the `embed-v2.js` script to change how it detects if the `highlight.js` syntax highlighting library has already been loaded. The previous implementation checked for the existence of the `window.hljs` global variable. We've replaced this with a more robust check that queries the DOM for the `highlight.js` script tag itself, ensuring we only append the script to the page once.
A race condition occurred when multiple emgithub snippets were embedded on the same page. Each embed script would independently check `if (!window.hljs)`. Before the first script could finish downloading, parsing, and creating the `hljs` global, the other scripts would also pass this check, triggering multiple, simultaneous downloads of the same library. This created unnecessary network requests and could slow down page rendering.
This fix significantly improves the loading performance and efficiency of pages that feature multiple emgithub code snippets. By preventing redundant script downloads, we reduce bandwidth consumption and eliminate unnecessary script parsing, leading to faster page loads and a smoother experience for end-users. Your existing embeds will automatically benefit from this improvement with no changes required on your part.
> Try this now
try this
# We've updated the script loading logic in `embed-v2.js` to be more resilient.
# The previous method could fail when multiple embeds were loaded at once.
# ---
# BEFORE: Checking for a global variable.
# This created a race condition, as `window.hljs` only exists *after*
# the script has fully loaded and executed.
# ---
# if (!window.hljs) {
# const script = document.createElement('script');
# script.src = '.../highlight.min.js';
# document.head.appendChild(script);
# }
# ---
# AFTER: Checking for the script tag in the DOM.
# This check is positive as soon as the script is appended,
# preventing other embeds from adding it again.
# ---
if (!document.querySelector('script[src*="highlight.min.js"]')) {
const script = document.createElement('script');
script.src = '.../highlight.min.js';
// This script will now only be appended once per page load.
document.head.appendChild(script);
}