Skip to content

Stache is refreshed too late #10344

@aerni

Description

@aerni

Bug description

I've got a CollectionSavedListener that is triggered on Statamic\Events\CollectionSaved. In the listener, I'm querying the collection's entries. The collection is configured for multiple sites in a multi-site setup. If you change the collection's sites and save the collection, the queried entries in the listener will not honor the newly configured sites, but still contain the entries of the previously selected sites. If you save the collection a second time, the queried entries are correct.

Here's a quick video:

CleanShot.2024-06-20.at.15.16.41.mp4

A workaround is to manually refresh the Stache in the listener before querying the entries. But this feels rather dirty.

How to reproduce

  1. Create a collection and configure it for multiple sites
  2. Create a few entries in both sites
  3. Add the following event listener
  4. Change the collection's sites and save
  5. Check the output in Ray and notice that the queried entries are wrong
<?php

namespace App\Listeners;

use Statamic\Events\CollectionSaved;
use Statamic\Facades\Stache;

class CollectionSavedListener
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(CollectionSaved $event): void
    {
        // Manually refreshing the Stache fixes the issue.
        // Stache::refresh();

        ray($event->collection->queryEntries()->get());
    }
}

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 11.11.1
PHP Version: 8.3.8
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: statamic-collection-stache-query-bug.test
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Statamic
Addons: 0
Sites: 2 (Statamic, German)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.9.0 PRO

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions