Skip to content

Commit 61e2990

Browse files
committed
fix(tools): correct lightdom css routing, improve trailing slash behavior
1 parent 1ed25fb commit 61e2990

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

tools/pfe-tools/dev-server/plugins/dev-server-router.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/-lightdom$/, '');
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
}

tools/pfe-tools/dev-server/plugins/dev-server-templates.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ export function pfeDevServerTemplateMiddleware(config: PfeDevServerInternalConfi
6969
if (config.loadDemo && !(method !== 'HEAD' && method !== 'GET' || path.includes('.'))) {
7070
const url = new URL(ctx.request.url, `http://${ctx.request.headers.host}`);
7171
const demos = await getDemos(config);
72-
const demo = demos.find(x => x.permalink === url.pathname);
72+
// const demo = demos.find(x => x.permalink === url.pathname);
73+
let demo = demos.find(x => x.permalink === url.pathname);
74+
// Handle case where URL ends with /demo/ but permalink was shortened to just /
75+
// e.g., request for /compoennts/jazz-hands/demo/ should match demo with permalink /components/jazz-hands/
76+
if (!demo && url.pathname.endsWith('/demo/')) {
77+
const alternativePathname = url.pathname.replace('/demo/', '/');
78+
demo = demos.find(x => x.permalink === alternativePathname);
79+
}
7380
const manifest = demo?.manifest;
7481
const templateContent = await getTemplateContent(demo);
7582
ctx.cwd = process.cwd();

0 commit comments

Comments
 (0)