diff --git a/packages/ssr/src/firebase/serve-storefront.ts b/packages/ssr/src/firebase/serve-storefront.ts index 25b4abe09..6e0077ad5 100644 --- a/packages/ssr/src/firebase/serve-storefront.ts +++ b/packages/ssr/src/firebase/serve-storefront.ts @@ -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$/, ''); @@ -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('' - + `` - + ''); + + '` + + `${err.toString()}`); } 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()); } }; @@ -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); } }); };