Skip to content

Commit af8c944

Browse files
authored
fix: issue with using __flush_here_and_after__ when in sync mode (#1804)
1 parent 6562922 commit af8c944

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

.changeset/spicy-actors-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"marko": patch
3+
---
4+
5+
Fix issue when internal <**flush_here_and_after**> tag (used by bundler plugins) is used in sync mode.
Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
const BufferedWriter = require("../../runtime/html/BufferedWriter");
22

33
module.exports = function __flushHereAndAfter__(input, out) {
4-
let flushed = false;
5-
const asyncOut = out.beginAsync({ last: true });
6-
const nextWriter = out.writer;
4+
if (out.isSync()) {
5+
// We create an async out that we pinky promise is going to be sync in order to postpone execution of the renderBody.
6+
out._sync = false;
7+
const asyncOut = out.beginAsync({ last: true });
8+
out._sync = true;
9+
asyncOut.sync();
10+
out.onLast(() => {
11+
input.renderBody(asyncOut);
12+
asyncOut.end();
13+
});
14+
} else {
15+
let flushed = false;
16+
const asyncOut = out.beginAsync({ last: true });
17+
const nextWriter = out.writer;
18+
19+
out.on("___toString", writer => {
20+
if (writer instanceof BufferedWriter) {
21+
if (flushed) {
22+
const detachedOut = out.createOut();
23+
detachedOut.sync();
24+
input.renderBody(detachedOut);
25+
writer._content = detachedOut.toString() + writer._content;
26+
} else if (writer.next === nextWriter) {
27+
asyncOut.sync();
28+
input.renderBody(asyncOut);
29+
asyncOut.end();
30+
flushed = true;
31+
}
32+
}
33+
});
734

8-
out.on("___toString", writer => {
9-
if (writer instanceof BufferedWriter) {
10-
if (flushed) {
11-
const detachedOut = out.createOut();
12-
detachedOut.sync();
13-
input.renderBody(detachedOut);
14-
writer._content = detachedOut.toString() + writer._content;
15-
} else if (writer.next === nextWriter) {
35+
out.onLast(() => {
36+
if (!flushed) {
1637
asyncOut.sync();
1738
input.renderBody(asyncOut);
1839
asyncOut.end();
1940
flushed = true;
2041
}
21-
}
22-
});
23-
24-
out.onLast(() => {
25-
if (!flushed) {
26-
asyncOut.sync();
27-
input.renderBody(asyncOut);
28-
asyncOut.end();
29-
flushed = true;
30-
}
31-
});
42+
});
43+
}
3244
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exports.templateData = {};
2-
2+
exports.sync = true;
33
exports["fails_html ≅ vdom"] = true;

packages/marko/test/render/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ async function runRenderTest(fixture) {
126126
isVDOM
127127
);
128128

129+
if (main.sync) {
130+
out.sync();
131+
}
132+
129133
await template.render(templateData, out).end();
130134

131135
if (isVDOM) {

0 commit comments

Comments
 (0)