Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default function ssr(
instance_javascript,
...parent_bindings,
css.code && b`$$result.css.add(#css);`,
main
b`return @no_current_component(() => { ${main} });`
].filter(Boolean);

const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');
Expand Down
43 changes: 23 additions & 20 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { add_render_callback, flush, flush_render_callbacks, schedule_update, dirty_components } from './scheduler';
import { current_component, set_current_component } from './lifecycle';
import { current_component, no_current_component, set_current_component } from './lifecycle';
import { blank_object, is_empty, is_function, run, run_all, noop } from './utils';
import { children, detach, start_hydrating, end_hydrating } from './dom';
import { transition_in } from './transitions';
Expand Down Expand Up @@ -121,26 +121,29 @@ export function init(component, options, instance, create_fragment, not_equal, p
ready = true;
run_all($$.before_update);

// `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;

if (options.target) {
if (options.hydrate) {
start_hydrating();
const nodes = children(options.target);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment!.l(nodes);
nodes.forEach(detach);
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment!.c();
}
no_current_component(() => {
// `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;


if (options.target) {
if (options.hydrate) {
start_hydrating();
const nodes = children(options.target);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment!.l(nodes);
nodes.forEach(detach);
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment && $$.fragment!.c();
}

if (options.intro) transition_in(component.$$.fragment);
mount_component(component, options.target, options.anchor, options.customElement);
end_hydrating();
flush();
}
if (options.intro) transition_in(component.$$.fragment);
mount_component(component, options.target, options.anchor, options.customElement);
end_hydrating();
flush();
}
});

set_current_component(parent_component);
}
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/internal/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export function get_current_component() {
return current_component;
}

export function no_current_component(callback: () => void) {
const old_current_component = current_component;
current_component = undefined;
const value = callback();
current_component = old_current_component;
return value;
}

/**
* Schedules a callback to run immediately before the component is updated after any state change.
*
Expand Down
3 changes: 3 additions & 0 deletions test/runtime/samples/context-api-in-element-1/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};
5 changes: 5 additions & 0 deletions test/runtime/samples/context-api-in-element-1/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>

<a href={getContext('test')}>{getContext('test')}</a>
3 changes: 3 additions & 0 deletions test/runtime/samples/context-api-in-element-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};
5 changes: 5 additions & 0 deletions test/runtime/samples/context-api-in-element-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>

<a href={getContext('test')}>xxx</a>
3 changes: 3 additions & 0 deletions test/runtime/samples/context-api-in-element-3/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};
5 changes: 5 additions & 0 deletions test/runtime/samples/context-api-in-element-3/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>

<span>{getContext('test')}</span>