# route-resource-preload

> Focus on improving the first screen loading speed of applications and providing the best user experience.

Latest version **1.0.0-beta.11** (published 2023-03-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install route-resource-preload
pnpm add route-resource-preload
yarn add route-resource-preload
bun add route-resource-preload
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.11 |
| Published | 2023-03-16 |
| First published | 2023-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 39 |
| Author | AwesomeDevin |
| Maintainers | awesomedevin |
| Keywords | prefetch, preload, route-prefetch, router-prefetch, route-preload, router-preload, js-preload, resource-preload, module-preload, module-federation-preload, router-resource-preload, router-resource-prefetch, route-resource-preload, route-resource-prefetch, react-preload, react-prefetch |

## Links

- npm: https://www.npmjs.com/package/route-resource-preload
- Repository: https://github.com/AwesomeDevin/route-resource-preload
- Homepage: https://github.com/AwesomeDevin/route-resource-preload#readme
- Issues: https://github.com/AwesomeDevin/route-resource-preload/issues
- npm.io page: https://npm.io/package/route-resource-preload

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.0-beta.11 (latest) — 2023-03-16
- 1.0.0-beta.10 — 2023-03-16
- 1.0.0-beta.9 — 2023-03-16
- 1.0.0-beta.8 — 2023-03-16
- 1.0.0-beta.7 — 2023-03-16
- 1.0.0-beta.6 — 2023-03-16
- 1.0.0-beta.4 — 2023-03-14
- 1.0.0-beta.3 — 2023-03-14
- 1.0.0-beta.2 — 2023-03-14
- 1.0.0-beta.1 — 2023-03-13

## README

# route-resource-preload 
[![Build Size](https://img.shields.io/bundlephobia/minzip/route-resource-preload?label=bundle%20size)](https://bundlephobia.com/result?p=route-resource-preload)
[![Version](https://img.shields.io/npm/v/route-resource-preload?style=flat)](https://www.npmjs.com/package/route-resource-preload)

🚀 Focus on improving the first screen loading speed of applications and providing the best user experience. 

## Why do you need route-resource-preload ?
- Split modules, improving the first screen loading experience of your App. 
- Minimize dynamic component loading time and providing the best user experience.
- Support manually to preloading.
- Support automatic the preloading of resources ( JS / Component / Module-Federation / Svg / Png , Etc) by route and providing the best user experience.
- Support typescript.

## [DEMO](https://route-resource-preload.netlify.app/)

## Install
```shell
npm install @route-resource-preload/webpack-plugin @route-resource-preload/react
```

## Using in react
#### Method 1 - Manual Preloading
```js
import { dynamic } from '@route-resource-preload/react'

const Image = dynamic({
  loader: () => import('Components'),
  loading: (props) => <>loading...</>,
})

const handleClick = () => {
  // Manual Preloading
  Image.preload()
}

export default function Main(props){
  return <div onClick={handleClick}>
    <Image {...props} />
  </div>
}
```

#### Method 2 - Automatic preloading by route.
Step 1: First, you need add **plugin** in your build config.
```js
const RouteResourcePreloadPlugin = require('@route-resource-preload/webpack-plugin')

webpack: {
  plugins: {
    add: [
      new RouteResourcePreloadPlugin({
        // project's components(modules), the key is route path
        modulePreloadMap: {
          "/A": ["../components/A"]
        },
        
        // module-federation's components(modules), the key is route path
        mfPreloadMap: {
          "/MF": ["ling_core/Components"]
        },
        
        // static assets (just like js/css/png/jpg/font, etc.), the key is route path
        assetsPreloadMap: {
          "/A": ['https://domain.com/xxx.png']
        }
      })
    ]
  },
}
```

Step 2: **Dynamic** import component and render **PreloadLink** 
```js
import { dynamic, PreloadLink } from '@route-resource-preload/react'

// project's component
const ComponentA = dynamic({
  loader: ()=>import('../components/A'),
  loading: () => <>loading...</>
})

// module-federation's component
const Image = dynamic({
  loader: ()=>import('your_lib/Components'),
  loading: () => <>loading...</>,
  submodule: 'Image' // may be you didn't export default, just like " export { Image, ...Others } " in js.
})

export default function Main(props){
  return <>
    <PreloadLink to="/A"  onClick={()=>{navigate('/A')}} className="App-link">
      Preload Component A
    </PreloadLink>
    <PreloadLink to="/MF" className="App-link">
      <Link to="/MF" >Preload MF</Link>
    </PreloadLink>
  </>
}
```

## API

#### dynamic
Param | Description | Type | Default Value | necessary
---- | ---- | ---- | ---- | ---
loader | dynamic import module | () => Promise<FunctionComponent<any> / Record<string, FunctionComponent<any>>> | - | ✅
loading | A spinner for displaying loading state | FunctionComponent<any> | - | 
submodule | maybe you didn't export default, you need it | string | - | ❎

#### PreloadLink
> PreloadLink's basename param is the same as RouteResourcePreloadPlugin's basename param

Param | Description | Type | Default Value | necessary
---- | ---- | ---- | ---- | ---
to | route path to preload | string | - | ✅
children | children ReactNode | ReactNode | - | ✅
basename | router basename | string | - | ❎
action | trigger preload action | <a href="#init--inview">string (init / inview)</a> | hover | ❎
onClick | PreloadLink click event | () => void | - | ❎
className | PreloadLink classname | string | - | ❎


## Plugin

#### RouteResourcePreloadPlugin
> RouteResourcePreloadPlugin's basename param is the same as PreloadLink's basename param

Param | Description | Type | Default Value | necessary
---- | ---- | ---- | ---- | ---
modulePreloadMap | project's components(modules) | <a href="#modulepreloadmap-object">modulePreloadMap Object</a> | - | ❎
mfPreloadMap | module-federation's components(modules) | <a href="#mfpreloadmap-object">mfPreloadMap Object</a> | - | ❎
assetsPreloadMap | static assets | <a href="#assetspreloadmap-object">assetsPreloadMap Object</a> | - | ❎
basename | router basename | string | - | ❎


## Others

#### init / inview
Value | Description
--- | ---
init | Trigger preload after PreloadLink rendering 
inview | Trigger preload after PreloadLink in the view


#### modulePreloadMap Object
```js
{
  "/A": ["../components/A"],
  // [route-path]: ['your components path']
}
```

#### mfPreloadMap Object
```js
{
  "/MF": ["ling_core/Components"]
  // [route-path]: ['your components path']
}
```

#### assetsPreloadMap Object
```js
{
  "/A": ['https://domain.com/xxx.png']
  // [route-path]: ['your assets link']
}
```

---
_Source: https://npm.io/package/route-resource-preload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
