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 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"
// 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 = htmlFromFile;
}
cachedIndexPageHtml = liveIndexPageHtml;
}
return cachedIndexPageHtml;