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

Persistent iframe #23

Draft
wants to merge 7 commits into
base: melange-v3
Choose a base branch
from
Draft

Persistent iframe #23

wants to merge 7 commits into from

Conversation

rusty-key
Copy link
Collaborator

Currently, reshowcase is reloading iframe every time we go from demo to demo. This causes fetching/parsing/executing all assets:
https://github.com/ahrefs/reshowcase/assets/1197026/e6705a68-2cd9-4c3a-8ad6-4122e77e38e8

It makes reshowcase navigation unnecessarily slow, especially on large applications.

Instead, we can make iframe persistent, and only replace its content. This way, reshowcase will fetch assets only once and only need to render relevant demo on navigation:
https://github.com/ahrefs/reshowcase/assets/1197026/798529fc-44db-4bea-8018-87794116c829

This is not yet clean solution, just "showcase" of possibility. I'll continue working on it

@rusty-key rusty-key requested a review from denis-ok March 26, 2024 16:59
Copy link
Collaborator Author

@rusty-key rusty-key left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about some unrelated formatting changes, looks like refmt defaults updated at some point 🤷‍♂️

Besides main refactor, I had to tweak right sidebar because it was throwing at runtime with my solution.

| None => React.null
| Some(body) => ReactDOM.createPortal(children, body)
}}
</iframe>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core change. Instead of referencing iframe with demo, we just render demo using portal in persistent iframe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you thought about how this change might affect stateful changes that the iframe keeps from one demo to another? E.g. if some iframe loads some styles in the page head then they might "leak" from one demo to the next.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't. I assumed this is the responsibility of the consumer: do a cleanup on unmount.

Comment on lines +1090 to +1091
| Some(window) when !showRightSidebar =>
Window.postMessage(window, RightSidebarDisplayed)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why it's done via "signalling", but decided to keep it this way for now to limit the scope of this PR

@@ -867,7 +838,7 @@ module DemoUnit = {

<div name="DemoUnit" style=Styles.container>
<div style=Styles.contents> {demoUnit(props)} </div>
{switch (parentWindowRightSidebarElem) {
{switch (sidebarElem) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also refactored the sidebar to use reference instead of getting elements by id, which seems much more fragile and caused runtime exceptions for me.

@rusty-key rusty-key marked this pull request as ready for review March 26, 2024 19:38
@rusty-key
Copy link
Collaborator Author

I think it's ready for review. I am not sure why things are implemented via signalling instead of using callbacks, so I refactored relevant part and willing to refactor more in the future. But please let me know if I am missing something.

@rusty-key
Copy link
Collaborator Author

oh well, there is an issue with incorrect mounting of styles/scripts. They get mounted to parent root instead of iframe :/
Investigating

@rusty-key rusty-key marked this pull request as draft March 27, 2024 14:59
Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how styles don't "leak" from one demo to the next (see inline comment).

Comment on lines -912 to -914
let useFullframeUrl: bool = [%mel.raw
{js|typeof USE_FULL_IFRAME_URL === "boolean" ? USE_FULL_IFRAME_URL : false|js}
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this feature removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad. I was not sure what it was doing and how to debug it. Will check

| None => React.null
| Some(body) => ReactDOM.createPortal(children, body)
}}
</iframe>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you thought about how this change might affect stateful changes that the iframe keeps from one demo to another? E.g. if some iframe loads some styles in the page head then they might "leak" from one demo to the next.

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.

2 participants