Skip to content

vite: Add inlineCssInDev option to fix FOUC in SSR dev #1614

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions .changeset/cyan-bikes-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@vanilla-extract/compiler': minor
---

Add new `getAllCss` API

The `Compiler` class now provides a `getAllCss` method that returns all the CSS currently stored by the compiler.

**EXAMPLE USAGE**:

```ts
import { createCompiler } from '@vanilla-extract/compiler';

const compiler = createCompiler({
root: process.cwd(),
});

await compiler.processVanillaFile('foo.css.ts');
await compiler.processVanillaFile('bar.css.ts');

// Contains all CSS created by `foo.css.ts` and `bar.css.ts`
const allCss = compiler.getAllCss();
```
26 changes: 26 additions & 0 deletions .changeset/shiny-emus-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@vanilla-extract/vite-plugin': minor
---

Add new `inlineCssInDev` option to `unstable_mode` configuration property

Setting `unstableMode: 'inlineCssInDev'` will result in all CSS generated by Vanilla Extract being inlined into a `style` element at the top of the document `head`. This feature is useful for preventing [FOUC] (Flash of Unstyled Content) when server-rendering your initial HTML. Without this, calling `ssrLoadModule` on your server entrypoint will not include the CSS in the HTML, leading to a FOUC when the client-side JavaScript takes over.

Note that CSS will only be inlined in development mode. Production builds are unaffected by this setting.

**EXAMPLE USAGE**:

```ts
// vite.config.ts
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';

export default {
plugins: [
vanillaExtractPlugin({
unstable_mode: 'inlineCssInDev',
}),
],
}
```

[FOUC]: https://en.wikipedia.org/wiki/Flash_of_unstyled_content
12 changes: 12 additions & 0 deletions packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export interface Compiler {
): Promise<{ source: string; watchFiles: Set<string> }>;
getCssForFile(virtualCssFilePath: string): { filePath: string; css: string };
close(): Promise<void>;
getAllCss(): string;
}

interface ProcessedVanillaFile {
Expand Down Expand Up @@ -499,5 +500,16 @@ export const createCompiler = ({

await server.close();
},
getAllCss() {
let allCss = '';

for (const { css } of cssCache.values()) {
if (css) {
allCss += css + '\n';
}
}

return allCss;
},
};
};
Loading