|
| 1 | +# @react-libraries/next-exchange-ssr |
| 2 | + |
| 3 | +SSR on Next.js using @apollo/client's useSuspenseQuery. |
| 4 | +To use it, simply add ApolloSSRProvider under ApolloProvider. |
| 5 | + |
| 6 | +## Sample |
| 7 | + |
| 8 | +- Source |
| 9 | + <https://github.com/SoraKumo001/next-apollo-ssr> |
| 10 | +- App |
| 11 | + <https://next-apollo-ssr-six.vercel.app/> |
| 12 | + |
| 13 | +### src/pages/\_app.tsx |
| 14 | + |
| 15 | +```tsx |
| 16 | +import type { AppType } from "next/app"; |
| 17 | +import { useState } from "react"; |
| 18 | +import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"; |
| 19 | +import { ApolloSSRProvider } from "@react-libraries/apollo-ssr"; |
| 20 | + |
| 21 | +const uri = "https://graphql.anilist.co"; |
| 22 | + |
| 23 | +const App: AppType = ({ Component }) => { |
| 24 | + const [client] = useState( |
| 25 | + () => |
| 26 | + new ApolloClient({ |
| 27 | + uri, |
| 28 | + cache: new InMemoryCache({}), |
| 29 | + }) |
| 30 | + ); |
| 31 | + return ( |
| 32 | + <ApolloProvider client={client}> |
| 33 | + {/* ←Add this */} |
| 34 | + <ApolloSSRProvider> |
| 35 | + <Component /> |
| 36 | + </ApolloSSRProvider> |
| 37 | + </ApolloProvider> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +// getInitialProps itself is not needed, but it is needed to prevent optimization of _app.tsx |
| 42 | +// If you don't include this, it will be executed at build time and will not be called after that. |
| 43 | +App.getInitialProps = () => ({}); |
| 44 | + |
| 45 | +export default App; |
| 46 | +``` |
| 47 | + |
| 48 | +### src/pages/index.tsx |
| 49 | + |
| 50 | +```tsx |
| 51 | +import { gql, useApolloClient, useSuspenseQuery } from "@apollo/client"; |
| 52 | +import Link from "next/link"; |
| 53 | +import { useRouter } from "next/router"; |
| 54 | +import { Suspense } from "react"; |
| 55 | + |
| 56 | +// Retrieving the animation list |
| 57 | +const QUERY = gql` |
| 58 | + query Query($page: Int, $perPage: Int) { |
| 59 | + Page(page: $page, perPage: $perPage) { |
| 60 | + media { |
| 61 | + id |
| 62 | + title { |
| 63 | + english |
| 64 | + native |
| 65 | + } |
| 66 | + } |
| 67 | + pageInfo { |
| 68 | + currentPage |
| 69 | + hasNextPage |
| 70 | + lastPage |
| 71 | + perPage |
| 72 | + total |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +`; |
| 77 | + |
| 78 | +type PageData = { |
| 79 | + Page: { |
| 80 | + media: { |
| 81 | + id: number; |
| 82 | + siteUrl: string; |
| 83 | + title: { english: string; native: string }; |
| 84 | + }[]; |
| 85 | + pageInfo: { |
| 86 | + currentPage: number; |
| 87 | + hasNextPage: boolean; |
| 88 | + lastPage: number; |
| 89 | + perPage: number; |
| 90 | + total: number; |
| 91 | + }; |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +const AnimationList = ({ page }: { page: number }) => { |
| 96 | + const client = useApolloClient(); |
| 97 | + const { data, refetch } = useSuspenseQuery<PageData>(QUERY, { |
| 98 | + variables: { page, perPage: 10 }, |
| 99 | + }); |
| 100 | + const { currentPage, lastPage } = data?.Page?.pageInfo ?? {}; |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <button onClick={() => refetch()}>Refetch</button> |
| 104 | + <button onClick={() => client.resetStore()}>Reset</button> |
| 105 | + <div> |
| 106 | + <Link href={`/?page=${currentPage - 1}`}> |
| 107 | + <button disabled={currentPage <= 1}>←</button> |
| 108 | + </Link> |
| 109 | + <Link href={`/?page=${currentPage + 1}`}> |
| 110 | + <button disabled={currentPage >= lastPage}>→</button> |
| 111 | + </Link> |
| 112 | + {currentPage}/{lastPage} |
| 113 | + </div> |
| 114 | + {data.Page.media.map((v) => ( |
| 115 | + <div |
| 116 | + key={v.id} |
| 117 | + style={{ |
| 118 | + border: "solid 1px", |
| 119 | + padding: "8px", |
| 120 | + margin: "8px", |
| 121 | + borderRadius: "4px", |
| 122 | + }} |
| 123 | + > |
| 124 | + <div> |
| 125 | + {v.title.english} / {v.title.native} |
| 126 | + </div> |
| 127 | + <a href={v.siteUrl}>{v.siteUrl}</a> |
| 128 | + </div> |
| 129 | + ))} |
| 130 | + </> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +const Page = () => { |
| 135 | + const router = useRouter(); |
| 136 | + const page = Number(router.query.page) || 1; |
| 137 | + |
| 138 | + return ( |
| 139 | + <Suspense fallback={<div>Loading</div>}> |
| 140 | + <AnimationList page={page} /> |
| 141 | + </Suspense> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +export default Page; |
| 146 | +``` |
0 commit comments