Skip to content

Commit

Permalink
wip: неудачный переход на новый роутер Next 13 (tRPC еще не готов)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxseev committed Nov 24, 2022
1 parent 04ab2bc commit bd9f7bd
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 114 deletions.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const sentryConfig = {
silent: true
}

/** @type {import('next').NextConfig} */
const config = {
sentry: {
disableServerWebpackPlugin: !isDeploy,
disableClientWebpackPlugin: !isDeploy,
},
experimental: {
appDir: true
}
}

Expand Down
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"next-axiom": "0.15.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"superjson": "^1.11.0",
"zod": "^3.19.1"
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client"

import {FC, useEffect} from "react"
import {logger} from "@gooditworks/monitoring"

type Props = {
error: Error
}

const ErrorPage: FC<Props> = ({error}) => {
useEffect(() => {
// eslint-disable-next-line no-console
console.error(error)

logger.captureException(error)
}, [error])

return (
<div>
<h2>Something went wrong!</h2>
<pre>{error.message}</pre>
</div>
)
}

export default ErrorPage
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import {FC} from "react"

import useCounter from "hooks/useCounter"
Expand Down
11 changes: 11 additions & 0 deletions src/app/example/greeting/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {FC} from "react"

import Greeting from "components/Greeting"

const GreetingPage: FC = () => (
<div>
<Greeting />
</div>
)

export default GreetingPage
15 changes: 15 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {FC, ReactNode} from "react"

import {TRPCClientProvider} from "datasources/trpc"

import "styles/globals.css"

type Props = {
children: ReactNode
}

const RootLayout: FC<Props> = ({children}) => (
<TRPCClientProvider>{children}</TRPCClientProvider>
)

export default RootLayout
File renamed without changes.
4 changes: 3 additions & 1 deletion src/components/Counter/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {FC} from "react"

interface Props {
value: number
onIncrement: () => void
onDecrement: () => void
}

const Counter = ({value, onIncrement, onDecrement}: Props) => (
const Counter: FC<Props> = ({value, onIncrement, onDecrement}: Props) => (
<div className="flex items-center space-x-8 text-5xl">
<button onClick={onDecrement} type="button" data-test="counter_dec">
-
Expand Down
21 changes: 0 additions & 21 deletions src/components/ErrorCapturer.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use client"

import {ChangeEventHandler, FC, FormEventHandler, useState} from "react"

import trpc from "datasources/trpc"

const useGreetingName = trpc.greeting.greetName.useQuery

const GreetingPage: FC = () => {
const Greeting: FC = () => {
const [name, setName] = useState("")
const greeting = useGreetingName({name}, {enabled: false})

Expand All @@ -14,6 +16,7 @@ const GreetingPage: FC = () => {

const onSubmit: FormEventHandler<HTMLFormElement> = event => {
event.preventDefault()

greeting.refetch()
}

Expand Down Expand Up @@ -45,4 +48,4 @@ const GreetingPage: FC = () => {
)
}

export default GreetingPage
export default Greeting
34 changes: 0 additions & 34 deletions src/datasources/trpc.ts

This file was deleted.

66 changes: 66 additions & 0 deletions src/datasources/trpc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use client"

// based on https://github.com/trpc/next-13/blob/main/client/trpcClient.tsx
// TODO wait for complete Next 13 support in tRPC and migrate

import {FC, ReactNode, useState} from "react"
import {httpBatchLink} from "@trpc/client"
import {createTRPCReact} from "@trpc/react-query"
import {QueryClient, QueryClientProvider} from "@tanstack/react-query"
import superjson from "superjson"

import type {AppRouter} from "server/routers"

const getBaseUrl = () => {
if (typeof window !== "undefined") {
return ""
}

const vercelUrl = process.env.VERCEL_URL
const port = process.env.PORT ?? 3000

if (vercelUrl) {
return `https://${vercelUrl}`
}

return `http://localhost:${port}`
}

const trpc = createTRPCReact<AppRouter>({
unstable_overrides: {
useMutation: {
async onSuccess(opts) {
await opts.originalFn()
await opts.queryClient.invalidateQueries()
}
}
}
})

type Props = {
children: ReactNode
}

const TRPCClientProvider: FC<Props> = ({children}) => {
const [queryClient] = useState(() => new QueryClient())
const [trpcClient] = useState(() => {
const baseUrl = getBaseUrl()
const trpcUrl = `${baseUrl}/api/trpc`

const links = [httpBatchLink({url: trpcUrl})]

return trpc.createClient({
transformer: superjson,
links
})
})

return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</trpc.Provider>
)
}

export default trpc
export {TRPCClientProvider}
17 changes: 0 additions & 17 deletions src/pages/_app.tsx

This file was deleted.

Loading

0 comments on commit bd9f7bd

Please sign in to comment.