From 60d896cd8c5e0c43bedf99f264f453aaef73c115 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Wed, 20 Feb 2019 20:33:34 +0200 Subject: [PATCH] feat: add route prefetching --- front/src/features/hello/Hello.tsx | 4 +++- front/src/features/landing/Landing.tsx | 17 +++++++++++------ front/src/features/routing/index.ts | 1 + front/src/features/routing/prefetchRoute.ts | 4 ++++ .../src/features/routing/useRoutePrefetching.ts | 9 +++++++++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 front/src/features/routing/prefetchRoute.ts create mode 100644 front/src/features/routing/useRoutePrefetching.ts diff --git a/front/src/features/hello/Hello.tsx b/front/src/features/hello/Hello.tsx index 9c2306de..89d4a883 100644 --- a/front/src/features/hello/Hello.tsx +++ b/front/src/features/hello/Hello.tsx @@ -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 (

diff --git a/front/src/features/landing/Landing.tsx b/front/src/features/landing/Landing.tsx index 595ef993..b8572cdc 100644 --- a/front/src/features/landing/Landing.tsx +++ b/front/src/features/landing/Landing.tsx @@ -1,9 +1,14 @@ +import { useRoutePrefetching } from '../routing' import { SignIn } from './features/sign-in' import { SignUp } from './features/sign-up' -export const Landing = () => ( - <> - - - -) +export const Landing = () => { + useRoutePrefetching(['/hello', '/app']) + + return ( + <> + + + + ) +} diff --git a/front/src/features/routing/index.ts b/front/src/features/routing/index.ts index e1ede5f5..ebc2026d 100644 --- a/front/src/features/routing/index.ts +++ b/front/src/features/routing/index.ts @@ -1,2 +1,3 @@ export { Link } from './Link' export { pushRoute } from './pushRoute' +export { useRoutePrefetching } from './useRoutePrefetching' diff --git a/front/src/features/routing/prefetchRoute.ts b/front/src/features/routing/prefetchRoute.ts new file mode 100644 index 00000000..1e970104 --- /dev/null +++ b/front/src/features/routing/prefetchRoute.ts @@ -0,0 +1,4 @@ +import NextRoutes from '../../../routes' + +export const prefetchRoute = async (route: string): Promise => + NextRoutes.Router.prefetchRoute(route) diff --git a/front/src/features/routing/useRoutePrefetching.ts b/front/src/features/routing/useRoutePrefetching.ts new file mode 100644 index 00000000..5eccc394 --- /dev/null +++ b/front/src/features/routing/useRoutePrefetching.ts @@ -0,0 +1,9 @@ +import { useEffect } from 'react' + +import { prefetchRoute } from './prefetchRoute' + +export const useRoutePrefetching = (paths: string[]) => { + useEffect(() => { + paths.forEach(prefetchRoute) + }, []) +}