From 2b66b6e7c262980ca36190dea4cf10d9fe5e6f5b Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 Sep 2024 18:53:11 +0000 Subject: [PATCH 1/3] fix #638 --- server/index.ts | 57 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/server/index.ts b/server/index.ts index 6a5cf026..7fc23006 100644 --- a/server/index.ts +++ b/server/index.ts @@ -22,11 +22,11 @@ if (IS_PROD && process.env.SENTRY_DSN) { const viteDevServer = IS_PROD ? undefined - : await import('vite').then((vite) => - vite.createServer({ + : await import('vite').then((vite) => { + return vite.createServer({ server: { middlewareMode: true }, - }), - ) + }) + }) const app = express() @@ -39,7 +39,7 @@ app.set('trust proxy', true) // ensure HTTPS only (X-Forwarded-Proto comes from Fly) app.use((req, res, next) => { if (req.method !== 'GET') return next() - + const proto = req.get('X-Forwarded-Proto') const host = getHost(req) if (proto === 'http') { @@ -198,13 +198,19 @@ app.use((req, res, next) => { }) async function getBuild() { - const build = viteDevServer - ? viteDevServer.ssrLoadModule('virtual:remix/server-build') - : // @ts-ignore this should exist before running the server - // but it may not exist just yet. - await import('../build/server/index.js') - // not sure how to make this happy 🤷‍♂️ - return build as unknown as ServerBuild + try { + const build = viteDevServer + ? await viteDevServer.ssrLoadModule('virtual:remix/server-build') + // @ts-expect-error - the file might not exist yet but it will + // eslint-disable-next-line import/no-unresolved + : await import('../build/server/index.js') + + return build as unknown as ServerBuild + } catch (error) { + // Catch error and return null to make express happy and avoid an unrecoverable crash + console.error('Error creating build:', error) + return null + } } if (!ALLOW_INDEXING) { @@ -216,13 +222,25 @@ if (!ALLOW_INDEXING) { app.all( '*', + (req, res, next) => { + next() + }, createRequestHandler({ - getLoadContext: (_: any, res: any) => ({ - cspNonce: res.locals.cspNonce, - serverBuild: getBuild(), - }), + getLoadContext: (_: any, res: any) => { + return { + cspNonce: res.locals.cspNonce, + serverBuild: getBuild(), + } + }, mode: MODE, - build: getBuild, + build: async () => { + const build = await getBuild() + // gracefully "catch" the error + if (!build) { + throw new Error('Error creating build') + } + return build + }, }), ) @@ -269,3 +287,8 @@ closeWithGrace(async () => { server.close((e) => (e ? reject(e) : resolve('ok'))) }) }) + +// Add a global error handler for the Express app +app.use((err: any, req: any, res: any, next: any) => { + res.status(500).send('Internal Server Error') +}) From 4460029e41244cc6aa23c24d6f8dc64ecb6720e1 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 Sep 2024 19:24:50 +0000 Subject: [PATCH 2/3] fix #638 #690 --- server/index.ts | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/server/index.ts b/server/index.ts index 7fc23006..eac728c7 100644 --- a/server/index.ts +++ b/server/index.ts @@ -22,11 +22,11 @@ if (IS_PROD && process.env.SENTRY_DSN) { const viteDevServer = IS_PROD ? undefined - : await import('vite').then((vite) => { - return vite.createServer({ + : await import('vite').then((vite) => + vite.createServer({ server: { middlewareMode: true }, - }) - }) + }), + ) const app = express() @@ -201,15 +201,15 @@ async function getBuild() { try { const build = viteDevServer ? await viteDevServer.ssrLoadModule('virtual:remix/server-build') - // @ts-expect-error - the file might not exist yet but it will - // eslint-disable-next-line import/no-unresolved - : await import('../build/server/index.js') - - return build as unknown as ServerBuild + : // @ts-expect-error - the file might not exist yet but it will + // eslint-disable-next-line import/no-unresolved + await import('../build/server/index.js') + + return { build: build as unknown as ServerBuild, error: null } } catch (error) { // Catch error and return null to make express happy and avoid an unrecoverable crash console.error('Error creating build:', error) - return null + return { error: error, build: null as unknown as ServerBuild } } } @@ -222,22 +222,17 @@ if (!ALLOW_INDEXING) { app.all( '*', - (req, res, next) => { - next() - }, createRequestHandler({ - getLoadContext: (_: any, res: any) => { - return { - cspNonce: res.locals.cspNonce, - serverBuild: getBuild(), - } - }, + getLoadContext: (_: any, res: any) => ({ + cspNonce: res.locals.cspNonce, + serverBuild: getBuild(), + }), mode: MODE, build: async () => { - const build = await getBuild() + const { error, build } = await getBuild() // gracefully "catch" the error - if (!build) { - throw new Error('Error creating build') + if (error) { + throw error } return build }, @@ -287,8 +282,3 @@ closeWithGrace(async () => { server.close((e) => (e ? reject(e) : resolve('ok'))) }) }) - -// Add a global error handler for the Express app -app.use((err: any, req: any, res: any, next: any) => { - res.status(500).send('Internal Server Error') -}) From c0b0a694a19e7ba700d1fc88796f967222110357 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 Sep 2024 19:37:27 +0000 Subject: [PATCH 3/3] remove nl --- server/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/index.ts b/server/index.ts index eac728c7..4575e78e 100644 --- a/server/index.ts +++ b/server/index.ts @@ -39,7 +39,6 @@ app.set('trust proxy', true) // ensure HTTPS only (X-Forwarded-Proto comes from Fly) app.use((req, res, next) => { if (req.method !== 'GET') return next() - const proto = req.get('X-Forwarded-Proto') const host = getHost(req) if (proto === 'http') {