seo improvements
This commit is contained in:
@@ -30,7 +30,24 @@ if (!isProduction) {
|
||||
const compression = (await import("compression")).default;
|
||||
const sirv = (await import("sirv")).default;
|
||||
app.use(compression());
|
||||
app.use(base, sirv(clientDir, { extensions: [] }));
|
||||
app.use(
|
||||
base,
|
||||
sirv(clientDir, {
|
||||
extensions: [],
|
||||
setHeaders(res, pathname) {
|
||||
if (pathname.startsWith("/assets/")) {
|
||||
// vite hashes these filenames, so the content can never change
|
||||
// under a given url
|
||||
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
|
||||
} else if (pathname.startsWith("/fonts/")) {
|
||||
// stable but unhashed, so cache for a while rather than forever
|
||||
res.setHeader("Cache-Control", "public, max-age=2592000");
|
||||
} else {
|
||||
res.setHeader("Cache-Control", "public, max-age=3600");
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
// a hashed asset that sirv did not find is genuinely gone, usually a stale
|
||||
// cached document asking for a previous build. 404 it here, otherwise it
|
||||
// falls through to the catch-all and answers a .js request with html
|
||||
@@ -43,26 +60,74 @@ if (!isProduction) {
|
||||
});
|
||||
}
|
||||
|
||||
/** load the SSR bundle, from source in dev and from the build in production */
|
||||
async function loadServerEntry() {
|
||||
if (!isProduction) {
|
||||
return await vite.ssrLoadModule("/src/entry-server.tsx");
|
||||
}
|
||||
return await import("./dist/server/entry-server.js");
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the deploy base from a request url, always leaving a rooted path.
|
||||
* The previous `originalUrl.replace(base, "")` dropped the leading slash when
|
||||
* base was "/", which would hand the router a relative location.
|
||||
*/
|
||||
function stripBase(originalUrl) {
|
||||
if (!originalUrl) return "/";
|
||||
if (base === "/") return originalUrl;
|
||||
if (originalUrl.startsWith(base)) {
|
||||
const rest = originalUrl.slice(base.length);
|
||||
return rest.startsWith("/") ? rest : `/${rest}`;
|
||||
}
|
||||
return originalUrl;
|
||||
}
|
||||
|
||||
// generated from the route table rather than checked in, so it cannot go stale
|
||||
app.get(`${base.replace(/\/$/, "")}/sitemap.xml`, async (req, res) => {
|
||||
try {
|
||||
const { renderSitemap } = await loadServerEntry();
|
||||
res
|
||||
.status(200)
|
||||
.set({
|
||||
"Content-Type": "application/xml; charset=utf-8",
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
})
|
||||
.send(renderSitemap());
|
||||
} catch (e) {
|
||||
vite?.ssrFixStacktrace(e);
|
||||
console.error(e.stack);
|
||||
res.status(500).end();
|
||||
}
|
||||
});
|
||||
|
||||
app.use("*all", async (req, res) => {
|
||||
try {
|
||||
const url = req.originalUrl.replace(base, "");
|
||||
const url = stripBase(req.originalUrl);
|
||||
|
||||
let template;
|
||||
let render;
|
||||
if (!isProduction) {
|
||||
template = await fs.readFile(path.resolve(root, "index.html"), "utf-8");
|
||||
template = await vite.transformIndexHtml(url, template);
|
||||
render = (await vite.ssrLoadModule("/src/entry-server.tsx")).render;
|
||||
} else {
|
||||
template = templateHtml;
|
||||
render = (await import("./dist/server/entry-server.js")).render;
|
||||
}
|
||||
|
||||
const { render } = await loadServerEntry();
|
||||
const rendered = await render(url);
|
||||
const html = template
|
||||
?.replace("<!--app-head-->", rendered.head ?? "")
|
||||
.replace("<!--app-html-->", rendered.html ?? "");
|
||||
res.status(200).set({ "Content-Type": "text/html" }).send(html);
|
||||
|
||||
res
|
||||
.status(rendered.statusCode ?? 200)
|
||||
.set({
|
||||
"Content-Type": "text/html",
|
||||
// short shared cache window; the page embeds live-ish data
|
||||
"Cache-Control":
|
||||
"public, max-age=0, s-maxage=60, stale-while-revalidate=300",
|
||||
})
|
||||
.send(html);
|
||||
} catch (e) {
|
||||
vite?.ssrFixStacktrace(e);
|
||||
console.log(e.stack);
|
||||
|
||||
Reference in New Issue
Block a user