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

fix: home page hmr doesn't work #79

Merged
merged 2 commits into from
Oct 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/theme-default/components/HomeFeatures/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styles from './index.module.scss';
import { usePageData } from 'island/client';
import { useFrontmatter } from '../../logic';

const getGridClass = (count?: number) => {
if (!count) {
Expand All @@ -16,9 +16,10 @@ const getGridClass = (count?: number) => {
};

export function HomeFeature() {
const { frontmatter } = usePageData();
const frontmatter = useFrontmatter();
const features = frontmatter?.features;
const gridClass = getGridClass(features?.length);

return (
<div className="max-w-1152px" m="auto" flex="~ wrap" justify="between">
{features?.map((feature) => {
Expand Down
5 changes: 2 additions & 3 deletions src/theme-default/components/HomeHero/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { usePageData } from 'island/client';
import { Button } from '../Button';
import { normalizeHref } from '../../logic/index';
import { normalizeHref, useFrontmatter } from '../../logic/index';
import styles from './index.module.scss';

const DEFAULT_HERO = {
Expand All @@ -12,7 +11,7 @@ const DEFAULT_HERO = {
};

export function HomeHero() {
const { frontmatter } = usePageData();
const frontmatter = useFrontmatter();
const hero = frontmatter?.hero || DEFAULT_HERO;
const hasImage = hero.image !== undefined;

Expand Down
1 change: 1 addition & 0 deletions src/theme-default/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { usePrevNextPage } from './usePrevNextPage';
export { useEditLink } from './useEditLink';
export { useSidebarData } from './useSidebarData';
export { useLocaleSiteData } from './useLocaleSiteData';
export { useFrontmatter } from './useFrontmatter';
export { setupEffects, bindingAsideScroll } from './sideEffects';
export { setupCopyCodeButton } from './copyCode';
export { PageSearcher } from './search';
Expand Down
23 changes: 23 additions & 0 deletions src/theme-default/logic/useFrontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState, useEffect } from 'react';
import { usePageData } from 'island/client';

export const useFrontmatter = () => {
const pageData = usePageData();
const [frontmatter, setFrontmatter] = useState(pageData.frontmatter);

// #65
// The frontmatter data changes require us to manually trigger the react rerender
useEffect(() => {
if (import.meta.env.DEV) {
import.meta.hot?.on('md(x)-changed', ({ filePath }) => {
import(/* @vite-ignore */ `${filePath}?import&t=${Date.now()}`).then(
(mod) => {
setFrontmatter(mod.frontmatter);
}
);
});
}
}, []);

return frontmatter;
};