Skip to content

Load the stylesheet and script only on the pages that show comments - #128

Open
theofanisv wants to merge 2 commits into
kirschbaum-development:mainfrom
theofanisv:load-assets-where-used
Open

theofanisv wants to merge 2 commits into
kirschbaum-development:mainfrom
theofanisv:load-assets-where-used

Conversation

@theofanisv

Copy link
Copy Markdown
Contributor

Hello,

I run an app with several Filament panels: an admin panel for the staff, a public panel that customers open on their phones, and a panel for kiosk machines. Only the admin panel shows comments, and only on a few of its pages. Filament prints every registered asset that is not loaded on request on every page of every panel, so every page of all three panels loaded the comment editor's script, which bundles Tiptap, and the stylesheet, including the pages customers open on their phones.

What this changes

  • Both assets are registered with ->loadedOnRequest().
  • A new commentions::assets view adds them with Livewire's @assets, and the comments, comment-list and comment views include it. Livewire puts them in the page's <head> on the first render, or adds them with the Livewire update that brings the comments in (a modal, a tab, a SPA navigation). Since it is one @assets block, a page gets each file once however many comment components it shows.
  • The script registered the editor Alpine component on alpine:init. When the script arrives with a Livewire update, Alpine has already started and that event will not fire again, so it now registers at once when window.Alpine is there and waits for alpine:init otherwise. resources/dist/commentions.js is rebuilt with node bin/build.js; a build of the unchanged source gave back the committed file byte for byte, so the only difference in it is this one.

Nothing changes on the pages that show comments; every other page stops loading the two files.

How it was tested

  • A new test in tests/Livewire/CommentsTest.php checks that both assets are loaded on request, and that a page rendering the Comments component has both files while a page without it has neither. It fails without the change.
  • The whole suite passes. One note: here Schema::dropAllTables() in TestCase::setUpDatabase() fails on an in-memory SQLite database ("table sqlite_master may not be modified"), with or without this change, so I ran the suite against a temporary SQLite file.
  • In a Filament 5 / Laravel 12 app with three panels, the admin one in SPA mode: a page with the comments entry loads both files and its editor works; after a SPA navigation from a page without comments, the editor still starts and a comment can be written and saved; the other pages and panels no longer load either file, and no page logs a script error.

Thank you for the plugin.

Filament prints every registered asset that is not loaded on request on
every page of every panel. An app with several panels, where only one of
them shows comments, loaded the editor's script (with Tiptap) and the
stylesheet on all pages of all of them, including public pages that never
show a comment.

Both assets are now registered as loaded on request. A new
`commentions::assets` view adds them with Livewire's `@assets`, and the
comments, comment list and comment views include it, so a page gets them
once, in its head, on the first render or with the Livewire update that
brings the comments in.

The script registered the editor's Alpine component on `alpine:init`,
which has already fired when the script arrives with a Livewire update,
for example comments opened in a modal. It now registers at once when
Alpine is already there, and waits for `alpine:init` otherwise.
@luisdalmolin

Copy link
Copy Markdown
Member

@theofanisv Can you please fix the static analysis check that are failing in CI?

@theofanisv

Copy link
Copy Markdown
Contributor Author

Thanks, fixed in 5ab2e6a: the new test now uses Pest's get() helper instead of $this->get(), which PHPStan could not resolve. composer static-analysis reports no errors locally.

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.

3 participants