Skip to content

Commit

Permalink
fix(ssr): Deal with redirects (hardfix) for multiple buit CSS filepaths
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed May 18, 2024
1 parent 9a19316 commit 6bc97a0
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/ssr/src/lib/serve-storefront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ type BuiltImage = { filename: string, width: number, height: number };
const builtImages: BuiltImage[] = [];

const staticFilepaths: string[] = [];
let cssFilepath: string | undefined;
let mainCssFilepath: string | undefined;
const cssFilepaths: string[] = [];
readFile(joinPath(baseDir, 'dist/server/static-builds.csv'), 'utf-8')
.then((staticBuildsManifest) => {
const cssFiles: string[] = [];
staticBuildsManifest.split(/\n/).forEach((line) => {
const [filepath] = line.split(',');
staticFilepaths.push(filepath);
if (filepath.endsWith('.css')) cssFiles.push(filepath);
});
if (cssFiles.length === 1) {
cssFilepath = cssFiles[0]?.replace('./dist/client/', '/');
if (
cssFilepath
&& cssFilepath.charAt(0) !== '/'
&& !cssFilepath.startsWith('https://')
) {
cssFilepath = `/${cssFilepath}`;
if (filepath.endsWith('.css')) {
const cssFilepath = filepath.replace('./dist/client/', '/');
if (cssFilepath.charAt(0) === '/') {
cssFilepaths.push(cssFilepath);
}
}
});
if (cssFilepaths.length === 1) {
mainCssFilepath = cssFilepaths[0];
} else if (cssFilepaths.length) {
mainCssFilepath = cssFilepaths.find((path) => path.includes('/_slug_.'));
}
})
.catch(warn);
Expand Down Expand Up @@ -76,10 +76,18 @@ export default async (req: Request, res: Response) => {
return;
}
}
if (ext === 'css' && cssFilepath) {
res.set('Cache-Control', 'max-age=3600, s-maxage=300');
redirect(302, cssFilepath);
return;
if (ext === 'css' && cssFilepaths.length) {
const baseFilename = req.path.split('/').pop()?.split('.')[0];
if (baseFilename) {
const cssFilepath = cssFilepaths.find((path) => {
return baseFilename === path.split('/').pop()?.split('.')[0];
});
if (cssFilepath) {
res.set('Cache-Control', 'max-age=3600, s-maxage=300');
redirect(302, cssFilepath);
return;
}
}
}
}

Expand Down Expand Up @@ -222,9 +230,9 @@ export default async (req: Request, res: Response) => {
if (!headers) {
headers = {};
}
if (canSetLinkHeader && status === 200 && cssFilepath) {
if (canSetLinkHeader && status === 200 && mainCssFilepath) {
// https://community.cloudflare.com/t/early-hints-need-more-data-before-switching-over/342888/21
const cssLink = `<${(assetsPrefix || '')}${cssFilepath}>; rel=preload; as=style`;
const cssLink = `<${(assetsPrefix || '')}${mainCssFilepath}>; rel=preload; as=style`;
if (!headers.link) {
headers.Link = cssLink;
} else {
Expand Down

0 comments on commit 6bc97a0

Please sign in to comment.