Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@sveltejs/adapter-node generates corrupted server #5431

Closed
UnlimitedBytes opened this issue Jul 8, 2022 · 30 comments
Closed

@sveltejs/adapter-node generates corrupted server #5431

UnlimitedBytes opened this issue Jul 8, 2022 · 30 comments
Labels
bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users

Comments

@UnlimitedBytes
Copy link

UnlimitedBytes commented Jul 8, 2022

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

  1. Clone the repository to your local system
  2. Navigate into the repository directory
  3. Install all dependencies using npm install
  4. Build the production server using npm run build
  5. Start the production server using node build
  6. 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

@UnlimitedBytes
Copy link
Author

Updated information to reflect this only occures after 1.0.0-next.79

@benmccann
Copy link
Member

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...

@Cluster2a
Copy link

Confirmed - got the same problem!

@benmccann
Copy link
Member

Ah, the Demo project works because it prerenders the index page while the Skeleton project does not

@benmccann benmccann added bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users labels Jul 8, 2022
@benmccann
Copy link
Member

benmccann commented Jul 8, 2022

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

@DrDanRyan
Copy link

DrDanRyan commented Jul 8, 2022

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!?

@benmccann
Copy link
Member

@DrDanRyan that's because npm run preview will use the Vite preview server and not the adapted build

@benmccann
Copy link
Member

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...

@benmccann
Copy link
Member

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

@SeaniaTwix
Copy link

but curl works! lol

@benmccann
Copy link
Member

Fix here: 810c52b

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

@itssumitrai
Copy link

Thanks for the issue, I was trying to figure out what went wrong. Facing the same issue, our production deployment is failing

jason0x43 added a commit to jason0x43/spellingcee that referenced this issue Jul 9, 2022
Bluebie added a commit to auslan-find-sign/find-sign-website that referenced this issue Jul 9, 2022
@UltraCakeBakery
Copy link

UltraCakeBakery commented Jul 9, 2022

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

@fernandolguevara
Copy link
Contributor

this issue is related to compression package

@UltraCakeBakery
Copy link

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.

@coryvirok
Copy link
Contributor

Pinning next.77 works for now while the compression PR is being reviewed.

"@sveltejs/adapter-node": "1.0.0-next.77",

@xpat
Copy link

xpat commented Jul 11, 2022

@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

@fernandolguevara
Copy link
Contributor

@xpat @SeaniaTwix the reason it works on curl is bc you are not sending the Accept-Encoding: gzip header on the request

@fernandolguevara
Copy link
Contributor

fernandolguevara commented Jul 12, 2022

@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 };

@d3tr0id
Copy link

d3tr0id commented Jul 12, 2022

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

@d3tr0id
Copy link

d3tr0id commented Jul 12, 2022

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

Nvm it doesn't matter... I forgot to npm ci after I changed the adapter-node version to 77

asoltys added a commit to tokenocean/raretoshi that referenced this issue Jul 13, 2022
asoltys added a commit to coinos/coinos-marketing that referenced this issue Jul 13, 2022
@edil-it-them
Copy link

Hi there. Downgrading to node adapter v77 not working for me. Tried with sveltekit v370, v366
Still seeing blank page

@d3tr0id
Copy link

d3tr0id commented Jul 13, 2022

Hi there. Downgrading to node adapter v77 not working for me. Tried with sveltekit v370, v366
Still seeing blank page

Delete package-lock.json and do npm ci then it should work

@edil-it-them
Copy link

Hi there. Downgrading to node adapter v77 not working for me. Tried with sveltekit v370, v366
Still seeing blank page

Delete package-lock.json and do npm ci then it should work

Still no result. I'm using vite v3.0.0

@shynome
Copy link

shynome commented Jul 13, 2022

comment build/index.js compress will make it work
sed -i 's|compression$1.exports({ threshold: 0 })|//compression$1.exports({ threshold: 0 })|g' build/index.js

@xpat
Copy link

xpat commented Jul 13, 2022

@shynome That worked. Nice.

@benmccann
Copy link
Member

will be fixed in the next version of adapter-node which removes compression at least until the bug is fixed

@CaptainCodeman
Copy link
Contributor

FYI: Still hangs for me on anything above node-adapter 1.0.0-next-78

@benmccann
Copy link
Member

It wasn't released yet. Try @sveltejs/adapter-node version 1.0.0-next.81

@CaptainCodeman
Copy link
Contributor

Oh, ha - that would do it! I just saw a new version and connected the dots to make a whole different picture 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p0-urgent SvelteKit is broken or vulnerable for most users
Projects
None yet
Development

No branches or pull requests