-
Notifications
You must be signed in to change notification settings - Fork 319
how to clean browser css cache #11
Comments
I ended up doing the following in
import Document, { Head, Main, NextScript } from 'next/document';
import { readFileSync } from 'fs';
let version = '';
if (process.env.NODE_ENV === 'production') {
version = `?v=${readFileSync(`${process.cwd()}/src/.next/BUILD_ID`)}`;
}
export default class extends Document {
render() {
return (
<html>
<Head>
<link rel="stylesheet" href={`/_next/static/style.css${version}`} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
} |
It's also possible to use file's hash instead of import { createHash } from "crypto";
import { readFileSync } from "fs";
// ...
let version = "";
if (process.env.NODE_ENV === "production") {
const hash = createHash("sha256");
hash.update(readFileSync(`${process.cwd()}/.next/static/style.css`));
version = `?v=${hash.digest("hex").substr(0, 8)}`;
}
// ... If the styles don't change between the builds, there's no need to download them and so everyone is better off. In general, it's more efficient to calculate file cache using streams, but this should be unnecessary in our case. if
What are the caveats of this trick? If anyone sees them, could you please share? |
Even though the workarounds in two previous comments solve the issue with It'd be truly great to bring some webpack magic here! This would not only allow correct caching by turning paths into something like |
Here's one more discovery that might help those who have little CSS in let style = null;
if (process.env.NODE_ENV === "production") {
style = fs.readFileSync(`${process.cwd()}/.next/static/style.css`, "utf8");
}
// inside _document's Head:
{typeof style === "string" ? (
<style dangerouslySetInnerHTML={{ __html: style }} />
) : (
<link rel="stylesheet" href="/_next/static/style.css" />
)} This helped me change the lighthouse performance score in production from 89 to 96 because First meaningful paint time decreased from 2.06 to 1.10 seconds. This is an advice in the original report that I followed: When a stylesheet is heavy, it's cheaper to load it as a separate file and cache that request in the browser – this keeps the size of the server-rendered HTMLs small while avoiding extra round trips in consequent page requests. However, when Don't forget to import styles in a higher order component that works as a page wrapper: # e.g. in hocs/page.js
import "./path/to/your/root/stylesheet.css" Can this issue be considered as resolved? If you want to check your website's performance, simply run npm install --global lighthouse
lighthouse http://example.com/ --view if let style = null;
if (process.env.NODE_ENV === "production") {
// ${"css"} prevents editors from incorrectly highlighting code after css`
style = readFileSync(`${process.cwd()}/.next/static/style.${"css"}`, "utf8");
} |
If you're going with the inlining approach, you may need to use:
Otherwise, things like quotes get converted to HTML entities. |
Thank you @bryandowning, you are right! I updated the comment and removed |
Will be possible when vercel/next.js#4119 lands for Next 6 |
Hi @timneutkens
In production the stylesheet is compiled to .next/static/style.css. You have to include it into the page using either next/head or a custom _document.js. The file will be served from /_next/static/style.css
usually we will use hash code to clean browser cache if css file has any modification, like style.41d523.css
how can we do this on this plugin?
The text was updated successfully, but these errors were encountered: