Skip to content

Commit

Permalink
gzip responses
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Mar 2, 2021
1 parent 8339ba2 commit ba879cc
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 33 deletions.
3 changes: 3 additions & 0 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@sveltejs/app-utils": "workspace:*",
"compression": "^1.7.4",
"polka": "^0.5.2",
"rollup": "^2.38.3",
"sirv": "^1.0.11"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

export default {
input: 'src/server.js',
Expand All @@ -8,6 +9,6 @@ export default {
format: 'esm',
sourcemap: true
},
plugins: [nodeResolve(), commonjs()],
plugins: [nodeResolve(), commonjs(), json()],
external: ['./app.js', ...require('module').builtinModules]
};
54 changes: 27 additions & 27 deletions packages/adapter-node/src/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import compression from 'compression';
import * as fs from 'fs';
import * as http from 'http';
import polka from 'polka';
import { dirname, join } from 'path';
import { parse, URLSearchParams, fileURLToPath } from 'url';
import sirv from 'sirv';
import { parse, URLSearchParams, fileURLToPath } from 'url';
import { get_body } from '@sveltejs/app-utils/http';
// App is a dynamic file built from the application layer.
/*eslint import/no-unresolved: [2, { ignore: ['\.\/app\.js$'] }]*/
Expand All @@ -27,30 +28,29 @@ const assets_handler = sirv(join(__dirname, '/assets'), {
immutable: true
});

const server = http.createServer((req, res) => {
const parsed = parse(req.url || '');

assets_handler(req, res, () => {
prerendered_handler(req, res, async () => {
const rendered = await app.render({
method: req.method,
headers: req.headers, // TODO: what about repeated headers, i.e. string[]
path: parsed.pathname,
body: await get_body(req),
query: new URLSearchParams(parsed.query || '')
});

if (rendered) {
res.writeHead(rendered.status, rendered.headers);
res.end(rendered.body);
} else {
res.statusCode = 404;
res.end('Not found');
}
polka()
.use(compression({ threshold: 0 }), assets_handler, prerendered_handler, async (req, res) => {
const parsed = parse(req.url || '');
const rendered = await app.render({
method: req.method,
headers: req.headers, // TODO: what about repeated headers, i.e. string[]
path: parsed.pathname,
body: await get_body(req),
query: new URLSearchParams(parsed.query || '')
});
});
});

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
if (rendered) {
res.writeHead(rendered.status, rendered.headers);
res.end(rendered.body);
} else {
res.statusCode = 404;
res.end('Not found');
}
})
.listen(PORT, (err) => {
if (err) {
console.log('error', err);
} else {
console.log(`Listening on port ${PORT}`);
}
});
118 changes: 113 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba879cc

Please sign in to comment.