Thanks to visit codestin.com
Credit goes to github.com

Skip to content

allow marked middlewares to run when page is cached#12497

Open
gutig wants to merge 3 commits into
concretecms:9.4.xfrom
gutig:feature/issue-12344-cache-middleware
Open

allow marked middlewares to run when page is cached#12497
gutig wants to merge 3 commits into
concretecms:9.4.xfrom
gutig:feature/issue-12344-cache-middleware

Conversation

@gutig
Copy link
Copy Markdown
Contributor

@gutig gutig commented Mar 13, 2025

This allows middlewares to run when the page is cached, so as an example you can add in the app.middleware config the following

<?php

return [
    'middleware' => [
        [
            'priority' => 1,
            'class' => \Application\Http\Middleware\TestMiddleware::class,
            'run_during_cache' => true
        ],
    ],
];

and then create the middleware as normal e.g

<?php

namespace Application\Http\Middleware;

use Concrete\Core\Routing\Redirect;
use Concrete\Core\Support\Facade\Application;
use Symfony\Component\HttpFoundation\Request;
use Concrete\Core\Http\Middleware\MiddlewareInterface;
use Concrete\Core\Http\Middleware\DelegateInterface;

class TestMiddleware implements MiddlewareInterface
{
    public function process(Request $request, DelegateInterface $frame)
    {
        if ($request->get('check_cache')) {
            $response = Redirect::to('/some-page');
            $response->send();
            Application::getFacadeApplication()->shutdown();
        }

        return $frame->next($request);
    }
}

Resolves this issue I raised #12344

*/
$stack = $this->app->make(StackInterface::class);
foreach ($middlewareCache as $middleware) {
if (is_array($middleware)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the elements of $middlewareCache are always arrays, this check is superfluous, isn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point have updated

foreach ($middlewareConfig as $middleware) {
if (is_array($middleware)) {
$runBeforeCache = isset($middleware['run_during_cache']) && $middleware['run_during_cache'] === true;
if ($runBeforeCache) {
Copy link
Copy Markdown
Contributor

@mlocati mlocati Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply

if (!empty($middleware['run_during_cache'])) {

this checks both that $middleware['run_during_cache'] is set and that it does not contain a falsy value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point have updated

@gutig
Copy link
Copy Markdown
Contributor Author

gutig commented Sep 19, 2025

added some functionality that will load the package if required too

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Oct 27, 2025

CLA assistant check
All committers have signed the CLA.

@aembler
Copy link
Copy Markdown
Member

aembler commented Feb 3, 2026

I'm just now getting a chance to look at this. I'm not sure how effective this will be - it looks to me like this forces packages to be loaded from the database during this cache checking? The setupPackageAutoload runs during the cache check, which loads packages. One of the best parts of our full page cache is that it runs without even making a single connection to the database (at least, that is the goal. We will frequently have to fix bugs that introduce a database connection before the cache delivers content to the user.)

$stack = $this->app->make(StackInterface::class);
foreach ($middlewareCache as $middleware) {
if (isset($middleware['package'])) {
$this->app->setupPackageAutoload($middleware['package']);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to always connect to the database? I don't think we're going to be able to accept this unless we can run this without connecting to the DB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants