Rails复制GET请求

时间:2019-07-04 21:46:22

标签: ruby-on-rails http get ruby-on-rails-5

我有一个Rails应用程序,它发送两次GET请求。我发现POST请求不存在该问题。

日志显示我正在本地测试网站,但是问题仍然存在(生产环境(Heroku)上。)

我发现了一些与Turbolink相关的相同问题的报告,此后,我从代码中删除了所有与TurboLink相关的内容。问题仍然存在。我还发现有些人遇到与空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 :(得分:0)

因此,通过按照注释中的建议检查浏览器开发工具中的“网络”选项卡,我发现问题出在服务工。触发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
     }
    });
  });
};

here's和正确的操作方式。