@trpc/next 10.43.6 → 10.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -69,6 +69,22 @@ function withTRPC(opts) {
69
69
  };
70
70
  if (AppOrPage.getInitialProps ?? opts.ssr) {
71
71
  WithTRPC.getInitialProps = async (appOrPageCtx)=>{
72
+ const shouldSsr = async ()=>{
73
+ if (typeof opts.ssr === 'function') {
74
+ if (typeof window !== 'undefined') {
75
+ return false;
76
+ }
77
+ try {
78
+ return await opts.ssr({
79
+ ctx: appOrPageCtx.ctx
80
+ });
81
+ } catch (e) {
82
+ return false;
83
+ }
84
+ }
85
+ return opts.ssr;
86
+ };
87
+ const ssr = await shouldSsr();
72
88
  const AppTree = appOrPageCtx.AppTree;
73
89
  // Determine if we are wrapping an App component or a Page component.
74
90
  const isApp = !!appOrPageCtx.Component;
@@ -86,7 +102,7 @@ function withTRPC(opts) {
86
102
  const getAppTreeProps = (props)=>isApp ? {
87
103
  pageProps: props
88
104
  } : props;
89
- if (typeof window !== 'undefined' || !opts.ssr) {
105
+ if (typeof window !== 'undefined' || !ssr) {
90
106
  return getAppTreeProps(pageProps);
91
107
  }
92
108
  const config = getClientConfig({
@@ -138,22 +154,24 @@ function withTRPC(opts) {
138
154
  // dehydrate query client's state and add it to the props
139
155
  pageProps.trpcState = trpcClient.runtime.combinedTransformer.output.serialize(dehydratedCacheWithErrors);
140
156
  const appTreeProps = getAppTreeProps(pageProps);
141
- const meta = opts.responseMeta?.({
142
- ctx,
143
- clientErrors: [
144
- ...dehydratedCache.queries,
145
- ...dehydratedCache.mutations
146
- ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
147
- err
148
- ] : [])
149
- }) ?? {};
150
- for (const [key, value] of Object.entries(meta.headers ?? {})){
151
- if (typeof value === 'string') {
152
- ctx.res?.setHeader(key, value);
157
+ if ('responseMeta' in opts) {
158
+ const meta = opts.responseMeta?.({
159
+ ctx,
160
+ clientErrors: [
161
+ ...dehydratedCache.queries,
162
+ ...dehydratedCache.mutations
163
+ ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
164
+ err
165
+ ] : [])
166
+ }) ?? {};
167
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
168
+ if (typeof value === 'string') {
169
+ ctx.res?.setHeader(key, value);
170
+ }
171
+ }
172
+ if (meta.status && ctx.res) {
173
+ ctx.res.statusCode = meta.status;
153
174
  }
154
- }
155
- if (meta.status && ctx.res) {
156
- ctx.res.statusCode = meta.status;
157
175
  }
158
176
  return appTreeProps;
159
177
  };
package/dist/index.mjs CHANGED
@@ -60,6 +60,22 @@ function withTRPC(opts) {
60
60
  };
61
61
  if (AppOrPage.getInitialProps ?? opts.ssr) {
62
62
  WithTRPC.getInitialProps = async (appOrPageCtx)=>{
63
+ const shouldSsr = async ()=>{
64
+ if (typeof opts.ssr === 'function') {
65
+ if (typeof window !== 'undefined') {
66
+ return false;
67
+ }
68
+ try {
69
+ return await opts.ssr({
70
+ ctx: appOrPageCtx.ctx
71
+ });
72
+ } catch (e) {
73
+ return false;
74
+ }
75
+ }
76
+ return opts.ssr;
77
+ };
78
+ const ssr = await shouldSsr();
63
79
  const AppTree = appOrPageCtx.AppTree;
64
80
  // Determine if we are wrapping an App component or a Page component.
65
81
  const isApp = !!appOrPageCtx.Component;
@@ -77,7 +93,7 @@ function withTRPC(opts) {
77
93
  const getAppTreeProps = (props)=>isApp ? {
78
94
  pageProps: props
79
95
  } : props;
80
- if (typeof window !== 'undefined' || !opts.ssr) {
96
+ if (typeof window !== 'undefined' || !ssr) {
81
97
  return getAppTreeProps(pageProps);
82
98
  }
83
99
  const config = getClientConfig({
@@ -129,22 +145,24 @@ function withTRPC(opts) {
129
145
  // dehydrate query client's state and add it to the props
130
146
  pageProps.trpcState = trpcClient.runtime.combinedTransformer.output.serialize(dehydratedCacheWithErrors);
131
147
  const appTreeProps = getAppTreeProps(pageProps);
132
- const meta = opts.responseMeta?.({
133
- ctx,
134
- clientErrors: [
135
- ...dehydratedCache.queries,
136
- ...dehydratedCache.mutations
137
- ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
138
- err
139
- ] : [])
140
- }) ?? {};
141
- for (const [key, value] of Object.entries(meta.headers ?? {})){
142
- if (typeof value === 'string') {
143
- ctx.res?.setHeader(key, value);
148
+ if ('responseMeta' in opts) {
149
+ const meta = opts.responseMeta?.({
150
+ ctx,
151
+ clientErrors: [
152
+ ...dehydratedCache.queries,
153
+ ...dehydratedCache.mutations
154
+ ].map((v)=>v.state.error).flatMap((err)=>err instanceof Error && err.name === 'TRPCClientError' ? [
155
+ err
156
+ ] : [])
157
+ }) ?? {};
158
+ for (const [key, value] of Object.entries(meta.headers ?? {})){
159
+ if (typeof value === 'string') {
160
+ ctx.res?.setHeader(key, value);
161
+ }
162
+ }
163
+ if (meta.status && ctx.res) {
164
+ ctx.res.statusCode = meta.status;
144
165
  }
145
- }
146
- if (meta.status && ctx.res) {
147
- ctx.res.statusCode = meta.status;
148
166
  }
149
167
  return appTreeProps;
150
168
  };
@@ -13,7 +13,9 @@ interface WithTRPCOptions<TRouter extends AnyRouter> extends CreateTRPCReactOpti
13
13
  }) => WithTRPCConfig<TRouter>;
14
14
  }
15
15
  export interface WithTRPCSSROptions<TRouter extends AnyRouter> extends WithTRPCOptions<TRouter> {
16
- ssr: true;
16
+ ssr: true | ((opts: {
17
+ ctx: NextPageContext;
18
+ }) => boolean | Promise<boolean>);
17
19
  responseMeta?: (opts: {
18
20
  ctx: NextPageContext;
19
21
  clientErrors: TRPCClientError<TRouter>[];
@@ -1 +1 @@
1
- {"version":3,"file":"withTRPC.d.ts","sourceRoot":"","sources":["../src/withTRPC.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAIL,eAAe,EAEhB,MA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C,OAAO,SAAS,SAAS,CAC7D,SAAQ,eAAe,CAAC,OAAO,CAAC;IAChC,GAAG,CAAC,EAAE,KAAK,CAAC;CACb;AAED,wBAAgB,QAAQ,CACtB,OAAO,SAAS,SAAS,EACzB,WAAW,SAAS,eAAe,GAAG,eAAe,EACrD,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAAG,kBAAkB,CAAC,OAAO,CAAC,eAU9C,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,KAAG,iBAAiB,CAgMxE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/next",
3
- "version": "10.43.6",
3
+ "version": "10.44.0",
4
4
  "description": "The tRPC Next.js library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -66,9 +66,9 @@
66
66
  ],
67
67
  "peerDependencies": {
68
68
  "@tanstack/react-query": "^4.18.0",
69
- "@trpc/client": "10.43.6",
70
- "@trpc/react-query": "10.43.6",
71
- "@trpc/server": "10.43.6",
69
+ "@trpc/client": "10.44.0",
70
+ "@trpc/react-query": "10.44.0",
71
+ "@trpc/server": "10.44.0",
72
72
  "next": "*",
73
73
  "react": ">=16.8.0",
74
74
  "react-dom": ">=16.8.0"
@@ -78,9 +78,9 @@
78
78
  },
79
79
  "devDependencies": {
80
80
  "@tanstack/react-query": "^4.18.0",
81
- "@trpc/client": "10.43.6",
82
- "@trpc/react-query": "10.43.6",
83
- "@trpc/server": "10.43.6",
81
+ "@trpc/client": "10.44.0",
82
+ "@trpc/react-query": "10.44.0",
83
+ "@trpc/server": "10.44.0",
84
84
  "@types/express": "^4.17.17",
85
85
  "@types/node": "^18.16.16",
86
86
  "@types/react": "^18.2.33",
@@ -100,5 +100,5 @@
100
100
  "funding": [
101
101
  "https://trpc.io/sponsor"
102
102
  ],
103
- "gitHead": "edceb5e4c35c67dadec600a4a8db66112c87ade8"
103
+ "gitHead": "d655d3389f2a245e9d21ae98bc4c796bed0826f7"
104
104
  }
package/src/withTRPC.tsx CHANGED
@@ -69,7 +69,7 @@ interface WithTRPCOptions<TRouter extends AnyRouter>
69
69
 
70
70
  export interface WithTRPCSSROptions<TRouter extends AnyRouter>
71
71
  extends WithTRPCOptions<TRouter> {
72
- ssr: true;
72
+ ssr: true | ((opts: { ctx: NextPageContext }) => boolean | Promise<boolean>);
73
73
  responseMeta?: (opts: {
74
74
  ctx: NextPageContext;
75
75
  clientErrors: TRPCClientError<TRouter>[];
@@ -145,6 +145,20 @@ export function withTRPC<
145
145
 
146
146
  if (AppOrPage.getInitialProps ?? opts.ssr) {
147
147
  WithTRPC.getInitialProps = async (appOrPageCtx: AppContextType) => {
148
+ const shouldSsr = async () => {
149
+ if (typeof opts.ssr === 'function') {
150
+ if (typeof window !== 'undefined') {
151
+ return false;
152
+ }
153
+ try {
154
+ return await opts.ssr({ ctx: appOrPageCtx.ctx });
155
+ } catch (e) {
156
+ return false;
157
+ }
158
+ }
159
+ return opts.ssr;
160
+ };
161
+ const ssr = await shouldSsr();
148
162
  const AppTree = appOrPageCtx.AppTree;
149
163
 
150
164
  // Determine if we are wrapping an App component or a Page component.
@@ -171,7 +185,7 @@ export function withTRPC<
171
185
  const getAppTreeProps = (props: Record<string, unknown>) =>
172
186
  isApp ? { pageProps: props } : props;
173
187
 
174
- if (typeof window !== 'undefined' || !opts.ssr) {
188
+ if (typeof window !== 'undefined' || !ssr) {
175
189
  return getAppTreeProps(pageProps);
176
190
  }
177
191
 
@@ -236,29 +250,32 @@ export function withTRPC<
236
250
 
237
251
  const appTreeProps = getAppTreeProps(pageProps);
238
252
 
239
- const meta =
240
- opts.responseMeta?.({
241
- ctx,
242
- clientErrors: [
243
- ...dehydratedCache.queries,
244
- ...dehydratedCache.mutations,
245
- ]
246
- .map((v) => v.state.error)
247
- .flatMap((err) =>
248
- err instanceof Error && err.name === 'TRPCClientError'
249
- ? [err as TRPCClientError<TRouter>]
250
- : [],
251
- ),
252
- }) ?? {};
253
+ if ('responseMeta' in opts) {
254
+ const meta =
255
+ opts.responseMeta?.({
256
+ ctx,
257
+ clientErrors: [
258
+ ...dehydratedCache.queries,
259
+ ...dehydratedCache.mutations,
260
+ ]
261
+ .map((v) => v.state.error)
262
+ .flatMap((err) =>
263
+ err instanceof Error && err.name === 'TRPCClientError'
264
+ ? [err as TRPCClientError<TRouter>]
265
+ : [],
266
+ ),
267
+ }) ?? {};
253
268
 
254
- for (const [key, value] of Object.entries(meta.headers ?? {})) {
255
- if (typeof value === 'string') {
256
- ctx.res?.setHeader(key, value);
269
+ for (const [key, value] of Object.entries(meta.headers ?? {})) {
270
+ if (typeof value === 'string') {
271
+ ctx.res?.setHeader(key, value);
272
+ }
273
+ }
274
+ if (meta.status && ctx.res) {
275
+ ctx.res.statusCode = meta.status;
257
276
  }
258
277
  }
259
- if (meta.status && ctx.res) {
260
- ctx.res.statusCode = meta.status;
261
- }
278
+
262
279
  return appTreeProps;
263
280
  };
264
281
  }