getgrav / grav-premium-issues

Official Grav Premium Issues repository to report problems or ask questions regarding the Premium products offered.
https://getgrav.org/premium
7 stars 2 forks source link

[algolia-pro] Can't create anchor links #370

Open Sogl opened 1 year ago

Sogl commented 1 year ago

Just updated Algolia Pro plugin from 1.0.8 to 1.0.12 and now anchor navigation (targets) is missing when reindexing. I did not change the settings of the plugin. After removing the indexes on the production and localhost (they are different) and indexing a few times, I found that when I use Xdebug, the anchors are sent perfectly. Without debugging they simply aren't there...

Tried much combinations: delete index in Algolia, clear all cache, reindex in admin, flush index in admin, index in CLI. Found a workaround on the localhost: 1. Delete index in Algolia dashboard. 2. Index in Admin (still no anchors). 3. Index in CLI (anchors appear). But for some reason it does not work in production.

After further debugging, I found the problem in this piece of code (GravPageSearch.php line 402):

foreach ($blocks as $block) {
    $block_data = [];
    $block_content = $block['content'] ?? '';

    if (isset($block['tag'], $block['header'])) {
        $block_data['objectType'] = 'header';
        $block_data['headers'][$block['tag']][] = $block['header'];
        $block_data['subtitle'] = $block['header'];
        $block_data['summary'] = $this->getFirstWords($block_content, 50);

        //HERE
        if (!empty($block['id'])) {
            $block_data['url'] = $page_data['url'] . '#' . $block['id'];
        }
    }

When I click on Reindex Now in the Admin panel, the h2 tag does not contain an id, so the anchor is not passed. In CLI indexing the id appears, so it is added to the index.

But this solution does not work in production, anchors are not there.

Sogl commented 1 year ago

I use MarkupFixer from the TOC plugin as a temporary solution:

$content = trim($page->content());
//for TOC search
$markup_fixer = new MarkupFixer();
$content = $markup_fixer->fix($content, $this->getAnchorOptions($page));

Working steps:

  1. Delete Algolia index.
  2. Clear Grav cache.
  3. Reset Index button.

Anchors in search are now in place!

But now the process of rebuilding indexes eats a lot more memory and time, especially Reset Index.

rhukster commented 1 year ago

This is quite strange.

  1. Makes no sense that it works with xdebug enabled, but doesn't when disabled
  2. Nothing has changed related to anchors between 1.0.8 and latest 1.0.12
  3. You always needed PageTOC plugin to get anchors to work (at least you needed anchors to be set by something), it was never built-in to Aloglia plugin itself. (https://getgrav.org/premium/algolia-pro/docs/backend#basic-usage-tutorial)

If you have PageTOC enabled, it should default to add anchors, which in turn should be picked up by AgoliaPro.

Sogl commented 1 year ago

Yes, it has nothing to do with Xdebug and version of Algolia plugin (I compared the files).

But my PageTOC plugin is always enabled. Any ideas? I can send configuration files etc if needed.

I use my own class (based on GravPageSearch) to collect Flex Pages and Pages into one index. Last version is here: https://gist.github.com/Sogl/78dce78b254b39a6678f759b3ff981b7#file-gravflexpagesearch-php

rhukster commented 1 year ago

Just sounds like an opcode cache issue to me.. nothing i would be able to replicate

Sogl commented 1 year ago

This has nothing to do with the version of php, does it?

What is the right way to clear the opcode cache in this case?

rhukster commented 1 year ago

usually restarting php process (php-fpm) clears it.