We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20197
    • 9 Posts
    Hola,

    I created a plugin to generate a table of contents and I have the feeling that it could be slow, because of bad caching.

    Here is an example:
    $e = &$modx->Event;
    switch ($e->name) 
    {
    	case "OnWebPagePrerender":
    		$output =& $modx->resource->_output;
    		// Do something with the output
    


    What would be the right System Event to change the content before it is cached and all snippets, chunks, tv's... are processed?

    Thank you very much for your help!


    Cheers,

    Thomas
      • 3749
      • 24,544 Posts
      If you don't need it to be dynamic, the best event would be OnDocFormSave or OnBeforeDocFormSave, so the changes will be saved to the database, then cached on the first visit. The slowdown will only happen once when the page is saved in the Manager, instead of on every single page visit.

      Changing to another event that's part of the rendering process probably won't save you any time at all. The time it takes for your code to run will be added on every page load regardless of which event you attach the plugin to.

      If you need the TOC to be dynamic, you'll be much better off with a snippet in the template rather than a plugin. The snippet's output and the page itself can be cached. Every time you add a new resource, the cache will be cleared when you save it, so the output should always be up to date and your code will only run once when a new resource is created.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 20197
        • 9 Posts
        Hi BobRay,

        thank you for your extended answer. I created 3 plugins:

        1) Markdown (I'm writing in Markdown) and generate HTML
        2) Generate Table of Content from HTML (Because its more flexible than from markdown e.g. when html content is used)
        3) Generate a second navigation for anchors from HTML

        Because of Markdown, I'm not sure if a snippet could work and if docsave can be used. I don't want to change the original content. [ed. note: MoonMaker last edited this post 9 years, 5 months ago.]
          • 3749
          • 24,544 Posts
          I think what I would do in your case, if you're not satisfied with the page-load speeds, is extend modResource with ClassExtender and store the TOC and HTML in custom fields -- updating them whenever the resource is saved. Then retrieve those fields in a cached snippet so the snippet will only run when the page changes and the cache is cleared.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 20197
            • 9 Posts
            Quote from: BobRay at Dec 13, 2014, 07:03 PM
            I think what I would do in your case, if you're not satisfied with the page-load speeds, is extend modResource with ClassExtender and store the TOC and HTML in custom fields -- updating them whenever the resource is saved. Then retrieve those fields in a cached snippet so the snippet will only run when the page changes and the cache is cleared.

            Thanks, I will test it!

            Cheers,

            Thomas