Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language component makes my page freeze for 20 sec #2954

Closed
chjoergensen opened this issue Jun 15, 2021 · 8 comments
Closed

Language component makes my page freeze for 20 sec #2954

chjoergensen opened this issue Jun 15, 2021 · 8 comments
Labels

Comments

@chjoergensen
Copy link

I am running a Ruby on Rails app which uses Webpack, but Prism is not loaded through it.

I have an issue with the browser freezing during syntax highlighting.

  1. My page loads prism-core and prism-autoloader from a CDN:
<head>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/components/prism-core.min.js'></script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/autoloader/prism-autoloader.min.js'></script>
  <link href='https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism-okaidia.min.css' rel='stylesheet'>
</head>
  1. A pre and code element is in the body.
<body>
  <pre><code class="language-json">{"foo": "bar"}</code></pre>
</body>
  1. I visit my page in a browser. The entire page freezes for about 20 sec, after which the syntax highlighting happens.

  2. I now execute Prism.highlightAll(); in my Google Chrome browser console. This, again, causes the page to freeze for about 20 sec, after which undefined is returned by the function. Since highlighting was already applied, nothing visible happens.

  3. I now add the following:

<script>
  window.Prism = window.Prism || {};
  window.Prism.manual = true;
</script>
  1. I reload the page while refreshing the cache. The code element is now unstyled (not even a black background), but no freeze. When I run the Prism.highlightAll(); the generic styling (white text on black background) happens in immediately, and the function return undefined right away. But then the freeze kicks in and only 20 secs later the JSON syntax highlighting happens.
  • Removing the pre and code elements turns the page back to normal.
  • Switching internet connection seems to have no effect.
  • I am unable to reproduce the issue on jsFiddle.
  • Removing the prism-autoloader plugin and adding the prism-json plugin instead does not change anything. Removing both of them does bring the page back to normal (now with generic styling).

Any help would be much appreciated.

@RunDevelopment
Copy link
Member

Strange. The text is too short to be a problem, so it's likely some DOM issue. Could you profile the highlighting process, so we get some more information?

@chjoergensen
Copy link
Author

chjoergensen commented Jun 16, 2021

Your hint towards the text length was very useful. I had replaced the original and very long text with sample code before submitting this issue. Seems this caused the freeze. It may be possible to get more out of this profiling tool, but the most useful I could derive from it was the summary:

39 ms  Loading
25597 ms  Scripting
1585 ms  Rendering
29 ms  Painting
172 ms  System
297455 ms  Idle
324878 ms  Total

I played around some more, and found an approach that does not cause a freeze, even with the long version text.

<div class="json-pre-container">
  <div class="json-source" style="display: none;">{"long": "text"}</div>
  <pre><code></code></pre>
</div>

<script>
  $(document).on('turbo:load', function() {
    $(".json-pre-container").each(function() {
      var pre_code = $(this).find("pre code");
      var json_source = $(this).find(".json-source").text();
      var source_json = JSON.stringify(JSON.parse(json_source), undefined, 2);
      pre_code.addClass("language-json").html(source_json);
      pre_code.text(source_json);
    });

    return Prism.highlightAll();
  })
</script>

Not sure why it helps, or if it's good practice.

@chjoergensen
Copy link
Author

He, seems it didn't fix it after all. For a while it did. I've been there a few times.

@RunDevelopment
Copy link
Member

Could you share the profile itself? Please save the profile (see image below) and attach the file to your comment.

image

@chjoergensen
Copy link
Author

Seems the file can not be attached. Instead I've made it available here.

The setup while profiling was as described in the steps 1-2 in the original issue description.

@RunDevelopment
Copy link
Member

Let me guess: the JSON you highlight has a string like this: "\"\"\"\"\"\"\"\"...\"\"\"".

I now know the problem. I'll fix it.

@RunDevelopment
Copy link
Member

My apologies, the issue (#2689) has already been fixed and will be released in the next version.

@chjoergensen
Copy link
Author

Awesome man. And yes you are absolutely right, the JSON has a key/value pair, where the value is the source code of a website, e.g.:

{"key": "<!DOCTYPE HTML> <!--[if lt IE 7]><html class=\"no-js lt-ie9 lt-ie8 lt-ie7\" lang=\"lv-lv\"><![endif]--> <!--[if IE 7]><html class=\"no-js lt-ie9 lt-ie8 ie7\" lang=\"lv-lv\"><![endif]--> <!--[if IE 8]><html class=\"no-js lt-ie9 ie8\" lang=\"lv-lv\"><![endif]--> <!--[if gt IE 8]><html class=\"no-js ie9\" lang=\"lv-lv\"><![endif]--><html lang=\"lv-lv\" class=\"menu-effect\"><head><meta charset=\"utf-8\" /><title>..."}

Lots of escaped quotes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants