Skip to content

Commit

Permalink
fix(ssr): Fix fallback and errors and skip cache/redirect handled by …
Browse files Browse the repository at this point in the history
…Astro SSR
  • Loading branch information
leomp12 committed Sep 10, 2022
1 parent 7fafde1 commit abe19ae
Showing 1 changed file with 13 additions and 37 deletions.
50 changes: 13 additions & 37 deletions packages/ssr/src/firebase/serve-storefront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import type { Request, Response } from 'firebase-functions';
import { join as joinPath } from 'path';
import { readFile } from 'fs/promises';

const {
STOREFRONT_BASE_DIR,
STOREFRONT_LONG_CACHE,
} = process.env;

const { STOREFRONT_BASE_DIR } = process.env;
const baseDir = STOREFRONT_BASE_DIR || process.cwd();
const clientRoot = new URL(joinPath(baseDir, 'dist/client/'), import.meta.url);
const isLongCache = String(STOREFRONT_LONG_CACHE).toLowerCase() === 'true';

export default (req: Request, res: Response) => {
const url = req.url.replace(/\?.*$/, '').replace(/\.html$/, '');
Expand All @@ -24,33 +19,16 @@ export default (req: Request, res: Response) => {
);
};

const redirect = (toUrl: string, status = 302) => {
let sMaxAge = status === 301 ? 360 : 12;
if (isLongCache) {
sMaxAge *= 10;
}
let cacheControl = `public, max-age=30, s-maxage=${sMaxAge}`;
if (status === 302) {
cacheControl += ', proxy-revalidate';
}
setStatusAndCache(status, cacheControl)
.set('Location', toUrl).end();
};

const fallback = (status = 404) => {
const is404 = status === 404;
if (is404 && url.slice(-1) === '/') {
redirect(url.slice(0, -1));
} else if (url !== `/${status}` && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
setStatusAndCache(status, `public, max-age=${(isLongCache ? 120 : 30)}`)
const fallback = (err: any, status = 500) => {
if (url !== '/fallback' && (/\/[^/.]+$/.test(url) || /\.x?html$/.test(url))) {
setStatusAndCache(status, 'public, max-age=120')
.send('<html><head>'
+ `<meta http-equiv="refresh" content="0; url=/${status}?url=${encodeURIComponent(url)}"/>`
+ '</head><body></body></html>');
+ '<meta http-equiv="refresh" content="0; '
+ `url=/fallback?status=${status}&url=${encodeURIComponent(url)}"/>`
+ `</head><body>${err.toString()}</body></html>`);
} else {
setStatusAndCache(status, isLongCache && is404
? 'public, max-age=60, s-maxage=86400'
: 'public, max-age=60, s-maxage=300')
.end();
setStatusAndCache(status, 'public, max-age=120, s-maxage=600')
.send(err.toString());
}
};

Expand All @@ -61,19 +39,17 @@ export default (req: Request, res: Response) => {
*/
global.ssr_handler(req, res, async (err: any) => {
if (err) {
res.set('X-SSR-Error', err.stack);
fallback(500);
res.set('X-SSR-Error', err.message);
fallback(err);
return;
}
const local = new URL(`.${url}`, clientRoot);
try {
const data = await readFile(local);
setStatusAndCache(200, isLongCache
? 'public, max-age=60, s-maxage=604800'
: 'public, max-age=60, s-maxage=600, stale-while-revalidate=2592000')
setStatusAndCache(200, 'public, max-age=60, s-maxage=600')
.send(data);
} catch {
fallback();
fallback(err, 404);
}
});
};

0 comments on commit abe19ae

Please sign in to comment.