Skip to content

Use with Blitz

Pierre Stoffe edited this page Jan 9, 2023 · 5 revisions

If you are using Blitz (or a similar cache plugin) you will notice that language redirections are not working anymore. This is because Blitz generates static pages and ends all requests before they can hit any other plugin, including Language Redirector.

As a solution we can listen to the EVENT_BEFORE_GET_RESPONSE Blitz event that gets triggered before Blitz returns a cached page, and hook Language Redirector there. See the code example below that you can add to your business logic.

use pierrestoffe\languageredirector\services\LanguageRedirectorService;
use putyourlightson\blitz\services\CacheRequestService;

Event::on(
    CacheRequestService::class,
    CacheRequestService::EVENT_BEFORE_GET_RESPONSE,
    function (Event $event) {
        $languageRedirectorService = new LanguageRedirectorService();
        $languageRedirectorService->canRedirectVisitor();
    }
);

Two notes:

  1. This solution works with v1.3.2 of Language Redirector and above
  2. This solution will not work if you have enabled server rewrites, since the page would be returned before PHP can even jump in
Clone this wiki locally