Experience-Monks / nyg-jam3

Second generation of the Jam3 Generator, many new features and breaking change features
https://generator.jam3.net
MIT License
11 stars 4 forks source link

preload section js chunk on link hover #237

Closed namniak closed 5 years ago

namniak commented 5 years ago

What kind of change does this PR introduce? (check at least one)

Does this PR introduce a breaking change? (check one)

Did you test your solution?

Problem Description

Missing html resource hints ISSUE https://github.com/Jam3/nyg-jam3/issues/234

Solution Description

Created PrefetchLink component that is able to download resource on link hover. Note that in case of page links it will download a js chunk based on route string matching it with routes/keys.

Side Effects, Risks, Impact

Aditional comments: IMPORTANT

  1. some browsers don't support prefetch (e.g Safari) see https://caniuse.com/#feat=link-rel-prefetch
  2. there's no check for resource type whether it's downloadable or not. checking against extension would be hard to implement considering variety of supported resources. see https://caniuse.com/#feat=link-rel-prefetch
  3. links with download attribute will ignore prefetch
  4. there is a piece of code that needs to be maintained manually - we need to name pages chunks appropriately and match with route keys. See example:

pages/Pages.js

const Landing = lazy(() => import(/* webpackChunkName: "Landing" */ '../../pages/Landing/Landing'));
...

routes/keys.js

export default {
  Landing: '/',
 ...
};

above webpackChunkName matches appropriate page key from routes