forked from vercel/next.js
-
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.
[Examples] Move with-apollo to SSG (vercel#13742)
Based on vercel#13607 - I created a new PR as I can't push changes to the initial PR. Migrated the Apollo client to use SSG, also removed the link to the live demo because I don't know who the owner is or how to update the deployment. The implementation is pretty simple and will be added to all the other Apollo examples
- Loading branch information
Showing
11 changed files
with
133 additions
and
276 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
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 |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import { useRouter } from 'next/router' | ||
import Link from 'next/link' | ||
import { withRouter } from 'next/router' | ||
|
||
const Header = ({ router: { pathname } }) => ( | ||
<header> | ||
<Link href="/"> | ||
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> | ||
</Link> | ||
<Link href="/client-only"> | ||
<a className={pathname === '/client-only' ? 'is-active' : ''}> | ||
Client-Only | ||
</a> | ||
</Link> | ||
<Link href="/about"> | ||
<a className={pathname === '/about' ? 'is-active' : ''}>About</a> | ||
</Link> | ||
<style jsx>{` | ||
header { | ||
margin-bottom: 25px; | ||
} | ||
a { | ||
font-size: 14px; | ||
margin-right: 15px; | ||
text-decoration: none; | ||
} | ||
.is-active { | ||
text-decoration: underline; | ||
} | ||
`}</style> | ||
</header> | ||
) | ||
export default function Header() { | ||
const { pathname } = useRouter() | ||
|
||
export default withRouter(Header) | ||
return ( | ||
<header> | ||
<Link href="/"> | ||
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> | ||
</Link> | ||
<Link href="/about"> | ||
<a className={pathname === '/about' ? 'is-active' : ''}>About</a> | ||
</Link> | ||
<Link href="/client-only"> | ||
<a className={pathname === '/client-only' ? 'is-active' : ''}> | ||
Client-Only | ||
</a> | ||
</Link> | ||
<style jsx>{` | ||
header { | ||
margin-bottom: 25px; | ||
} | ||
a { | ||
font-size: 14px; | ||
margin-right: 15px; | ||
text-decoration: none; | ||
} | ||
.is-active { | ||
text-decoration: underline; | ||
} | ||
`}</style> | ||
</header> | ||
) | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ export const ALL_POSTS_QUERY = gql` | |
} | ||
} | ||
` | ||
|
||
export const allPostsQueryVars = { | ||
skip: 0, | ||
first: 10, | ||
|
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,38 @@ | ||
import { useMemo } from 'react' | ||
import { ApolloClient } from 'apollo-client' | ||
import { InMemoryCache } from 'apollo-cache-inmemory' | ||
import { HttpLink } from 'apollo-link-http' | ||
|
||
let apolloClient | ||
|
||
function createApolloClient() { | ||
return new ApolloClient({ | ||
ssrMode: typeof window === 'undefined', | ||
link: new HttpLink({ | ||
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) | ||
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` | ||
}), | ||
cache: new InMemoryCache(), | ||
}) | ||
} | ||
|
||
export function initializeApollo(initialState = null) { | ||
const _apolloClient = apolloClient ?? createApolloClient() | ||
|
||
// If your page has Next.js data fetching methods that use Apollo Client, the initial state | ||
// get 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApolloProvider } from '@apollo/react-hooks' | ||
import { useApollo } from '../lib/apolloClient' | ||
|
||
export default function App({ Component, pageProps }) { | ||
const apolloClient = useApollo(pageProps.initialApolloState) | ||
|
||
return ( | ||
<ApolloProvider client={apolloClient}> | ||
<Component {...pageProps} /> | ||
</ApolloProvider> | ||
) | ||
} |
Oops, something went wrong.