generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow server-side parser access
See #348.
- Loading branch information
Showing
13 changed files
with
404 additions
and
280 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
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,7 @@ | ||
// This file is needed for projects that have `moduleResolution` set to `node` | ||
// in their tsconfig.json to be able to `import {} from 'next-usequerystate/parsers'`. | ||
// Other module resolutions strategies will look for the `exports` in `package.json`, | ||
// but with `node`, TypeScript will look for a .d.ts file with that name at the | ||
// root of the package. | ||
|
||
export * from './dist/parsers' |
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,54 @@ | ||
'use client' | ||
|
||
import Link from 'next/link' | ||
import { parseAsBoolean, useQueryState } from '../../../../dist' | ||
import { counterParser } from './parser' | ||
|
||
type Props = { | ||
serverSideCounter: number | ||
children: React.ReactNode | ||
} | ||
|
||
export function ServerSideParsingDemoClient({ | ||
serverSideCounter, | ||
children | ||
}: Props) { | ||
const [shallow, setShallow] = useQueryState( | ||
'shallow', | ||
parseAsBoolean.withDefault(true) | ||
) | ||
const [counter, setCounter] = useQueryState('counter', { | ||
...counterParser, | ||
shallow | ||
}) | ||
|
||
return ( | ||
<main> | ||
<Link href="/">⬅️ Home</Link> | ||
<h1>Server side parsing</h1> | ||
<div style={{ marginBottom: '1rem' }}> | ||
<input | ||
type="checkbox" | ||
checked={shallow} | ||
onChange={e => setShallow(e.target.checked)} | ||
id="shallow" | ||
/> | ||
<label htmlFor="shallow">Shallow?</label> | ||
</div> | ||
<button onClick={() => setCounter(x => x - 1)}>-</button> | ||
<button onClick={() => setCounter(x => x + 1)}>+</button> | ||
<button onClick={() => setCounter(null)}>Reset</button> | ||
<p>Client side counter: {counter}</p> | ||
<p> | ||
Server side counter: {serverSideCounter} <em>(client-rendered)</em> | ||
</p> | ||
{children} | ||
<p> | ||
<em> | ||
Check the server console, play with the "shallow" switch and try | ||
refreshing the page. | ||
</em> | ||
</p> | ||
</main> | ||
) | ||
} |
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,25 @@ | ||
import { Suspense } from 'react' | ||
import { ServerSideParsingDemoClient } from './client' | ||
import { counterParser } from './parser' | ||
|
||
type PageProps = { | ||
searchParams: { | ||
counter?: string | ||
} | ||
} | ||
|
||
export default function ServerSideParsingDemo({ searchParams }: PageProps) { | ||
const counter = | ||
counterParser.parse(searchParams.counter ?? '') ?? | ||
counterParser.defaultValue | ||
console.log('Server side counter: %d', counter) | ||
return ( | ||
<> | ||
<Suspense> | ||
<ServerSideParsingDemoClient serverSideCounter={counter}> | ||
<p>Server rendered counter: {counter}</p> | ||
</ServerSideParsingDemoClient> | ||
</Suspense> | ||
</> | ||
) | ||
} |
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,5 @@ | ||
import { parseAsInteger } from '../../../../dist/parsers' | ||
|
||
export const counterParser = parseAsInteger | ||
.withOptions({ shallow: false }) | ||
.withDefault(0) |
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.