An open API service indexing awesome lists of open source software.

https://github.com/bytekai/preload-next

A Preload Link Component for Next.js to allow for preloading of data before navigation on hover or when link is in view.
https://github.com/bytekai/preload-next

link nextjs preload

Last synced: about 1 month ago
JSON representation

A Preload Link Component for Next.js to allow for preloading of data before navigation on hover or when link is in view.

Awesome Lists containing this project

README

          

# Preload Link NextJS
## Or rather prefetching, the terms confuse me.

A variant of the Link component in NextJS that will execute a preload function that is attached to the page to allow for preloading data on hover. Providing the illusion of a faster page load as the data is loaded when the user hovers over the link.

## Demo

https://preload-next-example.vercel.app

## Installation

```bash
yarn install preload-next
```
or
```bash
npm install preload-next
```

## Usage

pages/index.tsx
```jsx
import { LinkPreload } from "preload-next";

export const Header = () => {
return (


Header




Pokemon



);
};
const Home = () => {
return (


Hello



);
};
```

pages/pokemon.tsx
```jsx

import dynamic from "next/dynamic";
import { NextPageContext } from "next/types";
import { useQuery } from "react-query";
import { PreloadContext } from "./_app";

type Pokemon = {
name: string;
};

const getPokemons = async (): Promise => {
return new Promise((resolve) => {
setTimeout(async () => {
const response = await fetch("https://pokeapi.co/api/v2/pokemon");
resolve((await response.json()).results);
}, 500);
});
};

const PokemonPage = () => {
const { data: pokemons, isLoading } = useQuery("pokemon", getPokemons);

return (


Pokemon


    {pokemons &&
    pokemons.map((pokemon: any) => (
  • {pokemon.name}

  • ))}

    {isLoading &&

  • Loading...
  • }


);
};

const preload = async ({ queryClient }: PreloadContext) => {
if (!queryClient.getQueryCache().find("pokemon")) {
queryClient.prefetchQuery("pokemon", getPokemons);
}
};

const getInitialProps = async (ctx: NextPageContext) => {
if (typeof window === "undefined") {
const { QueryClient, dehydrate } = await import("react-query");
const queryClient = new QueryClient();
await queryClient.prefetchQuery("pokemon", getPokemons);

return {
dehydratedState: dehydrate(queryClient),
};
}

return {};
};

const PPage = dynamic(() => Promise.resolve(PokemonPage), {
ssr: false,
}) as any;

PPage.preload = preload;
PPage.getInitialProps = getInitialProps;

export default PPage;
```