-
-
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.
fix(start): fix serverOnly implementation/types, and add clientOnly c…
…ounterpart (#2954) * Fix type of serverOnly and add clientOnly function * Replace invalid invariant call with throwing an error, and support clientOnly * only build handlers once and reuse * write type test and make linter happy * Add snapshot testing for serverOnly and clientOnly * Write e2e tests * use a single source of truth for tokens to transform and do dead code elimination after
- Loading branch information
1 parent
52e9d4f
commit 402bae6
Showing
19 changed files
with
373 additions
and
48 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,76 @@ | ||
import { createFileRoute } from '@tanstack/react-router' | ||
import { clientOnly, createServerFn, serverOnly } from '@tanstack/start' | ||
import { useState } from 'react' | ||
|
||
const serverEcho = serverOnly((input: string) => 'server got: ' + input) | ||
const clientEcho = clientOnly((input: string) => 'client got: ' + input) | ||
|
||
const testOnServer = createServerFn().handler(() => { | ||
const serverOnServer = serverEcho('hello') | ||
let clientOnServer: string | ||
try { | ||
clientOnServer = clientEcho('hello') | ||
} catch (e) { | ||
clientOnServer = | ||
'clientEcho threw an error: ' + | ||
(e instanceof Error ? e.message : String(e)) | ||
} | ||
return { serverOnServer, clientOnServer } | ||
}) | ||
|
||
export const Route = createFileRoute('/env-only')({ | ||
component: RouteComponent, | ||
}) | ||
|
||
function RouteComponent() { | ||
const [results, setResults] = useState<Partial<Record<string, string>>>() | ||
|
||
async function handleClick() { | ||
const { serverOnServer, clientOnServer } = await testOnServer() | ||
const clientOnClient = clientEcho('hello') | ||
let serverOnClient: string | ||
try { | ||
serverOnClient = serverEcho('hello') | ||
} catch (e) { | ||
serverOnClient = | ||
'serverEcho threw an error: ' + | ||
(e instanceof Error ? e.message : String(e)) | ||
} | ||
setResults({ | ||
serverOnServer, | ||
clientOnServer, | ||
clientOnClient, | ||
serverOnClient, | ||
}) | ||
} | ||
|
||
const { serverOnServer, clientOnServer, clientOnClient, serverOnClient } = | ||
results || {} | ||
|
||
return ( | ||
<div> | ||
<button onClick={handleClick} data-testid="test-env-only-results-btn"> | ||
Run | ||
</button> | ||
{!!results && ( | ||
<div> | ||
<h1> | ||
<code>serverEcho</code> | ||
</h1> | ||
When we called the function on the server: | ||
<pre data-testid="server-on-server">{serverOnServer}</pre> | ||
When we called the function on the client: | ||
<pre data-testid="server-on-client">{serverOnClient}</pre> | ||
<br /> | ||
<h1> | ||
<code>clientEcho</code> | ||
</h1> | ||
When we called the function on the server: | ||
<pre data-testid="client-on-server">{clientOnServer}</pre> | ||
When we called the function on the client: | ||
<pre data-testid="client-on-client">{clientOnClient}</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.