Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Forms: change how Forms iAPI modules are versioned to avoid sticky cache busted versions
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,8 @@ private function render_hidden_field( $id, $label, $value ) {
* @return void
*/
private function enqueue_file_field_assets() {
$version = Constants::get_constant( 'JETPACK__VERSION' );
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/file-field/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

\wp_enqueue_script_module(
'jetpack-form-file-field',
Expand Down Expand Up @@ -2883,7 +2884,8 @@ class="jetpack-field-slider__value-indicator"
* @return void
*/
private function enqueue_slider_field_assets() {
$version = defined( 'JETPACK__VERSION' ) ? \JETPACK__VERSION : '0.1';
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/slider-field/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

\wp_enqueue_style(
'jetpack-form-slider-field',
Expand Down Expand Up @@ -3159,12 +3161,8 @@ public function get_translatable_countries() {
* @return void
*/
private function enqueue_phone_field_assets() {
$version = defined( 'JETPACK__VERSION' ) ? \JETPACK__VERSION : '0.1';

// extra cache busting strategy for view.js, seems they are left out of cache clearing on deploys
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/field-phone/view.asset.php';
$asset = file_exists( $asset_file ) ? require $asset_file : null;
$version .= $asset['version'] ?? '';
$version = Util::get_view_asset_version( $asset_file );

// combobox styles
\wp_enqueue_style(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Automattic\Jetpack\Forms\ContactForm;

use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Extensions\Contact_Form\Contact_Form_Block;
use Automattic\Jetpack\Forms\Jetpack_Forms;
use Automattic\Jetpack\Forms\Service\MailPoet_Integration;
Expand Down Expand Up @@ -772,10 +771,8 @@ public static function reset_step() {
public static function gutenblock_render_form_step( $atts, $content ) {
self::$step_count = 1 + self::$step_count;

$version = Constants::get_constant( 'JETPACK__VERSION' );
if ( empty( $version ) ) {
$version = '0.1';
}
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/form-step/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

\wp_enqueue_script_module(
'jetpack-form-step',
Expand Down Expand Up @@ -827,11 +824,9 @@ public static function gutenblock_render_form_step( $atts, $content ) {
* @return string HTML for the number field.
*/
public static function gutenblock_render_form_step_navigation( $atts, $content ) {
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/form-step-navigation/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

$version = Constants::get_constant( 'JETPACK__VERSION' );
if ( empty( $version ) ) {
$version = '0.1';
}
\wp_enqueue_script_module(
'jetpack-form-step-navigation',
plugins_url( '../../dist/modules/form-step-navigation/view.js', __FILE__ ),
Expand Down Expand Up @@ -909,10 +904,8 @@ public static function gutenblock_render_form_step_navigation( $atts, $content )
* @return string HTML for the progress indicator.
*/
public static function gutenblock_render_form_progress_indicator( $attributes ) {
$version = Constants::get_constant( 'JETPACK__VERSION' );
if ( empty( $version ) ) {
$version = '0.1';
}
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/form-progress-indicator/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

// Get step count from Contact_Form_Block
$max_steps = Contact_Form_Block::get_form_step_count();
Expand Down
11 changes: 2 additions & 9 deletions projects/packages/forms/src/contact-form/class-contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,8 @@ public static function parse( $attributes, $content, $context = array() ) {
wp_enqueue_script( 'accessible-form' );
}

$version = \JETPACK__VERSION;

// Extra cache busting strategy for view.js, seems they are left out of cache clearing on deploys
$asset_file = plugin_dir_path( __FILE__ ) . 'dist/modules/form/view.asset.php';
$asset = file_exists( $asset_file ) ? require $asset_file : null;

if ( $asset && isset( $asset['version'] ) ) {
$version = $asset['version'];
}
$asset_file = plugin_dir_path( __FILE__ ) . '../../dist/modules/form/view.asset.php';
$version = Util::get_view_asset_version( $asset_file );

$config = array(
'error_types' => array(
Expand Down
18 changes: 18 additions & 0 deletions projects/packages/forms/src/contact-form/class-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Automattic\Jetpack\Forms\ContactForm;

use Automattic\Jetpack\Constants;

/**
* This class serves as a container for what previously were standalone grunion functions.
* In the long term we should aim to move things to other classes and gradually get rid of this rather than adding more.
Expand Down Expand Up @@ -386,4 +388,20 @@ public static function get_export_filename( $source = '' ) {
sanitize_file_name( $source )
);
}

/**
* Get version hash from generated `view.asset.php` files, or fallback to Jetpack version, to be used as cache bust for enqueing JS and CSS files.
*
* @param string $asset_file Path to view.asset.php file.
* @return string either asset hash or Jetpack version.
*/
public static function get_view_asset_version( $asset_file ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I could see this in Jetpack main plugin's Assets package as well.

Copy link
Member

Choose a reason for hiding this comment

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

Why view in the method name? Won't it work for any assets.php file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! It does.

I'm thinking of renaming to better highlight the intent of using alongside wp_enqueue_script_module(). Or just leave it generic.

$asset = file_exists( $asset_file ) ? require $asset_file : null;
$jetpack_version = Constants::get_constant( 'JETPACK__VERSION' );
Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively we could fail loudly instead of falling back to JP version; now if the path is incorrect for example, dev might not notice.

Copy link
Member

Choose a reason for hiding this comment

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

Failing when we actually expect the file to exist is better IMHO.

if ( empty( $jetpack_version ) ) {
$jetpack_version = '0.1';
}

return ! empty( $asset['version'] ) ? $asset['version'] : $jetpack_version;
}
}
Loading