Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update remix-serve static serving for prerendering #11547

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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