Skip to content

Commit 2b70cc3

Browse files
authored
Merge pull request #102 from atellmer/fix/server
fix: correct render textarea content on the server
2 parents 185e00e + a539229 commit 2b70cc3

File tree

17 files changed

+86
-95
lines changed

17 files changed

+86
-95
lines changed

examples/server-side-rendering/backend/app.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'module-alias/register';
22
import { join } from 'node:path';
3+
import zlib from 'node:zlib';
34
import express from 'express';
45
import compression from 'compression';
56
import { json } from 'body-parser';
@@ -21,12 +22,19 @@ app.use('/service-worker.js.map', express.static(join(__dirname, '../frontend/st
2122
createRestApi(app);
2223

2324
app.get('*', (req, res) => {
25+
const encoding = req.headers['accept-encoding'] || '';
2426
const { url } = req;
25-
console.log('url', url);
2627
const stream = bootstrap({ props: { url, api } });
2728

29+
console.log('url', url);
2830
res.statusCode = 200;
29-
stream.pipe(res);
31+
32+
if (encoding.includes('gzip')) {
33+
res.setHeader('Content-Encoding', 'gzip');
34+
stream.pipe(zlib.createGzip()).pipe(res);
35+
} else {
36+
stream.pipe(res);
37+
}
3038
});
3139

3240
const server = app.listen(PORT, () => {
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/server-side-rendering/frontend/components/metadata.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ const Metadata = component<MetadataProps>(({ marker }) => {
2424
<meta name='theme-color' content='#fdd835' />
2525
<meta name='description' content={description} />
2626
<link rel='manifest' href='/static/assets/manifest.webmanifest' />
27-
<link rel='preload' href='/static/assets/fonts/Roboto-Regular.ttf' _as='font' crossorigin='anonymous' />
27+
<link
28+
rel='preload'
29+
href='/static/assets/fonts/Roboto-Regular.woff2'
30+
_as='font'
31+
type='font/woff2'
32+
crossorigin='anonymous'
33+
/>
2834
<base href='/' />
2935
<title>{title}</title>
3036
</Metatags>

examples/server-side-rendering/frontend/components/ui.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const GlobalStyle = createGlobalStyle`
1818
font-family: 'Roboto';
1919
font-weight: 400;
2020
font-display: swap;
21-
src: url('/static/assets/fonts/Roboto-Regular.ttf');
21+
src: url('/static/assets/fonts/Roboto-Regular.woff2');
2222
}
2323
2424
*, *::before, *::after {

packages/core/src/fiber/fiber.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,14 @@ class Hook<T = unknown> {
186186
}
187187

188188
drop() {
189-
const { atoms } = this;
189+
const { atoms, values, owner } = this;
190190

191-
if (this.values.length > 0) {
192-
const $hook = this as Hook<HookValue<UseEffectValue>>;
193-
194-
this.owner.mask & EFFECT_HOST_MASK && dropEffects($hook);
191+
if (values.length > 0 && owner.mask & EFFECT_HOST_MASK) {
192+
dropEffects(this as Hook<HookValue<UseEffectValue>>);
195193
}
196194

197195
if (atoms) {
198-
for (const [_, cleanup] of atoms) {
199-
cleanup();
200-
}
201-
196+
for (const [_, cleanup] of atoms) cleanup();
202197
this.atoms = null;
203198
}
204199
}

packages/core/src/unmount/unmount.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EFFECT_HOST_MASK, ATOM_HOST_MASK } from '../constants';
22
import { removeScope, $$scope } from '../scope';
33
import { detectIsUndefined } from '../utils';
44
import { type Callback } from '../shared';
5+
import { platform } from '../platform';
56
import { type Fiber } from '../fiber';
67
import { walk } from '../walk';
78

@@ -19,14 +20,16 @@ function onWalk(fiber: Fiber, skip: Callback) {
1920
hook?.drop();
2021
}
2122

22-
function unmountRoot(rootId: number, onCompleted: () => void) {
23+
function unmountRoot(rootId: number) {
2324
if (detectIsUndefined(rootId)) return;
2425
const $scope = $$scope(rootId);
2526

26-
unmountFiber($scope.getRoot());
27-
$scope.off();
27+
if (platform.detectIsDynamic()) {
28+
unmountFiber($scope.getRoot());
29+
$scope.off();
30+
}
31+
2832
removeScope(rootId);
29-
onCompleted();
3033
}
3134

3235
export { unmountFiber, unmountRoot };

packages/core/src/utils/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ const mapRecord = <T extends object>(record: T) => keys(record).map(x => record[
6969

7070
const createError = (x: unknown) => (x instanceof Error ? x : new Error(String(x)));
7171

72+
const stringify = JSON.stringify as (x: unknown) => string;
73+
7274
export {
7375
detectIsFunction,
7476
detectIsUndefined,
@@ -99,4 +101,5 @@ export {
99101
createIndexKey,
100102
mapRecord,
101103
createError,
104+
stringify,
102105
};

packages/core/src/workloop/workloop.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -388,47 +388,54 @@ function supportConditional(inst: Instance) {
388388
}
389389

390390
function commit($scope: Scope) {
391+
const isStream = $scope.getIsStream();
392+
391393
if (process.env.NODE_ENV !== 'production') {
392394
process.env.NODE_ENV === 'development' && $scope.setIsHot(false);
393395
}
394396

395-
const wip = $scope.getWorkInProgress();
396-
const deletions = $scope.getDeletions();
397-
const candidates = $scope.getCandidates();
398-
const isUpdate = $scope.getIsUpdate();
399-
const awaiter = $scope.getAwaiter();
400-
const unmounts: Array<Fiber> = [];
401-
const inst = wip.inst as Component;
402-
403-
// !
404-
for (const fiber of deletions) {
405-
const canAsync = fiber.mask & ATOM_HOST_MASK && !(fiber.mask & EFFECT_HOST_MASK);
406-
407-
canAsync ? unmounts.push(fiber) : unmountFiber(fiber);
408-
fiber.tag = DELETE_EFFECT_TAG;
409-
platform.commit(fiber);
410-
}
397+
if (isStream) {
398+
platform.finishCommit();
399+
cleanup($scope);
400+
} else {
401+
const wip = $scope.getWorkInProgress();
402+
const deletions = $scope.getDeletions();
403+
const candidates = $scope.getCandidates();
404+
const isUpdate = $scope.getIsUpdate();
405+
const awaiter = $scope.getAwaiter();
406+
const unmounts: Array<Fiber> = [];
407+
const inst = wip.inst as Component;
408+
409+
// !
410+
for (const fiber of deletions) {
411+
const canAsync = fiber.mask & ATOM_HOST_MASK && !(fiber.mask & EFFECT_HOST_MASK);
412+
413+
canAsync ? unmounts.push(fiber) : unmountFiber(fiber);
414+
fiber.tag = DELETE_EFFECT_TAG;
415+
platform.commit(fiber);
416+
}
411417

412-
isUpdate && sync(wip);
413-
$scope.runInsertionEffects();
418+
isUpdate && sync(wip);
419+
$scope.runInsertionEffects();
414420

415-
for (const fiber of candidates) {
416-
const item = fiber.inst as CanHaveChildren;
421+
for (const fiber of candidates) {
422+
const item = fiber.inst as CanHaveChildren;
417423

418-
fiber.tag !== SKIP_EFFECT_TAG && platform.commit(fiber);
419-
fiber.alt = null;
420-
item.children && (item.children = null);
421-
}
424+
fiber.tag !== SKIP_EFFECT_TAG && platform.commit(fiber);
425+
fiber.alt = null;
426+
item.children && (item.children = null);
427+
}
422428

423-
wip.alt = null;
424-
wip.hook?.setIsWip(false);
425-
inst.children = null;
426-
platform.finishCommit(); // !
427-
$scope.runLayoutEffects();
428-
$scope.runAsyncEffects();
429-
awaiter.resolve();
430-
unmounts.length > 0 && setTimeout(onUnmount(unmounts));
431-
cleanup($scope);
429+
wip.alt = null;
430+
wip.hook?.setIsWip(false);
431+
inst.children = null;
432+
platform.finishCommit(); // !
433+
$scope.runLayoutEffects();
434+
$scope.runAsyncEffects();
435+
awaiter.resolve();
436+
unmounts.length > 0 && setTimeout(onUnmount(unmounts));
437+
cleanup($scope);
438+
}
432439
}
433440

434441
const onUnmount = (fibers: Array<Fiber>) => () => fibers.forEach(unmountFiber);

0 commit comments

Comments
 (0)