-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade meetup demo usage of NextJS latest pattern (#3229)
Ref: https://github.com/vercel/next.js/blob/canary/examples/with-apollo The summary of the changes are: - Less boilerplate code for setting/initializing Apollo Client - Update home, events and about page to opt in for SSG - Removed MyApp.getIntialProps as it will stop the Auto Static optimization. Ref: (https://nextjs.org/docs/api-reference/data-fetching/getInitialProps#caveats) - Handle the auth loading client side before checking of authentication Co-authored-by: Aman Singh <aman.singh@thinkmill.com.au>
- Loading branch information
1 parent
e114894
commit caf3021
Showing
7 changed files
with
111 additions
and
37 deletions.
There are no files selected for viewing
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,11 @@ | ||
--- | ||
'@keystonejs/demo-project-meetup': patch | ||
--- | ||
|
||
Updated usage of Apollo based on Next.js [example](https://github.com/vercel/next.js/blob/canary/examples/with-apollo). | ||
|
||
The **key changes** are: | ||
- Less boilerplate code for setting/initializing Apollo Client | ||
- Update home, events and about page to opt in for SSG | ||
- Removed MyApp.getIntialProps as it will stop the [Auto Static optimization](https://nextjs.org/docs/api-reference/data-fetching/getInitialProps#caveats). | ||
- Handle the authentication in client-side only. |
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
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,42 @@ | ||
// Derived from https://github.com/vercel/next.js/blob/canary/examples/with-apollo/lib/apolloClient.js | ||
import { useMemo } from 'react'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { createUploadLink } from 'apollo-upload-client'; | ||
|
||
let apolloClient; | ||
|
||
function createApolloClient(req) { | ||
return new ApolloClient({ | ||
ssrMode: typeof window === 'undefined', | ||
link: createUploadLink({ | ||
// TODO: server-side requests must have an absolute URI. We should find a way | ||
// to make this part of the project config, seems highly opinionated here | ||
uri: 'http://localhost:3000/admin/api', | ||
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` | ||
headers: req && req.headers, | ||
}), | ||
cache: new InMemoryCache(), | ||
}); | ||
} | ||
|
||
export function initializeApollo(initialState = null, req) { | ||
const _apolloClient = apolloClient ?? createApolloClient(req); | ||
|
||
// If your page has Next.js data fetching methods that use Apollo Client, the initial state | ||
// gets hydrated here | ||
if (initialState) { | ||
_apolloClient.cache.restore(initialState); | ||
} | ||
// For SSG and SSR always create a new Apollo Client | ||
if (typeof window === 'undefined') return _apolloClient; | ||
// Create the Apollo Client once in the client | ||
if (!apolloClient) apolloClient = _apolloClient; | ||
|
||
return _apolloClient; | ||
} | ||
|
||
export function useApollo(initialState) { | ||
const store = useMemo(() => initializeApollo(initialState), [initialState]); | ||
return store; | ||
} |
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
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
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
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