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

Prevent FOUC in the iframe while loading #339

Merged
merged 1 commit into from
Apr 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/builder-vite/input/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
window.module = undefined;
</script>
<!-- [HEAD HTML SNIPPET HERE] -->
<style>
/*noinspection CssUnusedSymbol*/
#root[hidden],
#docs-root[hidden] {
display: none !important;
}
</style>
</head>
<body>
<!-- [BODY HTML SNIPPET HERE] -->
Expand Down
49 changes: 49 additions & 0 deletions packages/builder-vite/plugins/no-fouc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { Plugin } from 'vite';

/**
* This plugin is a workaround to inject some styles into the `<head>` of the iframe to
* prevent the "no story" text from appearing breifly while the page loads in.
*
* It can be removed, and these styles placed back into the head,
* when https://github.com/vitejs/vite/issues/6737 is closed.
*/
export function noFouc(): Plugin {
return {
name: 'no-fouc',
enforce: 'post',
async transformIndexHtml(html, ctx) {
if (ctx.path !== '/iframe.html') {
return;
}

return insertHeadStyles(html);
},
};
}

function insertHeadStyles(html: string) {
return html.replace(
'</head>',
`
<style>
/* Fix for elements flashing */
body > * {
display: none !important;
}
.sb-show-preparing-story > .sb-preparing-story,
.sb-show-preparing-docs > .sb-preparing-docs,
.sb-show-nopreview > .sb-nopreview,
.sb-show-errordisplay > .sb-errordisplay,
.sb-show-main > #root,
.sb-show-main > #docs-root {
display: block !important;
}
#root[hidden],
#docs-root[hidden] {
display: none !important;
}
</style>
</head>
`.trim()
);
}
2 changes: 2 additions & 0 deletions packages/builder-vite/vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { mockCoreJs } from './mock-core-js';
import { codeGeneratorPlugin } from './code-generator-plugin';
import { injectExportOrderPlugin } from './inject-export-order-plugin';
import { mdxPlugin } from './mdx-plugin';
import { noFouc } from './plugins/no-fouc';
import { sourceLoaderPlugin } from './source-loader-plugin';

import type { UserConfig } from 'vite';
Expand Down Expand Up @@ -58,6 +59,7 @@ export async function pluginConfig(options: ExtendedOptions, _type: PluginConfig
mockCoreJs(),
sourceLoaderPlugin(),
mdxPlugin(),
noFouc(),
injectExportOrderPlugin,
] as Plugin[];
if (framework === 'vue' || framework === 'vue3') {
Expand Down