@@ -38,6 +38,62 @@ const cacheBustingMiddleware: PfeMiddleware = () => async function(ctx, next) {
3838 return next ( ) ;
3939} ;
4040
41+ /**
42+ * Redirects both /elements and /elements/ to the root
43+ * Handles both cases: /elements → / and /elements/ → /
44+ */
45+ const elementsToRootRedirectMiddleware : PfeMiddleware = ( ) => ctx => ctx . redirect ( '/' ) ;
46+
47+ /**
48+ * Ensures trailing slash for component URLs
49+ * FROM: `/elements/footer`
50+ * TO: `/elements/footer/`
51+ * @param config normalized PFE dev server config
52+ */
53+ const ensureTrailingSlashMiddleware : PfeMiddleware = ( ) => ( ctx , next ) => {
54+ // Only add trailing slash if path doesn't already end with one
55+ if ( ! ctx . path . endsWith ( '/' ) ) {
56+ return ctx . redirect ( `${ ctx . path } /` ) ;
57+ }
58+ // If it already ends with slash, continue to next middleware
59+ return next ( ) ;
60+ } ;
61+
62+ /**
63+ * Handles lightdom CSS files that are missing the tag name in the path
64+ * FROM: `elements/pf-jazz-hands-lightdom.css` or `elements/pf-jazz-hands-lightdom-shim.css`
65+ * TO: `elements/pf-jazz-hands/pf-jazz-hands-lightdom.css` or `elements/pf-jazz-hands/pf-jazz-hands-lightdom-shim.css`
66+ * @param config normalized PFE dev server config
67+ */
68+ const lightdomShortPathMiddleware : PfeMiddleware = config => ( ctx , next ) => {
69+ const { sheetName, suffix } = ctx . params ;
70+ // Extract tag name from sheet name (e.g., "rh-footer-lightdom" -> "rh-footer")
71+ const tagName = sheetName . replace ( / - l i g h t d o m $ / , '' ) ;
72+ // Keep the full sheetName including -lightdom part for the redirect
73+ const redirect = `/${ config . elementsDir } /${ tagName } /${ tagName } -lightdom${ suffix ?? '' } .css` ;
74+ if ( ctx . path !== redirect ) {
75+ return ctx . redirect ( redirect ) ;
76+ } else {
77+ return next ( ) ;
78+ }
79+ } ;
80+
81+ /**
82+ * Handles lightdom CSS files accessed from demo directory
83+ * FROM: `components/pf-jazz-hands/demo/pf-jazz-hands-lightdom.css`
84+ * TO: `elements/pf-jazz-hands/pf-jazz-hands-lightdom.css`
85+ * @param config normalized PFE dev server config
86+ */
87+ const demoLightdomMiddleware : PfeMiddleware = config => ( ctx , next ) => {
88+ const { tagName, sheetName, suffix } = ctx . params ;
89+ const redirect = `/${ config . elementsDir } /${ tagName } /${ sheetName } ${ suffix ?? '' } .css` ;
90+ if ( ctx . path !== redirect ) {
91+ return ctx . redirect ( redirect ) ;
92+ } else {
93+ return next ( ) ;
94+ }
95+ } ;
96+
4197/**
4298 * Loads the typescript sources for element declaration source requests
4399 * This is useful when the typescript build runs in parallel.
@@ -96,6 +152,10 @@ export function pfeDevServerRouterMiddleware(
96152 const router = new Router ( ) ;
97153 const shim = lightdomShimMiddleware ( config ) ;
98154 const demo = demoSubresourceMiddleware ( config ) ;
155+ const shortPath = lightdomShortPathMiddleware ( config ) ;
156+ const demoLightdom = demoLightdomMiddleware ( config ) ;
157+ const trailingSlash = ensureTrailingSlashMiddleware ( config ) ;
158+ const elementsRedirect = elementsToRootRedirectMiddleware ( config ) ;
99159 return router
100160 . get ( '/tools/pfe-tools/environment.js(.js)?' , environmentMiddleware ( config ) )
101161 . get ( `/core/pfe-core/:splatPath*.js` , coreMiddleware ( config ) )
@@ -107,5 +167,13 @@ export function pfeDevServerRouterMiddleware(
107167 . get ( `/${ componentSubpath } /:unprefixedElementSlug/:sheetName-lightdom.css` , shim )
108168 . get ( `/${ componentSubpath } /:unprefixedElementSlug/demo/:demoName/:fileName.:ext` , demo )
109169 . get ( `/${ componentSubpath } /:unprefixedElementSlug/demo/:fileName.:ext` , demo )
170+ . get ( `/${ componentSubpath } /:sheetName-lightdom:suffix.css` , shortPath )
171+ . get ( `/${ componentSubpath } /:sheetName-lightdom.css` , shortPath )
172+ . get ( `/${ elementsDir } /:tagName/demo/:sheetName-lightdom:suffix.css` , demoLightdom )
173+ . get ( `/${ elementsDir } /:tagName/demo/:sheetName-lightdom.css` , demoLightdom )
174+ . get ( `/${ componentSubpath } /:unprefixedElementSlug/demo` , trailingSlash )
175+ . get ( `/${ componentSubpath } /:unprefixedElementSlug` , trailingSlash )
176+ . get ( `/${ componentSubpath } ` , elementsRedirect )
177+ . get ( `/${ componentSubpath } /` , elementsRedirect )
110178 . routes ( ) ;
111179}
0 commit comments