From 5e657e7547d55e568dc362666666e6b5012f93c5 Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 14 May 2021 20:27:45 -0700 Subject: [PATCH] Fix prod outfit SSR by just using URLs, not build Hmm, the built copy of the HTML isn't deployed with the API function on Vercel. Ok! Let's just do the same trick as we did for the dev server, and make an HTTP request. This is fine enough for perf because we're caching this result locally to the function, plus it should be a fast CDN-cached response anyway. --- api/outfitPageSSR.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/api/outfitPageSSR.js b/api/outfitPageSSR.js index 98ac91f7..2f626f1c 100644 --- a/api/outfitPageSSR.js +++ b/api/outfitPageSSR.js @@ -23,8 +23,6 @@ const beeline = require("honeycomb-beeline")({ import escapeHtml from "escape-html"; import fetch from "node-fetch"; -import { promises as fs } from "fs"; -import * as path from "path"; import connectToDb from "../src/server/db"; import { normalizeRow } from "../src/server/util"; @@ -112,22 +110,17 @@ async function loadOutfitData(id) { let cachedIndexPageHtml = null; async function loadIndexPageHtml() { if (cachedIndexPageHtml == null) { - if (process.env.NODE_ENV === "development") { - // In development, request a built version of index.html from the dev - // server, by visiting `/`. - const htmlFromDevServer = await fetch( - "http://localhost:3000/" - ).then((res) => res.text()); - cachedIndexPageHtml = htmlFromDevServer; - } else { - // In production, read the build version of index.html from the local - // `build` directory. - const htmlFromFile = await fs.readFile( - path.join(__dirname, "../build/index.html"), - "utf8" - ); - cachedIndexPageHtml = htmlFromFile; - } + // Request the same built copy of index.html that we're already serving at + // our homepage. + const homepageUrl = process.env.VERCEL_URL + ? `https://${process.env.VERCEL_URL}/` + : process.env.NODE_ENV === "development" + ? "http://localhost:3000/" + : "https://impress-2020.openneo.net/"; + const liveIndexPageHtml = await fetch(homepageUrl).then((res) => + res.text() + ); + cachedIndexPageHtml = liveIndexPageHtml; } return cachedIndexPageHtml;