Automattic / Automattic/o2

Router init wipes out registered event handlers

Open
#175 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
338
Forks
56
Avg merge
24m
Merged PRs (30d)
16

Description

If another plugin registers a direct event callback handler for an element inside the post's content, and it gets registered before `o2.Routers.App` initializes, then those registrations will be wiped out by this line:

https://github.com/Automattic/o2/blob/62cb6262438be7eb0670265436ef842da87cfcdb/js/app/main.js#L44

That may not be a bug exactly, but it creates unexpected conflicts with other plugins, and is very time-consuming to troubleshoot. There's probably a better way to achieve whatever that code is doing, without breaking other plugins.

### Steps to reproduce

1. Add the code below to `mu-plugins/foo.php`:
1. Deactivate o2 plugin
1. Load a post on the front end of the site
1. Open the dev console
1. Click on the `click me` text at the bottom of the post, see the event logged in the console, to indicate that it's working
1. Activate the o2 plugin and repeat the above, you'll see that nothing happens.
1. Do any of the workarounds below and you'll see it start working again

```php

add_filter( 'the_content', function( $content ) {
ob_start();

?>

click me


document.addEventListener( 'DOMContentLoaded', function(event) {
document.getElementById( 'foo' ).addEventListener( 'click', function( event ) {
console.log( { event } );
} );
} );

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.