examples/with-ably: modernize to App Router + Ably v2#1
Open
examples/with-ably: modernize to App Router + Ably v2#1
Conversation
Replaces Pages Router with App Router, upgrades to ably v2 (hooks bundled into ably/react), and creates the Realtime client inside useEffect so no connection is attempted during SSR. Tracks ably/ably-nextjs-fundamentals-kit#11.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Review PR (fork-internal)
This PR is open against
rainbowFi/next.js:canarypurely so the diff can be reviewed in the GitHub UI before a real PR is opened againstvercel/next.js. Do not merge — the changes should go upstream.Summary
Modernizes
examples/with-ablyto match the patterns inably/ably-nextjs-fundamentals-kit(reference PR #11, merged).Tracks Ably ticket AIT-540.
What changed
Routing
pages/(Pages Router)app/(App Router):layout.tsx,page.tsx,home-client.tsx,ably-client-provider.tsx,api/createTokenRequest/route.ts,api/send-message/route.tsDependencies
@ably-labs/react-hooks ^2.0.4(deprecated)ably ^1.2.22→^2.21.0(hooks now bundled inably/react)react/react-dom/@types/*/typescriptto current stableAbly client lifecycle
Ably.Realtimeis created inside auseEffectinapp/ably-client-provider.tsx, so no connection is attempted during SSRclientIdis generated once in component state viauseState(() => ...)instead of at module loaduseAblyReady()context so children can render a placeholder until the client is ready (mirrors the reference repo pattern)API routes
Ably.RealtimetoAbly.Rest. Short-lived server work doesn't need a persistent WebSocket connection.ABLY_API_KEYmissing-env guard (the original would crash unhelpfully).awaitonchannel.publishinsend-message.Other
API_ROOTenv var — the browser is same-origin andauthUrlis relative, so the prefix was never neededstyles/Home.module.cssintostyles/globals.cssREADME.mdto document the App Router +ably/reactpatternTesting
pnpm build— clean, 681ms Turbopack compile, no TS errorspnpm dev— HTTP 200 on/, rendersConnecting to Ably...placeholder during client init (expected —AblyClientProvideris client-only)/api/createTokenRequest?clientId=test-123— returns a valid signed token request.env.localcreated withABLY_API_KEY, confirmed gitignoredKnown quirks preserved from original
some-channel-namefor messages butyour-channel-namefor presence, so presence never observes chat participants. Kept 1:1 to stay within scope. Worth fixing in a follow-up commit before the upstream PR.authMethod: 'POST'from the reference repo was not adopted — kept the original example's GET default (both work; smaller diff).For the upstream PR (when ready)
<!-- NEXT_JS_LLM_PR -->at the bottom of the PR description (Next.js repo convention)