-
I want to cache static assets like fonts for at least a year, but the default time set for static assets is 1 hour. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yes, Remix App Server doesn't expose its configuration. You're basically stuck with the defaults and need to eject to your own Express server if you want more flexibility. https://github.com/kiliman/rmx-cli#-eject-ras Another option is to move your static assets to a folder in https://remix.run/docs/en/v1/guides/migrating-react-router-app#asset-imports |
Beta Was this translation helpful? Give feedback.
-
If someone is looking for a workaround, I created a file import { LoaderFunction } from "@remix-run/node";
import fs from "fs";
import mime from "mime-types";
const allowedPaths = [/^\/some-path\/index\.js$/];
/**
* This method allows to serve static files from the server,
* overriding the cache-control header.
*/
export const loader: LoaderFunction = async ({ request }) => {
const url = new URL(request.url);
const filePath = url.pathname.replace("/static", "");
const ttl = String(url.searchParams.get("max-age") || 60 * 60 * 24 * 30);
if (!allowedPaths.some((path) => path.test(filePath))) {
return new Response("Not Found", { status: 404 });
}
try {
const file = await fs.readFileSync(`./public${filePath}`);
const fileType = mime.lookup(filePath);
return new Response(file, {
status: 200,
headers: {
"Content-Type": fileType || "text/plain",
"Content-Length": String(file.length),
"Cache-Control": `public, max-age=${ttl}`,
},
});
} catch (error) {
return new Response("Failed to download file", { status: 500 });
}
}; |
Beta Was this translation helpful? Give feedback.
Yes, Remix App Server doesn't expose its configuration. You're basically stuck with the defaults and need to eject to your own Express server if you want more flexibility. https://github.com/kiliman/rmx-cli#-eject-ras
Another option is to move your static assets to a folder in
app/
, likeapp/assets
. Then you canimport
the asset in your route, and Remix will fingerprint/hash and copy them topublic/build/_assets
. Thebuild
folder sets the cache toimmutable
so it'll be cached forever. If the asset changes, it'll get a new fingerprint/hash.https://remix.run/docs/en/v1/guides/migrating-react-router-app#asset-imports