Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/consent-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,24 @@ export default class ConsentManager {
saveConsents() {
if (this.consents === null) deleteCookie(this.cookieName);
const value = this.config.stringifyCookie(this.consents);
const configCookieExpiresAfterDays = this.config.cookieExpiresAfterDays;

let cookieExpiresAfterDays =
typeof configCookieExpiresAfterDays === 'function'
? configCookieExpiresAfterDays(this.consents, this.config)
: configCookieExpiresAfterDays;

if (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe remove this test and use a default value in setCookie like before.
This way any falsy or 0 value would be set to the default.

setCookie(
	this.cookieName,
	value,
	cookieExpiresAfterDays || 120,
	this.config.cookieDomain
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookieExpiresAfterDays could then be a const var.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with falsies values but 0 as an integer has a meaning for cookie expiration which ends when browser is closed. Using cookieExpiresAfterDays || 120 will replace 0 to 120 which may not be the expected behavior by the end developer.

To not break existing installations, the default could apply to non-functional cookieExpiresAfterDays (like before this feature). When used as a function it'll be up to the developer to explicitly return a value if he don't want a session cookie.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this behavior with 0, given the old implementation I thought it was kind of invalid.
I'm kind of torn here, it would be a breaking change to modify the current behavior, but it seems odd to have different behaviors for different methods...

cookieExpiresAfterDays === null ||
cookieExpiresAfterDays === undefined
) {
cookieExpiresAfterDays = 120;
}

setCookie(
this.cookieName,
value,
this.config.cookieExpiresAfterDays || 120,
cookieExpiresAfterDays,
this.config.cookieDomain
);
Comment thread
felixgirault marked this conversation as resolved.

Expand Down