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

skip pending block for already-resolved promises on mount #11995

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jun 11, 2024

#11989 got me thinking — aside from the question of keeping then/catch blocks mounted rather than recreating them each time (as though they contained {#key ...} blocks), why do we create pending blocks at all on mount, if the promise is already resolved?

This PR suggests an approach to that (though it appears to have picked up some unrelated changes due to a sloppy merge, oops). More intended to spark discussion than anything — it's draft for two reasons:

  • a whole bunch of tests fail, because they expect pending blocks to be rendered initially
  • it doesn't address the 'keep then/catch blocks mounted' thing

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 11, 2024

🦋 Changeset detected

Latest commit: 9988752

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR


if (catch_fn) {
catch_effect = create_effect(catch_fn, error);
}
}
);

Promise.resolve().then(() => {
Copy link
Member

@dummdidumm dummdidumm Jun 11, 2024

Choose a reason for hiding this comment

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

This should use something that plays nicely with flushSync - queue_micro_task for example. Else this makes mount async (conceptually).

In general I don't think this is a case worth optimizing for:

  • SSR is sync, so you can't set a promise into settled state right away. That means before hydration you see the pending state anyway, not worth adding extra logic to then "repair" the dom because it's in settled state now
  • mount should be sync conceptually (we agreed on this recently), so it doesn't really make sense there either

Copy link
Member Author

Choose a reason for hiding this comment

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

I view this as a correctness issue as much as an optimisation issue. In mount we're currently rendering a pending block for a promise that isn't pending. The fact that we're doing that and then destroying it before the user even sees it makes that worse, not better.

queue_micro_task doesn't help here, because the work can't happen before the original promise handlers have had a chance to run.

Copy link
Member

Choose a reason for hiding this comment

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

but does it matter in practise? Is this something worth considering? Who would have a resolved promise on mount? Even switching between resolved promises is an edge case, a resolved promise on mount even moreso.

Copy link
Member Author

Choose a reason for hiding this comment

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

You could imagine a case where a SvelteKit load function does this...

export function load() {
  return {
    critical: 'some critical data',
    streamed: get_promise()
  };
}

...and the promise resolves before hydration begins. Or a case where you have a client-side cache. I don't think these are the majority of cases, but I don't think they're edge cases either

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.

None yet

2 participants