Skip to content

Commit

Permalink
Add support for incremental static regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
hswolff committed Aug 12, 2020
1 parent d164f6a commit 62c46ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 19 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import ItemList from 'components/ItemList';
import { useItems } from 'services/api-hooks';
import { Item } from 'services/data-types';

export default function Home() {
const { data, isSuccess } = useItems();
interface HomeProps {
items: Item[] | undefined;
}

export default function Home({ items }: HomeProps) {
const { data, isSuccess } = useItems({ initialData: items });

return <div>{isSuccess && data && <ItemList items={data} />}</div>;
}

export async function getStaticProps() {
const res = await fetch(`${process.env.SITE}/api/items`);
const items = await res.json();

return {
props: {
items,
},
revalidate: 30,
};
}
6 changes: 3 additions & 3 deletions services/api-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useQuery, useMutation, queryCache } from 'react-query';
import { useQuery, useMutation, queryCache, QueryOptions } from 'react-query';
import { Item } from 'services/data-types';

const defaultQueryFn = (requestPath: string) =>
fetch(requestPath).then((res) => res.json());

// queries

export function useItems() {
return useQuery<Item[], string, string>('/api/items', defaultQueryFn);
export function useItems<Result = Item[]>(options?: QueryOptions<Result>) {
return useQuery<Result, string>('/api/items', defaultQueryFn, options);
}

export function useMeData() {
Expand Down

1 comment on commit 62c46ed

@vercel
Copy link

@vercel vercel bot commented on 62c46ed Aug 12, 2020

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.