-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring gatsby to use client only
- Loading branch information
1 parent
e5883e7
commit 0b6dcba
Showing
4 changed files
with
121 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { Suspense } from "react" | ||
|
||
import "./src/styles/fonts.css" | ||
import "bootstrap/dist/css/bootstrap.min.css" | ||
import "./src/styles/style.css" | ||
|
||
// import { useBrowserStore } from "./src/store" | ||
import App from "./src/components/App" | ||
import PageLayout from "./src/components/PageLayout" | ||
|
||
const AppLazy = React.lazy(() => import("./src/components/App")) | ||
const PageLayoutLazy = React.lazy(() => import("./src/components/PageLayout")) | ||
// Logs when the client route changes | ||
export function onRouteUpdate({ location, prevLocation }) { | ||
console.log("[Layout] render") | ||
console.log( | ||
"[gatsby-browser]@onRouteUpdate new pathname", | ||
location.pathname, | ||
"prev pathname", | ||
prevLocation?.pathname, | ||
) | ||
// useBrowserStore.getState().setPath(location.pathname, prevLocation?.pathname) | ||
} | ||
|
||
// Wraps every page in a component | ||
export function wrapRootElement({ element, props }) { | ||
return ( | ||
<Suspense> | ||
<AppLazy {...props}>{element}</AppLazy> | ||
</Suspense> | ||
) | ||
} | ||
|
||
// wraps every page in a component | ||
export function wrapPageElement({ element, props }) { | ||
console.log("[gatsby-browser]@wrapPageElement", props) | ||
|
||
// display all other pages a s modals | ||
return ( | ||
<main className="position-fixed"> | ||
<Suspense> | ||
<PageLayoutLazy {...props}>{element}</PageLayoutLazy> | ||
</Suspense> | ||
</main> | ||
) | ||
} | ||
|
||
export function shouldUpdateScroll() { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useEffect, useState } from "react" | ||
import { usePersistentStore } from "../store" | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query" | ||
import Layout from "../components/Layout" | ||
import PrefetchData from "../components/PrefetchData" | ||
import Modals from "../components/Modals" | ||
|
||
console.info( | ||
"Impresso datalab", | ||
"\n - PATH_PREFIX:", | ||
process.env.PATH_PREFIX, | ||
"\n - GATSBY_PATH_PREFIX:", | ||
process.env.GATSBY_PATH_PREFIX, | ||
) | ||
|
||
// setTimeout(() => { | ||
// console.info("[gatsby-browser] load version...") | ||
// versionService.find().then((data) => { | ||
// console.info("[gatsby-browser] use version:", data) | ||
// }) | ||
// }, 3000) | ||
|
||
const App: React.FC = ({ props, children }) => { | ||
const [queryClient] = useState( | ||
() => | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
refetchOnWindowFocus: false, | ||
retry: false, | ||
// Add a timeout of 10 seconds | ||
staleTime: 10000, | ||
}, | ||
}, | ||
}), | ||
) | ||
console.log("[App] rendering...", window) | ||
useEffect(() => { | ||
// get fresh data from the localstorage | ||
const existingToken = window.localStorage.getItem("feathers-jwt") | ||
|
||
if (existingToken && usePersistentStore.getState().token === null) { | ||
console.info("[usePersistentStore] use existing token from feathers-jwt") | ||
usePersistentStore.setState({ token: existingToken }) | ||
} else { | ||
console.info("[usePersistentStore] use fresh token") | ||
} | ||
}, []) | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<PrefetchData /> | ||
<Modals /> | ||
<Layout {...props}>{children}</Layout> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters