8000 WDT: Only load "Sfjs" if it is not present already by weaverryan · Pull Request #41168 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

WDT: Only load "Sfjs" if it is not present already #41168

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

Merged
merged 1 commit into from
May 11, 2021

Conversation

weaverryan
Copy link
Member
Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets none
License MIT
Doc PR not needed

Hi!

Playing with Turbo, I noticed two small issues with the WDT:

  1. When you click a link (which Turbo loads via AJAX), the new toolbar would correctly load, but its "AJAX tab" would be missing/empty. At first, that seems.. at least... "correct enough". After all, we did just load a new web debug toolbar. However, this can be avoided by simply not redefining the Sfjs variable: if it already exists, just use it. And this is the intention of that object, I believe: for it to be loaded once, and then reused over and over again via its loadToolbar() method.

  2. Additionally, I was experimenting with some crazy prefetch + Turbo functionality. It works, but caused one minor issue with the web debug toolbar. The flow is this:

A) A prefetch request is made to pre-cache a URL in Turbo.
B) When that links is actually clicked, the cached version from (A) is used as the "Preview". It's HTML calls Sfjs.loadToolbar().
C) At the same moment as (B), another AJAX request is made to the same URL from (A) to get the full, fresh page (in case it's out of date).
D) When (C) finishes, it will have its own Sfjs.loadToolbar() call.

The problem is that the AJAX request for the first web debug toolbar (from B) sometimes finishes after the AJAX call made by Turbo for (C). The result is that the first web debug toolbar tries to activate itself... but it's already gone from the page.

I realize this is a... kind of crazy scenario, but I think the fix is legit: if, for any reason, the web debug toolbar element is not on the page, we should not try to initialize it. It results in:

Screen Shot 2021-05-10 at 3 49 06 PM

To Test

JUST to be on the safe side, I prepared a symfony-demo app with Turbo installed, and using these changes. You can try it here - https://github.com/weaverryan/symfony-demo/tree/turbo - the README is updated to quickly get it running (no need to even run yarn, the JavaScript files are committed).

Thanks!

Plus, avoid loading the toolbar if it is no longer on the page
@fabpot
Copy link
Member
fabpot commented May 11, 2021

Thank you @weaverryan.

@fabpot fabpot merged commit 89c1be8 into symfony:4.4 May 11, 2021
@weaverryan weaverryan deleted the sf-debug-turbo branch May 11, 2021 12:51
This was referenced May 12, 2021
nicolas-grekas added a commit that referenced this pull request May 21, 2021
…d also loading base_js Sfjs if needed (weaverryan)

This PR was merged into the 4.4 branch.

Discussion
----------

[WebProfilerBundle] Wrapping exception js in Sfjs check and also loading base_js Sfjs if needed

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41305
| License       | MIT
| Doc PR        | Not needed

This bug was introduced in #41168.

Best viewed with: https://github.com/symfony/symfony/pull/41346/files?w=1 - we could also remove the new indentation in `exception.js`.

In that PR, we innocently don't reload `Sfjs` if it doesn't exist. However, on an error page, that template ALSO adds an `Sfjs`, and that `Sfjs` is *smaller* - containing only a subset of the `Sfjs` functions. This is tricky, but this PR fixes it.

Here are the various situations:

1) I hit an exception page. The `Sfjs` from `exception.js` is loaded first. This adds the smaller `Sfjs`. Then the `base_js.html.twig` version is hit. Because `Sfjs` is missing the `loadToolbar()` method, that code DOES run again to reinitialize it.

2) I am on a normal page, then travel to an exception page with Turbo. In this case, the original `Sfjs` from `base_js.html.twig` was processed. Then, on the exception page, the `Sfjs` from `exception.js` is ignored, as is the 2nd execution of `base_js.html.twig`.

Overall, the JS could use some work for supporting things like Turbo (e.g. `DOMContentLoaded` doesn't work with Turbo, so the exception page JS is broken). But I wanted to focus on fixing the bug in this PR.

As a reminder, #41168 (avoiding overriding `Sfjs`) was done to help with the AJAX toolbar and Turbo - it's explained in point (1) on #41168.

Cheers!

Commits
-------

5fa42f1 Wrapping exception js in Sfjs check and also loading base_js Sfjs if needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0