Skip to content

Commit 9312594

Browse files
committed
abort synchronously in SSR
1 parent c4a948d commit 9312594

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

packages/svelte/src/index-server.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { current_component } from './internal/server/context.js';
33
import { noop } from './internal/shared/utils.js';
44
import * as e from './internal/server/errors.js';
5-
import { STALE_REACTION } from '#client/constants';
65

76
/** @param {() => void} fn */
87
export function onDestroy(fn) {
@@ -36,20 +35,7 @@ export function unmount() {
3635

3736
export async function tick() {}
3837

39-
/** @type {AbortController | null} */
40-
let controller = null;
41-
42-
export function getAbortSignal() {
43-
if (controller === null) {
44-
const c = (controller = new AbortController());
45-
queueMicrotask(() => {
46-
c.abort(STALE_REACTION);
47-
controller = null;
48-
});
49-
}
50-
51-
return controller.signal;
52-
}
38+
export { getAbortSignal } from './internal/server/abort-signal.js';
5339

5440
export { getAllContexts, getContext, hasContext, setContext } from './internal/server/context.js';
5541

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { STALE_REACTION } from '#client/constants';
2+
3+
/** @type {AbortController | null} */
4+
export let controller = null;
5+
6+
export function abort() {
7+
if (controller !== null) {
8+
controller.abort(STALE_REACTION);
9+
controller = null;
10+
}
11+
}
12+
13+
export function getAbortSignal() {
14+
return (controller ??= new AbortController()).signal;
15+
}

packages/svelte/src/internal/server/index.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { validate_store } from '../shared/validate.js';
1818
import { is_boolean_attribute, is_raw_text_element, is_void } from '../../utils.js';
1919
import { reset_elements } from './dev.js';
2020
import { Payload } from './payload.js';
21+
import { abort } from './abort-signal.js';
2122

2223
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
2324
// https://infra.spec.whatwg.org/#noncharacter
@@ -66,50 +67,54 @@ export let on_destroy = [];
6667
* @returns {RenderOutput}
6768
*/
6869
export function render(component, options = {}) {
69-
const payload = new Payload(options.idPrefix ? options.idPrefix + '-' : '');
70+
try {
71+
const payload = new Payload(options.idPrefix ? options.idPrefix + '-' : '');
7072

71-
const prev_on_destroy = on_destroy;
72-
on_destroy = [];
73-
payload.out += BLOCK_OPEN;
73+
const prev_on_destroy = on_destroy;
74+
on_destroy = [];
75+
payload.out += BLOCK_OPEN;
7476

75-
let reset_reset_element;
77+
let reset_reset_element;
7678

77-
if (DEV) {
78-
// prevent parent/child element state being corrupted by a bad render
79-
reset_reset_element = reset_elements();
80-
}
79+
if (DEV) {
80+
// prevent parent/child element state being corrupted by a bad render
81+
reset_reset_element = reset_elements();
82+
}
8183

82-
if (options.context) {
83-
push();
84-
/** @type {Component} */ (current_component).c = options.context;
85-
}
84+
if (options.context) {
85+
push();
86+
/** @type {Component} */ (current_component).c = options.context;
87+
}
8688

87-
// @ts-expect-error
88-
component(payload, options.props ?? {}, {}, {});
89+
// @ts-expect-error
90+
component(payload, options.props ?? {}, {}, {});
8991

90-
if (options.context) {
91-
pop();
92-
}
92+
if (options.context) {
93+
pop();
94+
}
9395

94-
if (reset_reset_element) {
95-
reset_reset_element();
96-
}
96+
if (reset_reset_element) {
97+
reset_reset_element();
98+
}
9799

98-
payload.out += BLOCK_CLOSE;
99-
for (const cleanup of on_destroy) cleanup();
100-
on_destroy = prev_on_destroy;
100+
payload.out += BLOCK_CLOSE;
101+
for (const cleanup of on_destroy) cleanup();
102+
on_destroy = prev_on_destroy;
101103

102-
let head = payload.head.out + payload.head.title;
104+
let head = payload.head.out + payload.head.title;
103105

104-
for (const { hash, code } of payload.css) {
105-
head += `<style id="${hash}">${code}</style>`;
106-
}
106+
for (const { hash, code } of payload.css) {
107+
head += `<style id="${hash}">${code}</style>`;
108+
}
107109

108-
return {
109-
head,
110-
html: payload.out,
111-
body: payload.out
112-
};
110+
return {
111+
head,
112+
html: payload.out,
113+
body: payload.out
114+
};
115+
} finally {
116+
abort();
117+
}
113118
}
114119

115120
/**

0 commit comments

Comments
 (0)