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

Prevent CSS from loading on unrelated pages #255

Merged
merged 1 commit into from
Sep 19, 2023
Merged

Prevent CSS from loading on unrelated pages #255

merged 1 commit into from
Sep 19, 2023

Conversation

iandunn
Copy link
Member

@iandunn iandunn commented Sep 19, 2023

Fixes #35

The check for bbp_get_displayed_user_id() isn't perfect, since it'll still load the stylesheet on a handful of pages that don't display the block. I think it's good enough for a quick workaround, though. Hopefully we can move to viewStyle in the future and remove this code.

To test, look for the wporg-two-factor-settings-style-css stylesheet ID in response source, or the wp-content/plugins/wporg-two-factor/settings/build/style-index.css URL in the Network tab of browser dev tools.

> curl -is https://wpms.test/ | grep wporg-two-factor

> curl -is https://wpms.test/users/iandunn/edit/account/ | grep wporg-two-factor
<link rel='stylesheet' id='wporg-two-factor-settings-style-css' href='https://wpms.test/content/plugins/wporg-two-factor/settings/build/style-index.css?ver=0.1.3' type='text/css' media='all' />

@iandunn iandunn added this to the Iteration 1 milestone Sep 19, 2023
@iandunn iandunn self-assigned this Sep 19, 2023
@iandunn iandunn marked this pull request as ready for review September 19, 2023 18:17
@iandunn iandunn merged commit e21a86a into trunk Sep 19, 2023
2 checks passed
@iandunn iandunn deleted the lazy-load-css branch September 19, 2023 18:18
@adamwoodnz
Copy link
Contributor

adamwoodnz commented Oct 10, 2023

These styles are still being loaded on Showcase and affecting our performance scores

Screenshot 2023-10-10 at 4 11 18 PM

They're also being loaded on the WordPress.org homepage. Could we use the URI to determine whether to dequeue?

function maybe_dequeue_stylesheet() {
        // Allow to enqueue on the user settings page.
	if ( 0 === strpos( $_SERVER['REQUEST_URI'], '/support/users' ) ) {
		return;
	}

	wp_dequeue_style( 'wporg-two-factor-settings-style' );
}

@iandunn
Copy link
Member Author

iandunn commented Oct 10, 2023

🤔 That's a good point. Fixed in #262 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JS + CSS should not be loaded on every page
2 participants