From fca859ad3b71ce5aac30dfb547a13ab75d01cfce Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 9 May 2024 18:00:13 -0400 Subject: [PATCH] Update remix-serve static serving for prerendering --- .changeset/tough-pens-brush.md | 10 ++++++++++ packages/remix-serve/cli.ts | 17 ++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .changeset/tough-pens-brush.md diff --git a/.changeset/tough-pens-brush.md b/.changeset/tough-pens-brush.md new file mode 100644 index 0000000000..2927281795 --- /dev/null +++ b/.changeset/tough-pens-brush.md @@ -0,0 +1,10 @@ +--- +"@react-router/serve": patch +--- + +Update `express.static` configurations to support prerendering + +- Assets in the `build/client/assets` folder are served as before, with a 1-year immutable `Cache-Control` header +- Static files outside of assets, such as pre-rendered `.html` and `.data` files are not served with a specific `Cache-Control` header +- `.data` files are served with `Content-Type: text/x-turbo` + - For some reason, when adding this via `express.static`, it seems to also add a `Cache-Control: public, max-age=0` to `.data` files diff --git a/packages/remix-serve/cli.ts b/packages/remix-serve/cli.ts index 5532ef96cf..8a1456f55d 100644 --- a/packages/remix-serve/cli.ts +++ b/packages/remix-serve/cli.ts @@ -76,18 +76,21 @@ async function run() { let app = express(); app.disable("x-powered-by"); app.use(compression()); + app.use( + path.posix.join(build.publicPath, "assets"), + express.static(path.join(build.assetsBuildDirectory, "assets"), { + immutable: true, + maxAge: "1y", + }) + ); app.use( build.publicPath, express.static(build.assetsBuildDirectory, { - setHeaders: function (res, path, stat) { + // Don't redirect directory index.html request to include a trailing slash + redirect: false, + setHeaders: function (res, path) { if (path.endsWith(".data")) { res.set("Content-Type", "text/x-turbo"); - } else { - // Cache as an immutable asset for 1 year - // Do this here instead of via the immutable/maxAge headers so we can - // conditionally apply it to assets (which are hashed), and not - // pre-rendered .data files (not hashed) - res.set("Cache-Control", "public, max-age=31536000, immutable"); } }, })