File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import passport from "./config/passport.config.js";
77import errorHandler from "./middlewares/errorHandler.middlewares.js" ;
88import { requestLogger } from "./middlewares/requestLogger.middlewares.js" ;
99import routes from "./api.routes.js" ;
10+ import { serveFrontend } from "./static/serveFrontend.js" ;
1011
1112const __filename = fileURLToPath ( import . meta. url ) ;
1213const __dirname = path . dirname ( __filename ) ;
@@ -21,20 +22,10 @@ app.use(requestLogger);
2122app . use ( passport . initialize ( ) ) ;
2223app . use ( express . json ( ) ) ;
2324app . use ( cookieParser ( ) ) ;
24- app . use ( express . static ( clientDistPath ) ) ;
2525
2626app . use ( "/api/v1" , routes ) ;
2727
28- app . get ( "/*splat" , ( req , res , next ) => {
29- if ( req . path . startsWith ( "/api/" ) ) {
30- return next ( ) ;
31- }
32- res . sendFile ( path . join ( clientDistPath , "index.html" ) , ( err ) => {
33- if ( err ) {
34- next ( ) ;
35- }
36- } ) ;
37- } ) ;
28+ serveFrontend ( app , clientDistPath ) ;
3829
3930app . use ( errorHandler ) ;
4031
Original file line number Diff line number Diff line change 1+ import express from "express" ;
2+ import path from "path" ;
3+
4+ export function serveFrontend ( app : express . Express , clientDistPath : string ) {
5+ app . use (
6+ "/assets" ,
7+ express . static ( path . join ( clientDistPath , "assets" ) , {
8+ immutable : true ,
9+ maxAge : "1y" ,
10+ etag : false ,
11+ } ) ,
12+ ) ;
13+
14+ app . use (
15+ express . static ( clientDistPath , {
16+ etag : false ,
17+ maxAge : 0 ,
18+ setHeaders ( res , filePath ) {
19+ if ( filePath . endsWith ( ".html" ) ) {
20+ res . setHeader ( "Cache-Control" , "public, max-age=0, must-revalidate" ) ;
21+ }
22+ } ,
23+ } ) ,
24+ ) ;
25+
26+ app . get ( "/*splat" , ( _req , res , next ) => {
27+ res . setHeader ( "Cache-Control" , "public, max-age=0, must-revalidate" ) ;
28+
29+ res . sendFile ( path . join ( clientDistPath , "index.html" ) , ( err ) => {
30+ if ( err ) {
31+ next ( err ) ;
32+ }
33+ } ) ;
34+ } ) ;
35+ }
You can’t perform that action at this time.
0 commit comments