-
Notifications
You must be signed in to change notification settings - Fork 43
Cloudflare integration test #458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vladinator1000
wants to merge
9
commits into
apollographql:main
Choose a base branch
from
vladinator1000:pr/react-router-cloudflare-itest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,924
−184
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b249c0
add cloudflare integration test
vladinator1000 2e54779
update readme
vladinator1000 c8e9471
update gitignore
vladinator1000 7826676
update typecheck command
vladinator1000 14dc600
update gitignore
vladinator1000 c46fa38
add shell error logging logic
vladinator1000 4109d4f
reuse test
vladinator1000 4800755
remove unused file
vladinator1000 9ca17e3
remove @react-router-cloudflare tag
vladinator1000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,9 @@ | ||
| .DS_Store | ||
| /node_modules/ | ||
|
|
||
| # React Router | ||
| /.react-router/ | ||
| /build/ | ||
|
|
||
| /worker-configuration.d.ts | ||
| /.wrangler |
This file contains hidden or 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,3 @@ | ||
| # React Router Cloudflare integration test | ||
|
|
||
| Useful to check if we run well on runtimes that don't support Node APIs such as `renderToPipeableStream`. |
This file contains hidden or 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,25 @@ | ||
| import { ApolloLink, HttpLink, InMemoryCache } from "@apollo/client/index.js"; | ||
| import { | ||
| createApolloLoaderHandler, | ||
| ApolloClient, | ||
| } from "@apollo/client-integration-react-router"; | ||
| import { IncrementalSchemaLink } from "@integration-test/shared/IncrementalSchemaLink"; | ||
| import { schema } from "@integration-test/shared/schema"; | ||
| import { delayLink } from "@integration-test/shared/delayLink"; | ||
| import { errorLink } from "@integration-test/shared/errorLink"; | ||
|
|
||
| const link = ApolloLink.from([ | ||
| delayLink, | ||
| errorLink, | ||
| typeof window === "undefined" | ||
| ? (new IncrementalSchemaLink({ schema }) as any as ApolloLink) | ||
| : new HttpLink({ uri: "/graphql" }), | ||
| ]); | ||
|
|
||
| export const makeClient = (request?: Request) => { | ||
| return new ApolloClient({ | ||
| cache: new InMemoryCache(), | ||
| link, | ||
| }); | ||
| }; | ||
| export const apolloLoader = createApolloLoaderHandler(makeClient); |
17 changes: 17 additions & 0 deletions
17
integration-test/react-router-cloudflare/app/entry.client.tsx
This file contains hidden or 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,17 @@ | ||
| import { startTransition, StrictMode } from "react"; | ||
| import { hydrateRoot } from "react-dom/client"; | ||
| import { HydratedRouter } from "react-router/dom"; | ||
| import { makeClient } from "./apollo"; | ||
| import { ApolloProvider } from "@apollo/client/react/index.js"; | ||
|
|
||
| startTransition(() => { | ||
| const client = makeClient(); | ||
| hydrateRoot( | ||
| document, | ||
| <StrictMode> | ||
| <ApolloProvider client={client}> | ||
| <HydratedRouter /> | ||
| </ApolloProvider> | ||
| </StrictMode> | ||
| ); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
integration-test/react-router-cloudflare/app/entry.server.tsx
This file contains hidden or 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,73 @@ | ||
| import type { AppLoadContext, EntryContext } from "react-router"; | ||
| import { ServerRouter } from "react-router"; | ||
| import { isbot } from "isbot"; | ||
| import type { | ||
| RenderToPipeableStreamOptions, | ||
| RenderToReadableStreamOptions, | ||
| } from "react-dom/server"; | ||
| import { renderToReadableStream } from "react-dom/server"; | ||
| import { makeClient } from "./apollo"; | ||
| import { ApolloProvider } from "@apollo/client/react/index.js"; | ||
|
|
||
| export const streamTimeout = 5_000; | ||
| export type RenderOptions = { | ||
| [K in keyof RenderToReadableStreamOptions & | ||
| keyof RenderToPipeableStreamOptions]?: RenderToReadableStreamOptions[K]; | ||
| }; | ||
|
|
||
| // Based on this template https://github.com/cloudflare/templates/blob/staging/react-router-starter-template/app/entry.server.tsx | ||
| export default async function handleRequest( | ||
| request: Request, | ||
| responseStatusCode: number, | ||
| responseHeaders: Headers, | ||
| routerContext: EntryContext, | ||
| loadContext: AppLoadContext, | ||
| // vercel-specific options, originating from `@vercel/react-router/entry.server.js` | ||
| options?: RenderOptions | ||
| ) { | ||
| let shellRendered = false; | ||
| const userAgent = request.headers.get("user-agent"); | ||
| const client = makeClient(request); | ||
|
|
||
| const abortController = new AbortController(); | ||
| setTimeout(() => { | ||
| abortController.abort(`Rendering exceed timeout of ${streamTimeout}ms`); | ||
| }, streamTimeout + 1000); | ||
|
|
||
| responseHeaders.set("Content-Type", "text/html"); | ||
|
|
||
| const stream = await renderToReadableStream( | ||
| <ApolloProvider client={client}> | ||
| <ServerRouter | ||
| context={routerContext} | ||
| url={request.url} | ||
| nonce={options?.nonce} | ||
| /> | ||
| </ApolloProvider>, | ||
| { | ||
| ...options, | ||
| signal: abortController.signal, | ||
| onError(error: unknown) { | ||
| responseStatusCode = 500; | ||
|
|
||
| if (shellRendered) { | ||
| console.error(error); | ||
| } | ||
| }, | ||
| } | ||
| ); | ||
| shellRendered = true; | ||
|
|
||
| // Ensure requests from bots and SPA Mode renders wait for all content to load before responding | ||
| // https://react.dev/reference/react-dom/server/renderToReadableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation | ||
| const isCrawler = (userAgent && isbot(userAgent)) || routerContext.isSpaMode; | ||
|
|
||
| if (isCrawler) { | ||
| await stream.allReady; | ||
| } | ||
|
|
||
| return new Response(stream, { | ||
| headers: responseHeaders, | ||
| status: responseStatusCode, | ||
| }) | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
integration-test/react-router-cloudflare/app/entry.worker.ts
This file contains hidden or 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,14 @@ | ||
| import { createRequestHandler } from "react-router"; | ||
|
|
||
| const requestHandler = createRequestHandler( | ||
| () => import("virtual:react-router/server-build"), | ||
| import.meta.env.MODE | ||
| ); | ||
|
|
||
| export default { | ||
| fetch(request, env, ctx) { | ||
| return requestHandler(request, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Server entrypoint. |
||
| cloudflare: { env, ctx }, | ||
| }); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
This file contains hidden or 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,62 @@ | ||
| import { | ||
| isRouteErrorResponse, | ||
| Links, | ||
| Meta, | ||
| Outlet, | ||
| Scripts, | ||
| ScrollRestoration, | ||
| } from "react-router"; | ||
|
|
||
| import type { Route } from "./+types/root"; | ||
| import { ApolloHydrationHelper } from "@apollo/client-integration-react-router"; | ||
|
|
||
| export function Layout({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charSet="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <Meta /> | ||
| <Links /> | ||
| </head> | ||
| <body> | ||
| <ApolloHydrationHelper>{children}</ApolloHydrationHelper> | ||
| <ScrollRestoration /> | ||
| <Scripts /> | ||
| </body> | ||
| </html> | ||
| ); | ||
| } | ||
|
|
||
| export default function App() { | ||
| return <Outlet />; | ||
| } | ||
|
|
||
| export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { | ||
| let message = "Oops!"; | ||
| let details = "An unexpected error occurred."; | ||
| let stack: string | undefined; | ||
|
|
||
| if (isRouteErrorResponse(error)) { | ||
| message = error.status === 404 ? "404" : "Error"; | ||
| details = | ||
| error.status === 404 | ||
| ? "The requested page could not be found." | ||
| : error.statusText || details; | ||
| } else if (import.meta.env.DEV && error && error instanceof Error) { | ||
| details = error.message; | ||
| stack = error.stack; | ||
| } | ||
|
|
||
| return ( | ||
| <main className="pt-16 p-4 container mx-auto"> | ||
| <h1>{message}</h1> | ||
| <p>{details}</p> | ||
| {stack && ( | ||
| <pre className="w-full p-4 overflow-x-auto"> | ||
| <code>{stack}</code> | ||
| </pre> | ||
| )} | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or 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,3 @@ | ||
| import { flatRoutes } from "@react-router/fs-routes"; | ||
|
|
||
| export default flatRoutes(); |
70 changes: 70 additions & 0 deletions
70
integration-test/react-router-cloudflare/app/routes/_index.tsx
This file contains hidden or 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,70 @@ | ||
| import { useLoaderData } from "react-router"; | ||
| import type { Route } from "./+types/_index"; | ||
| import { | ||
| useApolloClient, | ||
| useQueryRefHandlers, | ||
| useReadQuery, | ||
| } from "@apollo/client/react/index.js"; | ||
| import { apolloLoader } from "~/apollo"; | ||
| import { DEFERRED_QUERY } from "@integration-test/shared/queries"; | ||
| import { useTransition } from "react"; | ||
|
|
||
| export const loader = apolloLoader<Route.LoaderArgs>()(({ preloadQuery }) => { | ||
| const queryRef = preloadQuery(DEFERRED_QUERY, { | ||
| variables: { delayDeferred: 1000 }, | ||
| }); | ||
| return { | ||
| queryRef, | ||
| }; | ||
| }); | ||
|
|
||
| export default function Home() { | ||
| const { queryRef } = useLoaderData<typeof loader>(); | ||
|
|
||
| const { refetch } = useQueryRefHandlers(queryRef); | ||
| const [refetching, startTransition] = useTransition(); | ||
| const { data } = useReadQuery(queryRef); | ||
| const client = useApolloClient(); | ||
|
|
||
| return ( | ||
| <> | ||
| <ul> | ||
| {data.products.map(({ id, title, rating }) => ( | ||
| <li key={id}> | ||
| {title} | ||
| <br /> | ||
| Rating:{" "} | ||
| <div style={{ display: "inline-block", verticalAlign: "text-top" }}> | ||
| {rating?.value || ""} | ||
| <br /> | ||
| {rating ? `Queried in ${rating.env} environment` : "loading..."} | ||
| </div> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| <p>Queried in {data.env} environment</p> | ||
| <button | ||
| disabled={refetching} | ||
| onClick={() => { | ||
| client.cache.batch({ | ||
| update(cache) { | ||
| for (const product of data.products) { | ||
| cache.modify({ | ||
| id: cache.identify(product), | ||
| fields: { | ||
| rating: () => null, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
| startTransition(() => { | ||
| refetch(); | ||
| }); | ||
| }} | ||
| > | ||
| {refetching ? "refetching..." : "refetch"} | ||
| </button> | ||
| </> | ||
| ); | ||
| } |
73 changes: 73 additions & 0 deletions
73
integration-test/react-router-cloudflare/app/routes/asyncLoader.tsx
This file contains hidden or 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,73 @@ | ||
| import { useLoaderData } from "react-router"; | ||
| import type { Route } from "./+types/asyncLoader"; | ||
| import { | ||
| useApolloClient, | ||
| useQueryRefHandlers, | ||
| useReadQuery, | ||
| } from "@apollo/client/react/index.js"; | ||
| import { apolloLoader } from "~/apollo"; | ||
| import { DEFERRED_QUERY } from "@integration-test/shared/queries"; | ||
| import { useTransition } from "react"; | ||
|
|
||
| export const loader = apolloLoader<Route.LoaderArgs>()(async ({ | ||
| preloadQuery, | ||
| }) => { | ||
| const queryRef = preloadQuery(DEFERRED_QUERY, { | ||
| variables: { delayDeferred: 1000 }, | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 300)); | ||
| return { | ||
| queryRef, | ||
| }; | ||
| }); | ||
|
|
||
| export default function Home() { | ||
| const { queryRef } = useLoaderData<typeof loader>(); | ||
|
|
||
| const { refetch } = useQueryRefHandlers(queryRef); | ||
| const [refetching, startTransition] = useTransition(); | ||
| const { data } = useReadQuery(queryRef); | ||
| const client = useApolloClient(); | ||
|
|
||
| return ( | ||
| <> | ||
| <ul> | ||
| {data.products.map(({ id, title, rating }) => ( | ||
| <li key={id}> | ||
| {title} | ||
| <br /> | ||
| Rating:{" "} | ||
| <div style={{ display: "inline-block", verticalAlign: "text-top" }}> | ||
| {rating?.value || ""} | ||
| <br /> | ||
| {rating ? `Queried in ${rating.env} environment` : "loading..."} | ||
| </div> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| <p>Queried in {data.env} environment</p> | ||
| <button | ||
| disabled={refetching} | ||
| onClick={() => { | ||
| client.cache.batch({ | ||
| update(cache) { | ||
| for (const product of data.products) { | ||
| cache.modify({ | ||
| id: cache.identify(product), | ||
| fields: { | ||
| rating: () => null, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
| startTransition(() => { | ||
| refetch(); | ||
| }); | ||
| }} | ||
| > | ||
| {refetching ? "refetching..." : "refetch"} | ||
| </button> | ||
| </> | ||
| ); | ||
| } |
4 changes: 4 additions & 0 deletions
4
integration-test/react-router-cloudflare/app/routes/graphql.ts
This file contains hidden or 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,4 @@ | ||
| import { schema } from "@integration-test/shared/schema"; | ||
| import { apiRouteHandler } from "@integration-test/shared/apiRoute"; | ||
|
|
||
| export const action = apiRouteHandler({ schema }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ported over the timeout logic from the other
react-routerintegration test. https://github.com/apollographql/apollo-client-nextjs/blob/next/integration-test/react-router/app/entry.server.tsx#L85