statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

Meta refresh is being output for API endpoints #1824

Closed jackwakefield closed 6 years ago

jackwakefield commented 6 years ago

Expected behaviour

The API request is blocked until the Stache has been warmed up.

Actual behaviour

The response was along the lines of <meta http-equiv="refresh" content="1; URL=..." />

Steps to reproduce

  1. Perform an API request while the Stache is being warmed up (I know, not great)

Server Details

Operating System: CentOS 7

Web Server: nginx

PHP Version: 7.2

Statamic Version: 2.8.7

Updated from an older Statamic or fresh install: Updated

List of installed addons: Mostly custom

jackwakefield commented 6 years ago

Very crude proof of concept which blocks the request for up to 10 seconds while the Stache is being warmed up by another request, either outputting the meta tag after waiting 10 seconds (not ideal), or carrying on the request.

diff --git a/statamic/core/Providers/StacheServiceProvider.php b/statamic/core/Providers/StacheServiceProvider.php
index c6ed025..1fabc34 100644
--- a/statamic/core/Providers/StacheServiceProvider.php
+++ b/statamic/core/Providers/StacheServiceProvider.php
@@ -100,8 +100,14 @@ class StacheServiceProvider extends ServiceProvider

         $this->app->make(Stache::class)->locales(Config::getLocales());

-        if ($this->stache->isPerformingInitialWarmUp() && !app()->runningInConsole()) {
-            $this->outputRefreshResponse();
+        $startTime = time();
+
+        while ($this->stache->isPerformingInitialWarmUp() && !app()->runningInConsole()) {
+            if (time() - $startTime >= 10) {
+                $this->outputRefreshResponse();
+            }
+
+            usleep(1000);
         }

         $this->manager = $this->app->make(Manager::class);
netnakgraham commented 6 years ago

I have Snipcart firing webhooks to my addon, this routinely "soft" fails because it gets a 200 from the refresh page, but the request never hits my addon controller.

I was thinking along the lines of detecting API requests and issuing a 503 so external services could retry.