Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly handle nonce caching? #237

Closed
LukaszJaro opened this issue Jan 30, 2020 · 6 comments
Closed

How to properly handle nonce caching? #237

LukaszJaro opened this issue Jan 30, 2020 · 6 comments
Milestone

Comments

@LukaszJaro
Copy link

Hi,

I have a contact form that uses a nonce, when the nonce expires it stays in the cache unless page reload occurs. Since the nonce value is stale and no longer valid, it causes the form to fail. Is there a filter for specific nonce exclusion?

@westonruter
Copy link
Collaborator

That's a good question. What caching strategy are you using for pages?

@LukaszJaro
Copy link
Author

LukaszJaro commented Jan 30, 2020

STRATEGY_STALE_WHILE_REVALIDATE

add_filter(
	'wp_service_worker_navigation_caching_strategy',
	function() {
		return WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE;
	}
);

add_filter(
	'wp_service_worker_navigation_caching_strategy_args',
	function( $args ) {
		$args['cacheName']                           = 'pages';
		$args['plugins']['expiration']['maxEntries'] = 20;
		return $args;
	}
);

@westonruter
Copy link
Collaborator

westonruter commented Jan 30, 2020

OK, thank you. Yes, I can definitely see how this would be problem. One possibility would be to make pages expire within the lifespan of the nonce. This can be done using maxAgeSeconds, for example:

add_filter(
	'wp_service_worker_navigation_caching_strategy_args',
	function( $args ) {
		$args['cacheName']                           = 'pages';
		$args['plugins']['expiration']['maxEntries'] = 20;

		// 👇 New!
		/** This filter is documented in wp-includes/pluggable.php */
		$max_age_seconds = apply_filters( 'nonce_life', DAY_IN_SECONDS );
		$args['plugins']['expiration']['maxAgeSeconds'] = $max_age_seconds;

		return $args;
	}
);

The value and applied filter there is copied from wp_nonce_tick() in core.

@LukaszJaro
Copy link
Author

Brilliant, thanks! I'm going to give this a shot

@LukaszJaro
Copy link
Author

Thanks @westonruter ! That seemed to do it, no issues so far. Looking forward to the new plugin version!

@westonruter
Copy link
Collaborator

Added this guidance to the new wiki page: https://github.com/GoogleChromeLabs/pwa-wp/wiki/Service-Worker

@westonruter westonruter added this to the 0.4 milestone Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants