Improve rendering perf (#4956)
* Improve rendering perf * Adding a changeset * Disable eslint warning
This commit is contained in:
parent
a59731995b
commit
ee8dd424fd
8 changed files with 73 additions and 31 deletions
5
.changeset/fifty-icons-smash.md
Normal file
5
.changeset/fifty-icons-smash.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix perf regression in SSR
|
|
@ -45,35 +45,22 @@ export function stringifyChunk(result: SSRResult, chunk: string | RenderInstruct
|
|||
}
|
||||
|
||||
export class HTMLParts {
|
||||
public parts: Array<HTMLBytes | string>;
|
||||
public parts: string;
|
||||
constructor() {
|
||||
this.parts = [];
|
||||
this.parts = ''
|
||||
}
|
||||
append(part: string | HTMLBytes | RenderInstruction, result: SSRResult) {
|
||||
if (ArrayBuffer.isView(part)) {
|
||||
this.parts.push(part);
|
||||
this.parts += decoder.decode(part);
|
||||
} else {
|
||||
this.parts.push(stringifyChunk(result, part));
|
||||
this.parts += stringifyChunk(result, part);
|
||||
}
|
||||
}
|
||||
toString() {
|
||||
let html = '';
|
||||
for (const part of this.parts) {
|
||||
if (ArrayBuffer.isView(part)) {
|
||||
html += decoder.decode(part);
|
||||
} else {
|
||||
html += part;
|
||||
}
|
||||
}
|
||||
return html;
|
||||
return this.parts;
|
||||
}
|
||||
toArrayBuffer() {
|
||||
this.parts.forEach((part, i) => {
|
||||
if (!ArrayBuffer.isView(part)) {
|
||||
this.parts[i] = encoder.encode(String(part));
|
||||
}
|
||||
});
|
||||
return concatUint8Arrays(this.parts as Uint8Array[]);
|
||||
return encoder.encode(this.parts);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,15 +73,3 @@ export function chunkToByteArray(
|
|||
}
|
||||
return encoder.encode(stringifyChunk(result, chunk));
|
||||
}
|
||||
|
||||
export function concatUint8Arrays(arrays: Array<Uint8Array>) {
|
||||
let len = 0;
|
||||
arrays.forEach((arr) => (len += arr.length));
|
||||
let merged = new Uint8Array(len);
|
||||
let offset = 0;
|
||||
arrays.forEach((arr) => {
|
||||
merged.set(arr, offset);
|
||||
offset += arr.length;
|
||||
});
|
||||
return merged;
|
||||
}
|
||||
|
|
7
packages/astro/test/benchmark/simple/astro.config.mjs
Normal file
7
packages/astro/test/benchmark/simple/astro.config.mjs
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
import nodejs from '@astrojs/node';
|
||||
|
||||
export default defineConfig({
|
||||
output: 'server',
|
||||
adapter: nodejs()
|
||||
});
|
11
packages/astro/test/benchmark/simple/package.json
Normal file
11
packages/astro/test/benchmark/simple/package.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "@benchmark/simple",
|
||||
"scripts": {
|
||||
"start": "node server.mjs",
|
||||
"build": "astro build"
|
||||
},
|
||||
"dependencies": {
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/node": "workspace:*"
|
||||
}
|
||||
}
|
19
packages/astro/test/benchmark/simple/server.mjs
Normal file
19
packages/astro/test/benchmark/simple/server.mjs
Normal file
|
@ -0,0 +1,19 @@
|
|||
import http from 'http';
|
||||
import { handler } from './dist/server/entry.mjs';
|
||||
|
||||
const listener = (req, res) => {
|
||||
handler(req, res, err => {
|
||||
if(err) {
|
||||
res.writeHead(500);
|
||||
res.end(err.toString());
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
res.end('Not found');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const server = http.createServer(listener);
|
||||
server.listen(3002);
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`Listening at http://localhost:3002`);
|
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>{ Astro.props.title }</title>
|
||||
</head>
|
||||
<body>
|
||||
<slot />
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
import Layout from '../components/Layout.astro';
|
||||
const name = 'world';
|
||||
---
|
||||
|
||||
<Layout title={`Index page`}>
|
||||
<h1>index page</h1>
|
||||
<h2>Hello { name }</h2>
|
||||
</Layout>
|
|
@ -1055,6 +1055,14 @@ importers:
|
|||
astro: link:../../..
|
||||
vue: 3.2.40
|
||||
|
||||
packages/astro/test/benchmark/simple:
|
||||
specifiers:
|
||||
'@astrojs/node': workspace:*
|
||||
astro: workspace:*
|
||||
dependencies:
|
||||
'@astrojs/node': link:../../../../integrations/node
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/0-css:
|
||||
specifiers:
|
||||
'@astrojs/react': workspace:*
|
||||
|
|
Loading…
Reference in a new issue