Skip to content

Commit

Permalink
fix(start): pass serverBase into create rpc functions (#3214)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Jan 22, 2025
1 parent 13d8cca commit 26badd5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 44 deletions.
7 changes: 3 additions & 4 deletions packages/server-functions-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import type {
ReplacerFn,
} from '@tanstack/directive-functions-plugin'

export type CreateClientRpcFn = (functionId: string) => any
export type CreateSsrRpcFn = (functionId: string) => any
export type CreateServerRpcFn = (
export type CreateRpcFn = (
functionId: string,
splitImportFn: string,
serverBase: string,
splitImportFn?: string,
) => any

declare global {
Expand Down
10 changes: 4 additions & 6 deletions packages/start-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,19 @@ export function defineConfig(
client: {
getRuntimeCode: () =>
`import { createClientRpc } from '@tanstack/start/server-functions-client'`,
replacer: (opts) => `createClientRpc(${JSON.stringify(opts.functionId)})`,
replacer: (opts) =>
`createClientRpc('${opts.functionId}', '${serverBase}')`,
},
ssr: {
getRuntimeCode: () =>
`import { createSsrRpc } from '@tanstack/start/server-functions-ssr'`,
replacer: (opts) => `createSsrRpc(${JSON.stringify(opts.functionId)})`,
replacer: (opts) => `createSsrRpc('${opts.functionId}', '${serverBase}')`,
},
server: {
getRuntimeCode: () =>
`import { createServerRpc } from '@tanstack/start/server-functions-server'`,
replacer: (opts) =>
`createServerRpc(${JSON.stringify(opts.functionId)}, ${opts.fn})`,
`createServerRpc('${opts.functionId}', '${serverBase}', ${opts.fn})`,
},
})

Expand Down Expand Up @@ -178,7 +179,6 @@ export function defineConfig(
...injectDefineEnv('TSS_PUBLIC_BASE', publicBase),
...injectDefineEnv('TSS_CLIENT_BASE', clientBase),
...injectDefineEnv('TSS_API_BASE', apiBase),
...injectDefineEnv('TSS_SERVER_FN_BASE', serverBase),
},
ssr: mergeSsrOptions([
viteConfig.userConfig.ssr,
Expand Down Expand Up @@ -239,7 +239,6 @@ export function defineConfig(
...injectDefineEnv('TSS_PUBLIC_BASE', publicBase),
...injectDefineEnv('TSS_CLIENT_BASE', clientBase),
...injectDefineEnv('TSS_API_BASE', apiBase),
...injectDefineEnv('TSS_SERVER_FN_BASE', serverBase),
},
ssr: mergeSsrOptions([
viteConfig.userConfig.ssr,
Expand Down Expand Up @@ -408,7 +407,6 @@ export function defineConfig(
...injectDefineEnv('TSS_PUBLIC_BASE', publicBase),
...injectDefineEnv('TSS_CLIENT_BASE', clientBase),
...injectDefineEnv('TSS_API_BASE', apiBase),
...injectDefineEnv('TSS_SERVER_FN_BASE', serverBase),
},
}),
TanStackRouterVite({
Expand Down
12 changes: 2 additions & 10 deletions packages/start-server-functions-client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import invariant from 'tiny-invariant'
import { serverFnFetcher } from '@tanstack/start-server-functions-fetcher'
import type { CreateClientRpcFn } from '@tanstack/server-functions-plugin'

const serverBase = process.env.TSS_SERVER_FN_BASE
import type { CreateRpcFn } from '@tanstack/server-functions-plugin'

function sanitizeBase(base: string) {
return base.replace(/^\/|\/$/g, '')
}

export const createClientRpc: CreateClientRpcFn = (functionId) => {
invariant(
serverBase,
'🚨A process.env.TSS_SERVER_FN_BASE env variable is required for the server functions client runtime, but was not provided.',
)

export const createClientRpc: CreateRpcFn = (functionId, serverBase) => {
const url = `/${sanitizeBase(serverBase)}/${functionId}`

const clientFn = (...args: Array<any>) => {
Expand Down
23 changes: 9 additions & 14 deletions packages/start-server-functions-server/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import invariant from 'tiny-invariant'
import type { CreateServerRpcFn } from '@tanstack/server-functions-plugin'

const serverBase = process.env.TSS_SERVER_FN_BASE
import type { CreateRpcFn } from '@tanstack/server-functions-plugin'

function sanitizeBase(base: string) {
return base.replace(/^\/|\/$/g, '')
}

export function getFunctionUrl(functionId: string) {
invariant(
serverBase,
'🚨A process.env.TSS_SERVER_FN_BASE env variable is required for the server functions ssr runtime, but was not provided.',
)

return `/${sanitizeBase(serverBase)}/${functionId}`
}

export const createServerRpc: CreateServerRpcFn = (
export const createServerRpc: CreateRpcFn = (
functionId,
serverBase,
splitImportFn,
) => {
const url = getFunctionUrl(functionId)
invariant(
splitImportFn,
'🚨splitImportFnis required for the server functions ssr runtime, but was not provided.',
)

const url = `/${sanitizeBase(serverBase)}/${functionId}`

return Object.assign(splitImportFn, {
url,
Expand Down
12 changes: 2 additions & 10 deletions packages/start-server-functions-ssr/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import invariant from 'tiny-invariant'
import { serverFnFetcher } from '@tanstack/start-server-functions-fetcher'
import type { CreateSsrRpcFn } from '@tanstack/server-functions-plugin'

const serverBase = process.env.TSS_SERVER_FN_BASE
import type { CreateRpcFn } from '@tanstack/server-functions-plugin'

function sanitizeBase(base: string) {
return base.replace(/^\/|\/$/g, '')
}

export const createSsrRpc: CreateSsrRpcFn = (functionId) => {
invariant(
serverBase,
'🚨A process.env.TSS_SERVER_FN_BASE env variable is required for the server functions ssr runtime, but was not provided.',
)

export const createSsrRpc: CreateRpcFn = (functionId, serverBase) => {
const url = `/${sanitizeBase(serverBase)}/${functionId}`

const ssrFn = (...args: Array<any>) => {
Expand Down

0 comments on commit 26badd5

Please sign in to comment.