1
1
import "dotenv/config" ;
2
2
// started with https://developers.cloudflare.com/workers/get-started/quickstarts/
3
3
import express from "express" ;
4
- import { createRequestHandler } from "@react-router/express" ;
5
4
// import { broadcastDevReady } from "react-router";
6
5
// import path from "path";
7
6
import { verifyKey } from "discord-interactions" ;
@@ -18,31 +17,7 @@ import * as report from "~/commands/report";
18
17
import * as track from "~/commands/track" ;
19
18
import setupTicket from "~/commands/setupTickets" ;
20
19
21
- declare module "react-router" {
22
- // Your AppLoadContext used in v2
23
- interface AppLoadContext {
24
- whatever : string ;
25
- }
26
-
27
- // TODO: remove this once we've migrated to `Route.LoaderArgs` instead for our loaders
28
- interface LoaderFunctionArgs {
29
- context : AppLoadContext ;
30
- }
31
-
32
- // TODO: remove this once we've migrated to `Route.ActionArgs` instead for our actions
33
- interface ActionFunctionArgs {
34
- context : AppLoadContext ;
35
- }
36
- }
37
-
38
20
// const BUILD_DIR = path.join(process.cwd(), "build");
39
- const viteDevServer = isProd ( )
40
- ? undefined
41
- : await import ( "vite" ) . then ( ( vite ) =>
42
- vite . createServer ( {
43
- server : { origin : "localhost:3000" , middlewareMode : true } ,
44
- } ) ,
45
- ) ;
46
21
47
22
const app = express ( ) ;
48
23
@@ -56,16 +31,37 @@ app.use(Sentry.Handlers.requestHandler());
56
31
Route handlers and static hosting
57
32
*/
58
33
59
- if ( viteDevServer ) {
60
- app . use ( viteDevServer . middlewares ) ;
61
- } else {
34
+ if ( isProd ( ) ) {
35
+ console . log ( "Starting production server" ) ;
62
36
app . use (
63
37
"/assets" ,
64
- express . static ( "build/client/assets" , {
65
- immutable : true ,
66
- maxAge : "1y" ,
38
+ express . static ( "build/client/assets" , { immutable : true , maxAge : "1y" } ) ,
39
+ ) ;
40
+ app . use ( express . static ( "build/client" , { maxAge : "1h" } ) ) ;
41
+ // @ts -expect-error Because this is a weird circular import that React Router wants for custom server integrations
42
+ // https://github.com/remix-run/react-router-templates/blob/f1a43a647de66d811b85d7f96d3b57b7e29ce0e7/node-custom-server/server.js#L41
43
+ const source = await import ( "./build/server/index.js" ) ;
44
+ console . log ( { source } ) ;
45
+ app . use ( source . app ) ;
46
+ } else {
47
+ console . log ( "Starting development server" ) ;
48
+ const viteDevServer = await import ( "vite" ) . then ( ( vite ) =>
49
+ vite . createServer ( {
50
+ server : { middlewareMode : true } ,
67
51
} ) ,
68
52
) ;
53
+ app . use ( viteDevServer . middlewares ) ;
54
+ viteDevServer
55
+ . ssrLoadModule ( "./app/server.ts" )
56
+ . then ( ( source ) => {
57
+ app . use ( source . app ) ;
58
+ } )
59
+ . catch ( ( error ) => {
60
+ if ( typeof error === "object" && error instanceof Error ) {
61
+ viteDevServer . ssrFixStacktrace ( error ) ;
62
+ }
63
+ console . log ( { error } ) ;
64
+ } ) ;
69
65
}
70
66
app . use ( express . static ( "build/client" , { maxAge : "1h" } ) ) ;
71
67
@@ -105,21 +101,6 @@ registerCommand(report);
105
101
registerCommand ( track ) ;
106
102
registerCommand ( setupTicket ) ;
107
103
108
- const build = viteDevServer
109
- ? ( ) => viteDevServer . ssrLoadModule ( "virtual:react-router/server-build" )
110
- : // @ts -ignore This breaks when `build/` doesn't exist, like during CI
111
- await import ( "../build/server/index.js" ) ;
112
-
113
- // needs to handle all verbs (GET, POST, etc.)
114
- app . all (
115
- "*" ,
116
- createRequestHandler ( {
117
- // `remix build` and `remix dev` output files to a build directory, you need
118
- // to pass that build to the request handler
119
- build,
120
- } ) ,
121
- ) ;
122
-
123
104
/** ERROR TRACKING
124
105
Must go after route handlers
125
106
*/
0 commit comments