Skip to content

Commit

Permalink
fix: home page hmr doesn't work (#79)
Browse files Browse the repository at this point in the history
* fix: home page hmr doesn't work

* fix: the return value format of useFrontmatter
  • Loading branch information
callqh committed Oct 10, 2022
1 parent bfaaffe commit 1358b13
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
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;
};

1 comment on commit 1358b13

@vercel
Copy link

@vercel vercel bot commented on 1358b13 Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.