-
-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(start): createIsomorphicFn (#2942)
* Set up types for createIsomorphicFn * do dead code analysis for serverOnly and createIsomorphicFn too * add createIsomorphicFn to relevant regexes * Add test files * fix test name * Add createIsomorphicFn compiler handling * check for presence/lack of properties * Add e2e test for isomorphic functions
- Loading branch information
1 parent
a97729f
commit eeea643
Showing
18 changed files
with
620 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,61 @@ | ||
import { createFileRoute } from '@tanstack/react-router' | ||
import { createIsomorphicFn, createServerFn } from '@tanstack/start' | ||
import { useState } from 'react' | ||
|
||
const getEnv = createIsomorphicFn() | ||
.server(() => 'server') | ||
.client(() => 'client') | ||
const getServerEnv = createServerFn().handler(() => getEnv()) | ||
|
||
const getEcho = createIsomorphicFn() | ||
.server((input: string) => 'server received ' + input) | ||
.client((input) => 'client received ' + input) | ||
const getServerEcho = createServerFn() | ||
.validator((input: string) => input) | ||
.handler(({ data }) => getEcho(data)) | ||
|
||
export const Route = createFileRoute('/isomorphic-fns')({ | ||
component: RouteComponent, | ||
}) | ||
|
||
function RouteComponent() { | ||
const [results, setResults] = useState<Partial<Record<string, string>>>() | ||
async function handleClick() { | ||
const env = getEnv() | ||
const echo = getEcho('hello') | ||
const [serverEnv, serverEcho] = await Promise.all([ | ||
getServerEnv(), | ||
getServerEcho({ data: 'hello' }), | ||
]) | ||
setResults({ env, echo, serverEnv, serverEcho }) | ||
} | ||
const { env, echo, serverEnv, serverEcho } = results || {} | ||
return ( | ||
<div> | ||
<button onClick={handleClick} data-testid="test-isomorphic-results-btn"> | ||
Run | ||
</button> | ||
{!!results && ( | ||
<div> | ||
<h1> | ||
<code>getEnv</code> | ||
</h1> | ||
When we called the function on the server it returned: | ||
<pre data-testid="server-result">{JSON.stringify(serverEnv)}</pre> | ||
When we called the function on the client it returned: | ||
<pre data-testid="client-result">{JSON.stringify(env)}</pre> | ||
<br /> | ||
<h1> | ||
<code>echo</code> | ||
</h1> | ||
When we called the function on the server it returned: | ||
<pre data-testid="server-echo-result"> | ||
{JSON.stringify(serverEcho)} | ||
</pre> | ||
When we called the function on the client it returned: | ||
<pre data-testid="client-echo-result">{JSON.stringify(echo)}</pre> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} |
This file contains 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 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 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
Oops, something went wrong.