generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Don't rely on URL for initial state
This makes the first render on client side navigation use the source URL rather than the destination, which can cause issues if the first state is memoised or used for some other purposes. useSearchParams contains the destination values for searchParams, however this might break in older versions of Next.js (TBC by CI). In which case this fix might only land on v2.
- Loading branch information
Showing
5 changed files
with
78 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference types="cypress" /> | ||
|
||
it('Reproduction for issue #542', () => { | ||
cy.visit('/app/repro-542/a?q=foo&r=bar') | ||
cy.contains('#hydration-marker', 'hydrated').should('be.hidden') | ||
cy.get('#q').should('have.text', 'foo') | ||
cy.get('#r').should('have.text', 'bar') | ||
cy.get('a').click() | ||
cy.location('search').should('eq', '') | ||
cy.get('#q').should('have.text', '') | ||
cy.get('#r').should('have.text', '') | ||
cy.get('#initial').should('have.text', '{"q":null,"r":null}') | ||
}) |
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,29 @@ | ||
'use client' | ||
|
||
import Link from 'next/link' | ||
import { parseAsString, useQueryState, useQueryStates } from 'nuqs' | ||
import { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<Client /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
function Client() { | ||
console.log( | ||
'rendering page a, url: %s', | ||
typeof location !== 'undefined' ? location.href : 'ssr' | ||
) | ||
const [q] = useQueryState('q') | ||
const [{ r }] = useQueryStates({ r: parseAsString }) | ||
return ( | ||
<> | ||
<div id="q">{q}</div> | ||
<div id="r">{r}</div> | ||
<Link href="/app/repro-542/b">Go to page B</Link> | ||
</> | ||
) | ||
} |
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,32 @@ | ||
'use client' | ||
|
||
import { parseAsString, useQueryState, useQueryStates } from 'nuqs' | ||
import React, { Suspense } from 'react' | ||
|
||
export default function Page() { | ||
return ( | ||
<Suspense> | ||
<Client /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
function Client() { | ||
console.log( | ||
'rendering page b, url: %s', | ||
typeof location !== 'undefined' ? location.href : 'ssr' | ||
) | ||
const ref = React.useRef<any>(null) | ||
const [q] = useQueryState('q') | ||
const [{ r }] = useQueryStates({ r: parseAsString }) | ||
if (ref.current === null) { | ||
ref.current = { q, r } | ||
} | ||
return ( | ||
<> | ||
<div id="q">{q}</div> | ||
<div id="r">{r}</div> | ||
<div id="initial">{JSON.stringify(ref.current)}</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