@sveltejs/adapter-node generates corrupted server
Describe the bug
⚠ Disclaimer
First of all I'm not a native english speaker so please forgive my sometimes bad english! ❤
Second of all I'm not entirely sure if this issue belongs to sveltejs/kit or more to sveltejs/kit/adapter-node but as they both are on the same repository I'm posting this issue here anyway.
🐞 The bug
EDIT: This issue was introduced in 1.0.0-next.79 as my other project with 1.0.0-next.78 works fine.
While testing some stuff out for further information on #5412 I discovered the inability to generate production servers using adapter-node.
When building a production server using @sveltejs/adapter-node the generated server takes forever to respond (running in clientTimeout after 300s). When I switch the handler polka middleware inside build/index.js to a custom handler like so:
const server = polka().use(
// https://github.com/lukeed/polka/issues/173
// @ts-ignore - nothing we can do about so just ignore it
compression$1.exports({ threshold: 0 }),
(req, res) => res.end('Hello world')
);
and run node build the server instantly responds with Hello world so the issue is somewhere in the sveltekit handler.
🌟A solution
I don't know what exactly causes this problem so I can't give any solution 😥.
But this needs to be fixed somehow as it makes production builds using adapter-node impossible!
Reproduction
Manual reproduction
Repository: https://github.com/UnlimitedBytes/sveltekit-adapter-node-issue
- Clone the repository to your local system
- Navigate into the repository directory
- Install all dependencies using
npm install - Build the production server using
npm run build - Start the production server using
node build - Open your webbrowser and navigate to
http://127.0.0.1:3000
FAST (automatic) reproduction
Execute the following commands
git clone https://github.com/UnlimitedBytes/sveltekit-adapter-node-issue
cd sveltekit-adapter-node-issue
npm install
npm run build
node build
Open your webbrowser and navigate to http://127.0.0.1:3000
Logs
Console:
> Listening on 0.0.0.0:3000
Browser:
Loading for ever
System Info
System:
OS: Windows 10 10.0.19044
CPU: (8) x64 AMD Ryzen™ 7 5800X3D @ 4.50GHz
Memory: 6.05 GB / 32.00 GB
Binaries:
Node: 18.4.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.13.1 - ~\AppData\Roaming\npm\npm.CMD
Browsers:
Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.44)
Internet Explorer: 11.0.19041.1566
npmPackages:
@sveltejs/adapter-node: ^1.0.0-next.79 => 1.0.0-next.79
@sveltejs/kit: next => 1.0.0-next.366
svelte: ^3.44.0 => 3.49.0
vite: ^2.9.13 => 2.9.14
Severity
EDIT: blocking an upgrade (as this issue was introduced by .79 as I found out now)
Additional Information
No response
Updated information to reflect this only occures after 1.0.0-next.79
Hmm. It looks like adapter-node works with the Demo project, but is broken with the Skeleton project. I'm not sure what the difference is...
Confirmed - got the same problem!
Ah, the Demo project works because it prerenders the index page while the Skeleton project does not
It works if I pin adapter-node to 1.0.0-next.78. Only change is a TypeScript upgrade from 4.7.2 to 4.7.4 (changelog). WTF TypeScript
Using 1.0.0-next.79 I can still use npm preview and getting a working site locally, but when I deploy to production it does not work... I don't understand how that is possible!?
@DrDanRyan that's because npm run preview will use the Vite preview server and not the adapted build
I pinned TypeScript to 4.7.2 in @sveltejs/kit, @sveltejs/adapter-node, and sites/kit.svelte.dev and still had the site hang for me with adapter-node. So assuming I did that correctly and didn't screw anything up, now I have no idea what's going on...
Well and plus the adapter-node source is written in JS, so it's not like the TypeScript compiler would even have a chance to screw things up anyway, so that's unlikely to be the cause
but curl works! lol
Fix here: https://github.com/sveltejs/kit/commit/810c52bd2729e9a0996ce7049826678892b61627
I accidentally screwed up and pushed it to master rather than sending a PR. I'll wait for a review from the other maintainers before releasing it
Thanks for the issue, I was trying to figure out what went wrong. Facing the same issue, our production deployment is failing
Does this also effect adapter-auto? Having some issues right after upgrading. Downgrading doesn't help. I'm thinking about blaming the new vite.config.js setup
this issue is related to compression package
Does this also effect adapter-auto? Having some issues right after upgrading. Downgrading doesn't help. I'm thinking about blaming the new vite.config.js setup
This happend due to the order of my vite plugins in my vite.config.js. Apparently SvelteKit needed to run AFTER everything else.
Pinning next.77 works for now while the compression PR is being reviewed.
"@sveltejs/adapter-node": "1.0.0-next.77",
@SeaniaTwix Curl works for mine, too. @coryvirok Pinning to earlier versions hasn't worked for me. I've tried 78 and 77. The page remains blank.
Request Method: GET
Status Code: 200
Remote Address: 35.193.8.13:443
Referrer Policy: strict-origin-when-cross-origin
content-encoding: gzip
content-type: text/html
date: Mon, 11 Jul 2022 23:40:45 GMT
etag: "wx1tjj"
server: nginx/1.18.0 (Ubuntu)
vary: Accept-Encoding
:authority: antoine.patraldo.com
:method: GET
:path: /
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,es-US;q=0.8,es;q=0.7,fr-CA;q=0.6,fr;q=0.5,ca;q=0.4
cache-control: max-age=0
dnt: 1
sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Chrome OS"
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (X11; CrOS x86_64 14816.82.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
@xpat @SeaniaTwix the reason it works on curl is bc you are not sending the Accept-Encoding: gzip header on the request
@benmccann can we have an environment var to toggle the compression middleware? this could work as workaround for this issue
as I understand, those users with a proxy in front of the node process, like nginx/apache, could run the compression in that context
// packages/adapter-node/src/env.js
/* global ENV_PREFIX */
const expected = new Set([
'SOCKET_PATH',
'HOST',
'PORT',
'ORIGIN',
'XFF_DEPTH',
'ADDRESS_HEADER',
'PROTOCOL_HEADER',
'HOST_HEADER',
'COMPRESSION_ENABLED'
]);
if (ENV_PREFIX) {
for (const name in process.env) {
if (name.startsWith(ENV_PREFIX)) {
const unprefixed = name.slice(ENV_PREFIX.length);
if (!expected.has(unprefixed)) {
throw new Error(
`You should change envPrefix (${ENV_PREFIX}) to avoid conflicts with existing environment variables — unexpectedly saw ${name}`
);
}
}
}
}
/**
* @param {string} name
* @param {any} fallback
*/
export function env(name, fallback) {
const prefixed = ENV_PREFIX + name;
return prefixed in process.env ? process.env[prefixed] : fallback;
}
// packages/adapter-node/src/index.js
import { handler } from './handler.js';
import { env } from './env.js';
import compression from 'compression';
import polka from 'polka';
export const path = env('SOCKET_PATH', false);
export const host = env('HOST', '0.0.0.0');
export const port = env('PORT', !path && '3000');
export const compression_enabled = env('COMPRESSION_ENABLED', 'true') === 'true';
const compression_middleware = compression_enabled ? compression({ threshold: 0 }) : undefined;
const middlewares = [compression_middleware, handler].filter(Boolean);
// https://github.com/lukeed/polka/issues/173
// @ts-ignore - nothing we can do about so just ignore it
const server = polka().use(...middlewares);
server.listen({ path, host, port }, () => {
console.log(`Listening on ${path ? path : host + ':' + port}`);
});
export { server };
Pinning next.77 works for now while the compression PR is being reviewed.
"@sveltejs/adapter-node": "1.0.0-next.77",
Which sveltekit version are you using meanwhile? @coryvirok