@@ -100,7 +100,7 @@ export const loadRemoteMiddlewares = async (serverId: string) => {
100100 throw new Error ( `File not found: ${ configPath } ` ) ;
101101 }
102102} ;
103- export const writeMiddleware = < T > ( config : T ) => {
103+ export const writeMiddleware = ( config : FileConfig ) => {
104104 const { DYNAMIC_TRAEFIK_PATH } = paths ( ) ;
105105 const configPath = join ( DYNAMIC_TRAEFIK_PATH , "middlewares.yml" ) ;
106106 const newYamlContent = stringify ( config ) ;
@@ -111,6 +111,18 @@ export const createPathMiddlewares = async (
111111 app : ApplicationNested ,
112112 domain : Domain ,
113113) => {
114+ const { appName } = app ;
115+ const { uniqueConfigKey, internalPath, stripPath, path } = domain ;
116+
117+ // Early return if there's no path middleware to create
118+ const needsInternalPathMiddleware =
119+ internalPath && internalPath !== "/" && internalPath !== path ;
120+ const needsStripPathMiddleware = stripPath && path && path !== "/" ;
121+
122+ if ( ! needsInternalPathMiddleware && ! needsStripPathMiddleware ) {
123+ return ;
124+ }
125+
114126 let config : FileConfig ;
115127
116128 if ( app . serverId ) {
@@ -127,20 +139,19 @@ export const createPathMiddlewares = async (
127139 }
128140 }
129141
130- const { appName } = app ;
131- const { uniqueConfigKey, internalPath, stripPath, path } = domain ;
132-
133- if ( ! config . http ) {
142+ if ( ! config ) {
143+ config = { http : { middlewares : { } } } ;
144+ } else if ( ! config . http ) {
134145 config . http = { middlewares : { } } ;
135146 }
136- if ( ! config . http . middlewares ) {
137- config . http . middlewares = { } ;
147+ if ( ! config . http ? .middlewares ) {
148+ config . http ! . middlewares = { } ;
138149 }
139150
140151 // Add internal path prefix middleware
141152 if ( internalPath && internalPath !== "/" && internalPath !== path ) {
142153 const middlewareName = `addprefix-${ appName } -${ uniqueConfigKey } ` ;
143- config . http . middlewares [ middlewareName ] = {
154+ config . http ! . middlewares [ middlewareName ] = {
144155 addPrefix : {
145156 prefix : internalPath ,
146157 } ,
@@ -150,7 +161,7 @@ export const createPathMiddlewares = async (
150161 // Strip external path middleware if needed
151162 if ( stripPath && path && path !== "/" ) {
152163 const middlewareName = `stripprefix-${ appName } -${ uniqueConfigKey } ` ;
153- config . http . middlewares [ middlewareName ] = {
164+ config . http ! . middlewares [ middlewareName ] = {
154165 stripPrefix : {
155166 prefixes : [ path ] ,
156167 } ,
@@ -184,6 +195,10 @@ export const removePathMiddlewares = async (
184195 }
185196 }
186197
198+ if ( ! config ) {
199+ return ;
200+ }
201+
187202 const { appName } = app ;
188203
189204 if ( config . http ?. middlewares ) {
@@ -194,6 +209,23 @@ export const removePathMiddlewares = async (
194209 delete config . http . middlewares [ stripPrefixMiddleware ] ;
195210 }
196211
212+ if (
213+ config ?. http ?. middlewares &&
214+ Object . keys ( config . http . middlewares ) . length === 0
215+ ) {
216+ // if there aren't any middlewares, remove the whole section
217+ delete config . http . middlewares ;
218+ }
219+
220+ // // If http section is empty, remove it completely
221+ if ( config ?. http && Object . keys ( config ?. http ) . length === 0 ) {
222+ delete config . http ;
223+ }
224+
225+ if ( config && Object . keys ( config || { } ) . length === 0 ) {
226+ config = { } ;
227+ }
228+
197229 if ( app . serverId ) {
198230 await writeTraefikConfigRemote ( config , "middlewares" , app . serverId ) ;
199231 } else {
0 commit comments