Giter Club home page Giter Club logo

data-prefetch-link's Introduction

data-prefetch-link

Extends <Link> to allow invoking getInitialProps when prefetching a page. That way, if you use a caching layer in getInitialProps, the page data will be already cached when you click the link, thus producing the same instantaneous navigation that is possible with prefetched static pages.

This approach is explained in more detail in our blog post: Increasing the Performance of Dynamic Next.js Websites.

Installation

npm i --save data-prefetch-link

Usage

This component extends Next.js <Link>, so you can use the same parameters and expect the same behavior. Additionally, you can pass the prop withData, which will make it run getInitialProps when prefetching (this only works when prefetch is also used). So a usage with data prefetching would look like this:

import Link from 'data-prefetch-link'

<Link prefetch withData href="…"><a>Some dynamic page</a></Link>

When getInitialProps is invoked by this component, the context will receive an isVirtualCall flag set to true, which can be used to skip fetching resources that will not be cached, so these calls are not made unnecessarily.

Note

Prefetching data should be used strategically, rather than habitually.

A good use case might be high-level pages in the header navigation, in which there are typically only a handful. Or when you'd expect a link to get clicked on.

You probably wouldn't want to use it on large lists of links (e.g. grid with infinite scroll) as this would negatively affect performance — for both your users and the web server that's handling the requests.

data-prefetch-link's People

Contributors

gmaliandi avatar jabacchetta avatar kennand avatar sergiodxa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.