digitoimistodude / air-cookie

12 stars 2 forks source link

Why send consent POST on each request? #2

Closed Ciantic closed 2 months ago

Ciantic commented 7 months ago

Reproduce:

  1. Start Google Chrome as Guest user (so no extensions are affecting it)
  2. Navigate dude.fi
  3. Accept cookies
  4. Go to some sub page, hit refresh

I see POST https://www.dude.fi/wp-json/air-cookie/v1/consent on each request:

https://github.com/digitoimistodude/air-cookie/assets/64731/978e9680-4ff4-4cbd-968b-bd6597f8d6c8

ronilaukkarinen commented 7 months ago

Quick input: This could be because

1) we need to know if the user changes the consent in any given moment. 2) dude.fi is using swup.js for page transtions

Screen-Shot-2023-12-14-10-52-18

But I might be wrong about this. Need to check in some point.

Ciantic commented 7 months ago

It happened on our site too (not using swup.js), that's why I opened the issue. I just didn't want to post that site URL so I found out it happens on dude.fi which was better showcase.

Ciantic commented 5 months ago

Cookie Consent's onAccept is triggered on each page load.

I managed to fix this like this, add a new callback onFirstAction:

      airCookieSettings.onFirstAction = function() {
        airCookierecordConsent();
      }

Then commenting out airCookierecordConsent() from onAccept like this:

    <?php // Allow adding categiry specific javascript to be runned when the category is accepted.
    if ( ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) : ?>
      airCookieSettings.onAccept = function() {
        // airCookierecordConsent(); // THIS LINE IS THE PROBLEM, BECAUSE CONSENT calls onAccept on each request

        <?php foreach ( $cookie_categories as $cookie_category ) {
          echo do_category_js( $cookie_category ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        } ?>
      }

I think they have changed many things in 3.0.0, because there is now onConsent callback.

ronilaukkarinen commented 5 months ago

Hmm, I see. We should upgrade to 3 in some point (#3). In the meantime you could send a PR about your fix?

Ciantic commented 4 months ago

I was about to send PR, but I don't think it's useful anymore because there is now this excellent PR for upgrading directly 3.0 that also fixes this: https://github.com/digitoimistodude/air-cookie/pull/4