Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat: add route prefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Feb 20, 2019
1 parent d3a5d39 commit 60d896c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 3 additions & 1 deletion front/src/features/hello/Hello.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { pushRoute } from '@front/features/routing'
import { pushRoute, useRoutePrefetching } from '@front/features/routing'
import { Button } from '@front/ui/components/form/button'

import * as styles from './Hello.css'

export const Hello = () => {
useRoutePrefetching(['/app'])

return (
<section className={styles.container}>
<h1>
Expand Down
17 changes: 11 additions & 6 deletions front/src/features/landing/Landing.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { useRoutePrefetching } from '../routing'
import { SignIn } from './features/sign-in'
import { SignUp } from './features/sign-up'

export const Landing = () => (
<>
<SignIn />
<SignUp />
</>
)
export const Landing = () => {
useRoutePrefetching(['/hello', '/app'])

return (
<>
<SignIn />
<SignUp />
</>
)
}
1 change: 1 addition & 0 deletions front/src/features/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Link } from './Link'
export { pushRoute } from './pushRoute'
export { useRoutePrefetching } from './useRoutePrefetching'
4 changes: 4 additions & 0 deletions front/src/features/routing/prefetchRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import NextRoutes from '../../../routes'

export const prefetchRoute = async (route: string): Promise<void> =>
NextRoutes.Router.prefetchRoute(route)
9 changes: 9 additions & 0 deletions front/src/features/routing/useRoutePrefetching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect } from 'react'

import { prefetchRoute } from './prefetchRoute'

export const useRoutePrefetching = (paths: string[]) => {
useEffect(() => {
paths.forEach(prefetchRoute)
}, [])
}

0 comments on commit 60d896c

Please sign in to comment.