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

Fix/#638 #837

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -198,13 +197,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: 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 { error: error, build: null as unknown as ServerBuild }
}
}

if (!ALLOW_INDEXING) {
Expand All @@ -222,7 +227,14 @@ app.all(
serverBuild: getBuild(),
}),
mode: MODE,
build: getBuild,
build: async () => {
const { error, build } = await getBuild()
// gracefully "catch" the error
if (error) {
throw error
}
return build
},
}),
)

Expand Down