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

[amp-autocomplete ] Fix invalid URL protocol for local development #5402

Closed
artneo-io opened this issue Sep 18, 2020 · 10 comments · Fixed by #7508
Closed

[amp-autocomplete ] Fix invalid URL protocol for local development #5402

artneo-io opened this issue Sep 18, 2020 · 10 comments · Fixed by #7508
Assignees
Labels
Changelogged Whether the issue/PR has been added to release notes. WS:Core Work stream for Plugin core
Milestone

Comments

@artneo-io
Copy link

Bug Description

Invalid URL protocol http: for attribute src for amp-autocomplete on local development.

The plugin triggers INVALID_URL_PROTOCOL for //localhost:3000/wp-json/neoblog/v1/search/ URL.

error

Expected Behaviour

It shouldn't be consider an error. To test amp-autocomplete I need to disable the plugin or select View with AMP disabled.

view-with-amp-disabled


Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

@westonruter
Copy link
Member

westonruter commented Sep 18, 2020

Is your local development site running under HTTP and not HTTPS?

In looking at the AMP playground, you can see that http: is not valid AMP:

image

So on one hand, this would ideally be something that AMP core would update in the validator, to exempt localhost from having HTTPS enforced, similar to how Service Workers normally require HTTPS except for when on localhost, which is considered a secure origin.

Otherwise, to exempt http://localhost from being flagged as a src validation error, an alternative would be to force AMP Dev Mode for such elements when wp_get_environment_type() === 'local' (as of WP 5.5). In fact, amp_is_dev_mode() could be by default return true when on the local environment and the HTTP_HOST is localhost. That makes sense to me.

@westonruter
Copy link
Member

The quick fix is to enable HTTPS for your local environment.

@artneo-io
Copy link
Author

I develop locally using Browsersync – the HTTP version page refreshes faster than HTTPS.

@westonruter
Copy link
Member

I suggest you conditionally add the data-ampdevmode attribute to this <amp-autocomplete> element when wp_get_environment_type() === 'local'.

And then also add this plugin code:

if ( wp_get_environment_type() === 'local' ) {
    add_filter( 'amp_dev_mode_enabled', '__return_true' );
}

@artneo-io
Copy link
Author

Does adding add_filter( 'amp_dev_mode_enabled', '__return_true' ); already changes the plugin behavior somehow?

@westonruter
Copy link
Member

It forces the html element to have the data-ampdevmode attribute. Normally this is only ever added if you are logged-in and the admin bar is displayed, since it's important that AMP Dev Mode documents not be accessible to crawlers, since they are explicitly not valid AMP. However, if your site is on a local environment then it's clearly not going to be accessible to crawlers, so it doesn't matter.

@westonruter
Copy link
Member

See default value for this filter here:

/**
* Filters whether AMP mode is enabled.
*
* When enabled, the data-ampdevmode attribute will be added to the document element and it will allow the
* attributes to be added to the admin bar. It will also add the attribute to all elements which match the
* queries for the expressions returned by the 'amp_dev_mode_element_xpaths' filter.
*
* @since 1.3
* @param bool Whether AMP dev mode is enabled.
*/
return apply_filters(
'amp_dev_mode_enabled',
(
// For the few sites that forcibly show the admin bar even when the user is logged out, only enable dev
// mode if the user is actually logged in. This prevents the dev mode from being served to crawlers
// when they index the AMP version. The theme support check disables dev mode in Reader mode.
( is_admin_bar_showing() && is_user_logged_in() )
||
is_customize_preview()
)
);

@westonruter
Copy link
Member

I think amending the logic as follows would also make sense:

--- a/includes/amp-helper-functions.php
+++ b/includes/amp-helper-functions.php
@@ -1458,6 +1458,16 @@ function amp_is_dev_mode() {
 			( is_admin_bar_showing() && is_user_logged_in() )
 			||
 			is_customize_preview()
+			||
+			(
+				! is_ssl()
+				&&
+				(
+					( function_exists( 'wp_get_environment_type' ) && 'local' === wp_get_environment_type() )
+					||
+					'localhost' === wp_parse_url( home_url(), PHP_URL_HOST )
+				)
+			)
 		)
 	);
 }

This can then be combined with extending the AMP_Dev_Mode_Sanitizer to automatically add data-ampdevmode attributes to all elements with src and action attributes that that have the http: protocol which point to the site. Nevertheless, it may be that some AMP components will just refuse to work if not on HTTPS as well, so your mileage may vary with this approach. If that is the case, a feature request to exempt localhost from the HTTPS requirement should be submitted to AMP core.

@jwold jwold added the WS:Core Work stream for Plugin core label Sep 29, 2020
@westonruter westonruter added this to the v2.4.2 milestone Mar 30, 2023
@westonruter
Copy link
Member

This should also address an issue I've seen with wp-env, where when the PWA plugin is enabled, there are validation error due to the use of http: protocol for the Web App Manifest link. This is due to wp-env serving from http://localhost:8888

@thelovekesh thelovekesh added Bug Something isn't working Enhancement New feature or improvement of an existing one P2 Low priority Reader Mode and removed Bug Something isn't working Enhancement New feature or improvement of an existing one P2 Low priority Reader Mode labels Jul 13, 2023
@pooja-muchandikar
Copy link

QA Passed ✅

Before with v2.4.1:

Screenshot 2023-07-13 at 1 58 01 PM

After the fix, this error is not visible. Verified with other plugins such as Web stories, PWA and WooCommerce nothing suspicious error observed it is working fine.

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants