Skip to content
Merged
Show file tree
Hide file tree
Changes from 89 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
3e75893
WIP
Rich-Harris Oct 24, 2025
d18d1b9
WIP
Rich-Harris Oct 24, 2025
c34348e
Merge branch 'main' into out-of-order-rendering
Rich-Harris Oct 24, 2025
795fa1a
WIP
Rich-Harris Oct 25, 2025
67ee1ba
WIP
Rich-Harris Oct 25, 2025
b8a27f4
WIP
Rich-Harris Oct 25, 2025
2f69c1c
WIP
Rich-Harris Oct 25, 2025
db593ae
WIP
Rich-Harris Oct 25, 2025
249faa5
WIP
Rich-Harris Oct 25, 2025
271623f
WIP
Rich-Harris Oct 25, 2025
c2e28fb
WIP
Rich-Harris Oct 25, 2025
b71126f
WIP
Rich-Harris Oct 25, 2025
ed36dab
WIP
Rich-Harris Oct 25, 2025
dd827a8
WIP
Rich-Harris Oct 25, 2025
7ef0515
WIP
Rich-Harris Oct 25, 2025
75487f6
WIP
Rich-Harris Oct 25, 2025
c253ee3
WIP
Rich-Harris Oct 25, 2025
357eb8f
WIP
Rich-Harris Oct 25, 2025
9c3b796
WIP
Rich-Harris Oct 25, 2025
14851b4
WIP
Rich-Harris Oct 25, 2025
2a2dfc8
WIP
Rich-Harris Oct 25, 2025
20cc1c6
WIP
Rich-Harris Oct 25, 2025
f4b94e5
WIP
Rich-Harris Oct 25, 2025
b18a5e3
WIP
Rich-Harris Oct 25, 2025
60370e6
WIP
Rich-Harris Oct 25, 2025
55acec6
WIP
Rich-Harris Oct 25, 2025
92e0fc2
WIP
Rich-Harris Oct 25, 2025
1580134
WIP
Rich-Harris Oct 25, 2025
5d63ae5
WIP
Rich-Harris Oct 25, 2025
37b9372
WIP
Rich-Harris Oct 25, 2025
4b83b9e
WIP
Rich-Harris Oct 25, 2025
e79ea01
Merge branch 'main' into out-of-order-rendering
Rich-Harris Oct 26, 2025
8fe3c93
merge main
Rich-Harris Oct 26, 2025
6254efb
merge main
Rich-Harris Oct 26, 2025
b4ad332
revert
Rich-Harris Oct 26, 2025
4cacaa3
note to self
Rich-Harris Oct 26, 2025
47f0ae5
unused
Rich-Harris Oct 26, 2025
b7510b0
WIP
Rich-Harris Oct 26, 2025
2cf7739
WIP
Rich-Harris Oct 26, 2025
eda1dd9
WIP
Rich-Harris Oct 26, 2025
3f06b6c
WIP
Rich-Harris Oct 26, 2025
aa547c5
WIP
Rich-Harris Oct 26, 2025
eccebf0
WIP
Rich-Harris Oct 26, 2025
8638d22
WIP
Rich-Harris Oct 26, 2025
75867c4
WIP
Rich-Harris Oct 26, 2025
7aae17b
deprecate
Rich-Harris Oct 26, 2025
96ad539
update tests
Rich-Harris Oct 26, 2025
c8c030a
lint
Rich-Harris Oct 26, 2025
61f6a15
lint
Rich-Harris Oct 26, 2025
c334653
WIP
Rich-Harris Oct 26, 2025
f91ccc6
WIP
Rich-Harris Oct 26, 2025
33d3792
fix
Rich-Harris Oct 26, 2025
3fc998f
WIP
Rich-Harris Oct 26, 2025
df31c40
unused
Rich-Harris Oct 26, 2025
2e571e3
deopt to ensure state is ready
Rich-Harris Oct 26, 2025
30b04a1
fix
Rich-Harris Oct 26, 2025
a77fab9
DRY
Rich-Harris Oct 26, 2025
df666c3
reduce diff
Rich-Harris Oct 26, 2025
7585da0
reduce diff
Rich-Harris Oct 26, 2025
5186155
reduce diff
Rich-Harris Oct 26, 2025
17bb97c
handle blocked attributes
Rich-Harris Oct 26, 2025
f1715ac
WIP
Rich-Harris Oct 27, 2025
3ad47c3
pre-transform
Rich-Harris Oct 27, 2025
c856ea2
tidy up
Rich-Harris Oct 27, 2025
49df988
fix
Rich-Harris Oct 27, 2025
80d7bfd
WIP
Rich-Harris Oct 27, 2025
f85aff4
WIP
Rich-Harris Oct 27, 2025
61e5b43
fix: handle `<svelte:head>` rendered asynchronously
Rich-Harris Oct 28, 2025
e621f1a
fix tests
Rich-Harris Oct 28, 2025
7d49981
Merge branch 'main' into out-of-order-rendering
Rich-Harris Oct 28, 2025
051f4ec
Merge branch 'async-head' into out-of-order-rendering
Rich-Harris Oct 28, 2025
2e56cd7
fix
Rich-Harris Oct 28, 2025
2a24566
merge main
Rich-Harris Oct 28, 2025
2fa63eb
delay resolve
Rich-Harris Oct 28, 2025
761fb1e
Revert "fix"
Rich-Harris Oct 28, 2025
d029d9f
add error
Rich-Harris Oct 28, 2025
a8708ca
simplify/fix hydration restoration
Rich-Harris Oct 28, 2025
01d7853
fix
Rich-Harris Oct 28, 2025
584a52d
use $state.eager mechanism for $effect.pending - way simpler and more…
Rich-Harris Oct 28, 2025
e4bec34
disable these warnings for now, too many false positives
Rich-Harris Oct 28, 2025
7a1531f
fix
Rich-Harris Oct 28, 2025
65defe4
changeset was already merged
Rich-Harris Oct 28, 2025
a8ba8ae
changeset
Rich-Harris Oct 28, 2025
dc6cda8
oops
Rich-Harris Oct 28, 2025
2cdcc07
Merge branch 'main' into out-of-order-rendering
dummdidumm Oct 28, 2025
7166401
lint
Rich-Harris Oct 28, 2025
b9a6a16
docs + tidy
dummdidumm Oct 28, 2025
8a79efe
prettier
Rich-Harris Oct 28, 2025
c1c8da7
robustify: logic inside memoizer and outside could get out of sync, i…
dummdidumm Oct 28, 2025
29caa62
oops
dummdidumm Oct 28, 2025
49a0a09
uncomment
Rich-Harris Oct 28, 2025
55358bd
use finally
Rich-Harris Oct 28, 2025
fbcfefc
use is_async
Rich-Harris Oct 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bitter-rings-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: out-of-order rendering
203 changes: 201 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { walk } from 'zimmerframe';
import { parse } from '../1-parse/acorn.js';
import * as e from '../../errors.js';
import * as w from '../../warnings.js';
import { extract_identifiers, has_await_expression } from '../../utils/ast.js';
import {
extract_identifiers,
has_await_expression,
object,
unwrap_pattern
} from '../../utils/ast.js';
import * as b from '#compiler/builders';
import { Scope, ScopeRoot, create_scopes, get_rune, set_scope } from '../scope.js';
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
Expand Down Expand Up @@ -543,7 +548,13 @@ export function analyze_component(root, source, options) {
snippet_renderers: new Map(),
snippets: new Set(),
async_deriveds: new Set(),
pickled_awaits: new Set()
pickled_awaits: new Set(),
instance_body: {
sync: [],
async: [],
declarations: [],
hoisted: []
}
};

if (!runes) {
Expand Down Expand Up @@ -676,6 +687,194 @@ export function analyze_component(root, source, options) {
}
}

/**
* @param {ESTree.Node} expression
* @param {Scope} scope
* @param {Set<Binding>} touched
* @param {Set<ESTree.Node>} seen
*/
const touch = (expression, scope, touched, seen = new Set()) => {
if (seen.has(expression)) return;
seen.add(expression);

walk(
expression,
{ scope },
{
ImportDeclaration(node) {},
Identifier(node, context) {
const parent = /** @type {ESTree.Node} */ (context.path.at(-1));
if (is_reference(node, parent)) {
const binding = context.state.scope.get(node.name);
if (binding) {
touched.add(binding);

for (const assignment of binding.assignments) {
touch(assignment.value, assignment.scope, touched, seen);
}
}
}
}
}
);
};

/**
* @param {ESTree.Node} node
* @param {Set<ESTree.Node>} seen
* @param {Set<Binding>} reads
* @param {Set<Binding>} writes
*/
const trace_references = (node, reads, writes, seen = new Set()) => {
if (seen.has(node)) return;
seen.add(node);

/**
* @param {ESTree.Pattern} node
* @param {Scope} scope
*/
function update(node, scope) {
for (const pattern of unwrap_pattern(node)) {
const node = object(pattern);
if (!node) return;

const binding = scope.get(node.name);
if (!binding) return;

writes.add(binding);
}
}

walk(
node,
{ scope: instance.scope },
{
_(node, context) {
const scope = scopes.get(node);
if (scope) {
context.next({ scope });
} else {
context.next();
}
},
AssignmentExpression(node, context) {
update(node.left, context.state.scope);
},
UpdateExpression(node, context) {
update(
/** @type {ESTree.Identifier | ESTree.MemberExpression} */ (node.argument),
context.state.scope
);
},
CallExpression(node, context) {
// for now, assume everything touched by the callee ends up mutating the object
// TODO optimise this better

// special case — no need to peek inside effects as they only run once async work has completed
const rune = get_rune(node, context.state.scope);
if (rune === '$effect') return;

/** @type {Set<Binding>} */
const touched = new Set();
touch(node, context.state.scope, touched);

for (const b of touched) {
writes.add(b);
}
},
// don't look inside functions until they are called
ArrowFunctionExpression(_, context) {},
FunctionDeclaration(_, context) {},
FunctionExpression(_, context) {},
Identifier(node, context) {
const parent = /** @type {ESTree.Node} */ (context.path.at(-1));
if (is_reference(node, parent)) {
const binding = context.state.scope.get(node.name);
if (binding) {
reads.add(binding);
}
}
}
}
);
};

let awaited = false;

// TODO this should probably be attached to the scope?
var promises = b.id('$$promises');

/**
* @param {ESTree.Identifier} id
* @param {ESTree.Expression} blocker
*/
function push_declaration(id, blocker) {
analysis.instance_body.declarations.push(id);

const binding = /** @type {Binding} */ (instance.scope.get(id.name));
binding.blocker = blocker;
}

for (let node of instance.ast.body) {
if (node.type === 'ImportDeclaration') {
analysis.instance_body.hoisted.push(node);
continue;
}

if (node.type === 'ExportDefaultDeclaration' || node.type === 'ExportAllDeclaration') {
// these can't exist inside `<script>` but TypeScript doesn't know that
continue;
}

if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
node = node.declaration;
} else {
continue;
}
}

const has_await = has_await_expression(node);
awaited ||= has_await;

if (awaited && node.type !== 'FunctionDeclaration') {
/** @type {Set<Binding>} */
const reads = new Set(); // TODO we're not actually using this yet

/** @type {Set<Binding>} */
const writes = new Set();

trace_references(node, reads, writes);

const blocker = b.member(promises, b.literal(analysis.instance_body.async.length), true);

for (const binding of writes) {
binding.blocker = blocker;
}

if (node.type === 'VariableDeclaration') {
for (const declarator of node.declarations) {
for (const id of extract_identifiers(declarator.id)) {
push_declaration(id, blocker);
}

// one declarator per declaration, makes things simpler
analysis.instance_body.async.push({
node: declarator,
has_await
});
}
} else if (node.type === 'ClassDeclaration') {
push_declaration(node.id, blocker);
analysis.instance_body.async.push({ node, has_await });
} else {
analysis.instance_body.async.push({ node, has_await });
}
} else {
analysis.instance_body.sync.push(node);
}
}

if (analysis.runes) {
const props_refs = module.scope.references.get('$$props');
if (props_refs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import * as e from '../../../errors.js';
export function AwaitExpression(node, context) {
const tla = context.state.ast_type === 'instance' && context.state.function_depth === 1;

// preserve context for
// a) top-level await and
// b) awaits that precede other expressions in template or `$derived(...)`
// preserve context for awaits that precede other expressions in template or `$derived(...)`
if (
tla ||
(is_reactive_expression(
is_reactive_expression(
context.path,
context.state.derived_function_depth === context.state.function_depth
) &&
!is_last_evaluated_expression(context.path, node))
!is_last_evaluated_expression(context.path, node)
) {
context.state.analysis.pickled_awaits.add(node);
}
Expand Down Expand Up @@ -145,6 +142,9 @@ function is_last_evaluated_expression(path, node) {
if (node !== parent.expressions.at(-1)) return false;
break;

case 'VariableDeclarator':
return true;

default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function BindDirective(node, context) {
}

const binding = context.state.scope.get(left.name);
node.metadata.binding = binding;

if (assignee.type === 'Identifier') {
// reassignment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ export function SnippetBlock(node, context) {
function can_hoist_snippet(scope, scopes, visited = new Set()) {
for (const [reference] of scope.references) {
const binding = scope.get(reference);
if (!binding) continue;

if (!binding || binding.scope.function_depth === 0) {
if (binding.blocker) {
return false;
}

if (binding.scope.function_depth === 0) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { FunctionExpression } from './visitors/FunctionExpression.js';
import { HtmlTag } from './visitors/HtmlTag.js';
import { Identifier } from './visitors/Identifier.js';
import { IfBlock } from './visitors/IfBlock.js';
import { ImportDeclaration } from './visitors/ImportDeclaration.js';
import { KeyBlock } from './visitors/KeyBlock.js';
import { LabeledStatement } from './visitors/LabeledStatement.js';
import { LetDirective } from './visitors/LetDirective.js';
Expand Down Expand Up @@ -111,7 +110,6 @@ const visitors = {
HtmlTag,
Identifier,
IfBlock,
ImportDeclaration,
KeyBlock,
LabeledStatement,
LetDirective,
Expand Down Expand Up @@ -153,7 +151,7 @@ export function client_component(analysis, options) {
scope: analysis.module.scope,
scopes: analysis.module.scopes,
is_instance: false,
hoisted: [b.import_all('$', 'svelte/internal/client')],
hoisted: [b.import_all('$', 'svelte/internal/client'), ...analysis.instance_body.hoisted],
node: /** @type {any} */ (null), // populated by the root node
legacy_reactive_imports: [],
legacy_reactive_statements: new Map(),
Expand Down Expand Up @@ -370,41 +368,22 @@ export function client_component(analysis, options) {
analysis.reactive_statements.size > 0 ||
component_returned_object.length > 0;

if (analysis.instance.has_await) {
if (should_inject_context && component_returned_object.length > 0) {
component_block.body.push(b.var('$$exports'));
}
const body = b.block([
...store_setup,
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body),
...(should_inject_context && component_returned_object.length > 0
? [b.stmt(b.assignment('=', b.id('$$exports'), b.object(component_returned_object)))]
: []),
b.if(b.call('$.aborted'), b.return()),
.../** @type {ESTree.Statement[]} */ (template.body)
]);

component_block.body.push(
b.stmt(b.call(`$.async_body`, b.id('$$anchor'), b.arrow([b.id('$$anchor')], body, true)))
);
} else {
component_block.body.push(
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body)
);
if (should_inject_context && component_returned_object.length > 0) {
component_block.body.push(b.var('$$exports', b.object(component_returned_object)));
}
component_block.body.unshift(...store_setup);
component_block.body.push(
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body)
);

if (!analysis.runes && analysis.needs_context) {
component_block.body.push(b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined)));
}
if (should_inject_context && component_returned_object.length > 0) {
component_block.body.push(b.var('$$exports', b.object(component_returned_object)));
}
component_block.body.unshift(...store_setup);

component_block.body.push(.../** @type {ESTree.Statement[]} */ (template.body));
if (!analysis.runes && analysis.needs_context) {
component_block.body.push(b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined)));
}

component_block.body.push(.../** @type {ESTree.Statement[]} */ (template.body));

if (analysis.needs_mutation_validation) {
component_block.body.unshift(
b.var('$$ownership_validator', b.call('$.create_ownership_validator', b.id('$$props')))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export interface ClientTransformState extends TransformState {
*/
readonly in_constructor: boolean;

/** `true` if we're transforming the contents of `<script>` */
readonly is_instance: boolean;

readonly transform: Record<
string,
{
Expand Down
Loading
Loading