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.
This commit is contained in:
Emi Matchu 2021-05-14 20:27:45 -07:00
parent 08347ad88f
commit 5e657e7547

View file

@ -23,8 +23,6 @@ const beeline = require("honeycomb-beeline")({
import escapeHtml from "escape-html"; import escapeHtml from "escape-html";
import fetch from "node-fetch"; import fetch from "node-fetch";
import { promises as fs } from "fs";
import * as path from "path";
import connectToDb from "../src/server/db"; import connectToDb from "../src/server/db";
import { normalizeRow } from "../src/server/util"; import { normalizeRow } from "../src/server/util";
@ -112,22 +110,17 @@ async function loadOutfitData(id) {
let cachedIndexPageHtml = null; let cachedIndexPageHtml = null;
async function loadIndexPageHtml() { async function loadIndexPageHtml() {
if (cachedIndexPageHtml == null) { if (cachedIndexPageHtml == null) {
if (process.env.NODE_ENV === "development") { // Request the same built copy of index.html that we're already serving at
// In development, request a built version of index.html from the dev // our homepage.
// server, by visiting `/`. const homepageUrl = process.env.VERCEL_URL
const htmlFromDevServer = await fetch( ? `https://${process.env.VERCEL_URL}/`
"http://localhost:3000/" : process.env.NODE_ENV === "development"
).then((res) => res.text()); ? "http://localhost:3000/"
cachedIndexPageHtml = htmlFromDevServer; : "https://impress-2020.openneo.net/";
} else { const liveIndexPageHtml = await fetch(homepageUrl).then((res) =>
// In production, read the build version of index.html from the local res.text()
// `build` directory. );
const htmlFromFile = await fs.readFile( cachedIndexPageHtml = liveIndexPageHtml;
path.join(__dirname, "../build/index.html"),
"utf8"
);
cachedIndexPageHtml = htmlFromFile;
}
} }
return cachedIndexPageHtml; return cachedIndexPageHtml;