Skip to content

Commit

Permalink
Merge pull request #11547 from remix-run/brophdawg11/prerender-serve
Browse files Browse the repository at this point in the history
Update remix-serve static serving for prerendering
  • Loading branch information
ryanflorence authored May 9, 2024
2 parents cb25a21 + fca859a commit 13d6d43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 10 additions & 0 deletions .changeset/tough-pens-brush.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 10 additions & 7 deletions packages/remix-serve/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
},
})
Expand Down

0 comments on commit 13d6d43

Please sign in to comment.