Repository navigation
Initialize SQLPage fragments incrementally and only once #1534
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
+763
−327
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03c4623
Initialize SQLPage fragments incrementally and only once
lovasoa 5bd9e21
Reuse existing component fixtures for fragment lifecycle coverage
lovasoa 8d53afa
Preserve initial-page coverage alongside fragment test cases
lovasoa 2676175
Preserve fragment roots through lazy initialization and modal moves
lovasoa b460387
Move fragment component scenarios into fixture suites
lovasoa 6a7286f
Merge main into fragment initialization PR
lovasoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,37 @@ | ||
| export function add_init_fn(f: () => void) { | ||
| document.addEventListener("DOMContentLoaded", f); | ||
| document.addEventListener("fragment-loaded", f); | ||
| if (document.readyState !== "loading") setTimeout(f, 0); | ||
| export type InitRoot = Element | Document; | ||
|
|
||
| // The script element carries roots across independently bundled initializers. | ||
| export type InitScript = HTMLScriptElement & { | ||
| sqlpage_init_roots?: Set<InitRoot>; | ||
| }; | ||
|
|
||
| /** Select matching descendants, including the fragment root itself. */ | ||
| export function select_all<T extends Element>( | ||
| root: InitRoot, | ||
| selector: string, | ||
| ): T[] { | ||
| const descendants = [...root.querySelectorAll<T>(selector)]; | ||
| if (root instanceof Element && root.matches(selector)) { | ||
| descendants.unshift(root as T); | ||
| } | ||
| return descendants; | ||
| } | ||
|
|
||
| export function add_init_fn(f: (root: InitRoot) => void) { | ||
| const script = document.currentScript as InitScript | null; | ||
| const pending_roots = script?.sqlpage_init_roots; | ||
| const initialize = () => { | ||
| if (script) delete script.sqlpage_init_roots; | ||
| for (const root of pending_roots ?? [document]) { | ||
| if (root instanceof Document || root.isConnected) f(root); | ||
| } | ||
| }; | ||
| if (document.readyState === "loading") { | ||
| document.addEventListener("DOMContentLoaded", initialize, { once: true }); | ||
| } else { | ||
| setTimeout(initialize, 0); | ||
| } | ||
| document.addEventListener("fragment-loaded", ({ target }) => { | ||
| if (target instanceof Element || target instanceof Document) f(target); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On normal pages
sqlpage.jsis deferred, so it executes whiledocument.readyStateis alreadyinteractive; this branch postpones every initializer to a timer, which runs afterDOMContentLoaded. Customjavascriptscripts included byshell.handlebarsare also deferred and can therefore receiveDOMContentLoadedwhile forms, tables, Bootstrap widgets, and lazy bundles are still uninitialized. Previously SQLPage registered itsDOMContentLoadedlisteners before those custom scripts, so integrations that access initialized components from that event now race or fail; retain a one-shot initialization duringDOMContentLoadedrather than deferring it past the event.Useful? React with 👍 / 👎.