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

feat: preload styles when lazy routes are preloaded #1257

Merged
merged 2 commits into from
Jan 15, 2024
Merged
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
94 changes: 44 additions & 50 deletions packages/start/shared/lazyRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,54 @@
/// <reference types="vinxi/types/client" />
import { createComponent, lazy, onCleanup } from "solid-js";
import { appendStyles, cleanupStyles, updateStyles } from "vinxi/css";
import { appendStyles, cleanupStyles, updateStyles, preloadStyles } from "vinxi/css";

import { renderAsset } from "../server/renderAsset";

export default function lazyRoute(
component,
clientManifest,
serverManifest,
exported = "default",
) {
return lazy(async () => {
if (import.meta.env.DEV) {
let manifest = import.meta.env.SSR ? serverManifest : clientManifest;
export default function lazyRoute(component, clientManifest, serverManifest, exported = "default") {
return lazy(async () => {
if (import.meta.env.DEV) {
let manifest = import.meta.env.SSR ? serverManifest : clientManifest;

const mod = await manifest.inputs[component.src].import();
if (!mod[exported]) console.error(`Module ${component.src} does not export ${exported}`)
const Component = mod[exported];
let assets = await clientManifest.inputs?.[component.src].assets();
const styles = assets.filter((asset) => asset.tag === "style");
const mod = await manifest.inputs[component.src].import();
if (!mod[exported]) console.error(`Module ${component.src} does not export ${exported}`);
const Component = mod[exported];
let assets = await clientManifest.inputs?.[component.src].assets();
const styles = assets.filter(asset => asset.tag === "style");

if (typeof window !== "undefined" && import.meta.hot) {
import.meta.hot.on("css-update", (data) => {
updateStyles(styles, data);
});
}
if (typeof window !== "undefined" && import.meta.hot) {
import.meta.hot.on("css-update", data => {
updateStyles(styles, data);
});
}

const Comp = (props) => {
if (typeof window !== "undefined") {
appendStyles(styles);
}
const Comp = props => {
if (typeof window !== "undefined") {
appendStyles(styles);
}

onCleanup(() => {
if (typeof window !== "undefined") {
// remove style tags added by vite when a CSS file is imported
cleanupStyles(styles);
}
});
return [
...assets.map((asset) => renderAsset(asset)),
createComponent(Component, props),
];
};
return { default: Comp };
} else {
const mod = await component.import();
const Component = mod[exported];
let assets = await clientManifest.inputs?.[component.src].assets();
const styles = assets.filter((asset) => asset.tag === "style" || asset.attrs.rel === "stylesheet");
const Comp = (props) => {
return [
...styles.map((asset) => renderAsset(asset)),
createComponent(Component, props),
];
};
return { default: Comp };
}
});
onCleanup(() => {
if (typeof window !== "undefined") {
// remove style tags added by vite when a CSS file is imported
cleanupStyles(styles);
}
});
return [...assets.map(asset => renderAsset(asset)), createComponent(Component, props)];
};
return { default: Comp };
} else {
const mod = await component.import();
const Component = mod[exported];
let assets = await clientManifest.inputs?.[component.src].assets();
const styles = assets.filter(
asset => asset.tag === "style" || asset.attrs.rel === "stylesheet"
);
if (typeof window !== "undefined") {
preloadStyles(styles);
}
const Comp = props => {
return [...styles.map(asset => renderAsset(asset)), createComponent(Component, props)];
};
return { default: Comp };
}
});
}
Loading