Rails дублирует запросы GET - PullRequest
0 голосов
/ 05 июля 2019

У меня есть приложение Rails, отправляющее двойные запросы GET. Я обнаружил, что проблема не существует с запросами POST.

В журнале показано, что я тестирую веб-сайт локально, но проблема сохраняется на производстве (Heroku).

Я нашел несколько сообщений об одной и той же проблеме, связанной с турболинками, после чего я приступил к удалению всего, что связано с турболинками, из моего кода. Проблема все еще сохраняется. Я также нашел некоторых людей с похожими проблемами, связанными с пустыми тегами img или url (). Я не нашел ничего подобного. Я также попытался удалить все html-файлы, отображаемые на определенных страницах, чтобы попытаться проверить, не была ли ошибка, в том числе частичная визуализация, безуспешной. Теперь я совершенно невежественен.

Вот журнал, показывающий двойной запрос:

Started GET "/" for 127.0.0.1 at 2019-07-04 18:38:30 -0300
Processing by HomeController#index as HTML
  Cart Load (0.5ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" IS NULL LIMIT $1  [["LIMIT", 1]]
  Setting Load (0.5ms)  SELECT  "settings".* FROM "settings" WHERE "settings"."key" = $1 LIMIT $2  [["key", "featured_artist_slug"], ["LIMIT", 1]]
  Artist Load (33.8ms)  SELECT  "users".* FROM "users" left outer join (select user_id from arts group by user_id) as arts on users.id = arts.user_id WHERE (arts.user_id is not null) AND "users"."slug" = $1 LIMIT $2  [["slug", "herisson-artes"], ["LIMIT", 1]]
  Rendering home/index.html.erb within layouts/application
   (0.5ms)  SELECT COUNT(*) AS "size", MAX("banners"."updated_at") AS timestamp FROM "banners" WHERE "banners"."active" = $1  [["active", true]]
  Admin::Banner Load (0.5ms)  SELECT "banners".* FROM "banners" WHERE "banners"."active" = $1 ORDER BY "banners"."position" ASC  [["active", true]]
  Rendered home/index.html.erb within layouts/application (18.4ms)
  Rendered shared/_google_tag_manager_head.html.erb (0.8ms)
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 56592], ["LIMIT", 1]]
  Rendered shared/_google_analytics.html.erb (4.7ms)
  Rendered layouts/application/_favicons.html.erb (2.6ms)
  Rendered shared/_seo.html.erb (4.1ms)
  Rendered layouts/application/_head.html.erb (20.4ms)
  Rendered shared/_google_tag_manager_body.html.erb (1.1ms)
  Rendered shared/_facebook_js_sdk.html (0.4ms)
   (0.9ms)  SELECT COUNT(*) AS "size", MAX("coupons"."updated_at") AS timestamp FROM "coupons"
  Coupon Load (4.6ms)  SELECT "coupons".* FROM "coupons"
  Rendered shared/_smartbar.html.erb (219.3ms) [cache hit]
  Rendered layouts/application/_navbar.html.erb (7.6ms) [cache hit]
  Rendered shared/_alert.html.erb (0.9ms)
  Rendered layouts/application/_footer.html.erb (3.4ms) [cache hit]
  Art Load (0.6ms)  SELECT  "arts".* FROM "arts" WHERE "arts"."active" = $1 AND "arts"."user_id" = $2 LIMIT $3  [["active", true], ["user_id", 56592], ["LIMIT", 1]]
   (1.2ms)  SELECT COUNT(*) FROM "arts" WHERE "arts"."active" = $1 AND "arts"."user_id" = $2  [["active", true], ["user_id", 56592]]
  Order Exists (0.5ms)  SELECT  1 AS one FROM "orders" WHERE "orders"."deleted_at" IS NULL AND "orders"."user_id" = $1 LIMIT $2  [["user_id", 56592], ["LIMIT", 1]]
  Rendered shared/_crisp.html.erb (12.2ms)
Completed 200 OK in 349ms (Views: 291.6ms | ActiveRecord: 44.3ms)


Started GET "/" for 127.0.0.1 at 2019-07-04 18:38:30 -0300
Processing by HomeController#index as HTML
  Cart Load (0.5ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" IS NULL LIMIT $1  [["LIMIT", 1]]
  Setting Load (0.7ms)  SELECT  "settings".* FROM "settings" WHERE "settings"."key" = $1 LIMIT $2  [["key", "featured_artist_slug"], ["LIMIT", 1]]
  Artist Load (140.1ms)  SELECT  "users".* FROM "users" left outer join (select user_id from arts group by user_id) as arts on users.id = arts.user_id WHERE (arts.user_id is not null) AND "users"."slug" = $1 LIMIT $2  [["slug", "herisson-artes"], ["LIMIT", 1]]
  Rendering home/index.html.erb within layouts/application
   (0.6ms)  SELECT COUNT(*) AS "size", MAX("banners"."updated_at") AS timestamp FROM "banners" WHERE "banners"."active" = $1  [["active", true]]
  Admin::Banner Load (1.2ms)  SELECT "banners".* FROM "banners" WHERE "banners"."active" = $1 ORDER BY "banners"."position" ASC  [["active", true]]
  Rendered home/index.html.erb within layouts/application (26.7ms)
  Rendered shared/_google_tag_manager_head.html.erb (0.7ms)
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 56592], ["LIMIT", 1]]
  Rendered shared/_google_analytics.html.erb (6.8ms)
  Rendered layouts/application/_favicons.html.erb (2.8ms)
  Rendered shared/_seo.html.erb (5.6ms)
  Rendered layouts/application/_head.html.erb (27.5ms)
  Rendered shared/_google_tag_manager_body.html.erb (3.1ms)
  Rendered shared/_facebook_js_sdk.html (0.7ms)
   (1.8ms)  SELECT COUNT(*) AS "size", MAX("coupons"."updated_at") AS timestamp FROM "coupons"
  Coupon Load (20.7ms)  SELECT "coupons".* FROM "coupons"
  Rendered shared/_smartbar.html.erb (812.0ms) [cache hit]
  Rendered layouts/application/_navbar.html.erb (13.8ms) [cache hit]
  Rendered shared/_alert.html.erb (2.1ms)
  Rendered layouts/application/_footer.html.erb (7.5ms) [cache hit]
  Art Load (1.0ms)  SELECT  "arts".* FROM "arts" WHERE "arts"."active" = $1 AND "arts"."user_id" = $2 LIMIT $3  [["active", true], ["user_id", 56592], ["LIMIT", 1]]
   (1.5ms)  SELECT COUNT(*) FROM "arts" WHERE "arts"."active" = $1 AND "arts"."user_id" = $2  [["active", true], ["user_id", 56592]]
  Order Exists (1.3ms)  SELECT  1 AS one FROM "orders" WHERE "orders"."deleted_at" IS NULL AND "orders"."user_id" = $1 LIMIT $2  [["user_id", 56592], ["LIMIT", 1]]
  Rendered shared/_crisp.html.erb (23.1ms)
Completed 200 OK in 1137ms (Views: 948.5ms | ActiveRecord: 170.5ms)


Started GET "/assets/favicons/favicon-359aff8efc45853aea627316f2ab1c7eb0242d855d0e5049152a699ac872092d.ico" for 127.0.0.1 at 2019-07-04 18:38:32 -0300
Started GET "/manifest.json" for 127.0.0.1 at 2019-07-04 18:38:32 -0300
Started GET "/assets/favicons/favicon-359aff8efc45853aea627316f2ab1c7eb0242d855d0e5049152a699ac872092d.ico" for 127.0.0.1 at 2019-07-04 18:38:32 -0300
Started GET "/manifest.json" for 127.0.0.1 at 2019-07-04 18:38:32 -0300
Started GET "/serviceworker.js" for 127.0.0.1 at 2019-07-04 18:38:34 -0300

Любые советы приветствуются. Заранее спасибо.

1 Ответ

0 голосов
/ 10 июля 2019

Итак, просмотрев вкладку «Сеть» в инструментах разработчика моего браузера, как это было предложено в комментариях, я обнаружил, что проблема связана с работником сервиса.Он дублировал запросы, когда было запущено событие fetch.Для всех, кто интересуется, вот код, который дублировал запросы:

self.addEventListener('fetch', function(event) {
  if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
    return;
  }
  event.respondWith(
    checkResponse(event.request).catch(function() {
    return returnFromCache(event.request)}
  ));
  event.waitUntil(addToCache(event.request));
});

var checkResponse = function(request){
  return new Promise(function(fulfill, reject) {
    fetch(request).then(function(response){
      if(response.status !== 404) {
        fulfill(response)
      } else {
        reject()
      }
    }, reject)
  });
};

var addToCache = function(request){
  return caches.open('touts-offline').then(function (cache) {
    return fetch(request).then(function (response) {
      return cache.put(request, response);
    });
  });
};

var returnFromCache = function(request){
  return caches.open('touts-offline').then(function (cache) {
    return cache.match(request).then(function (matching) {
     if(!matching || matching.status == 404) {
       return cache.match('offline.html')
     } else {
       return matching
     }
    });
  });
};

И вот правильный способ сделать это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...