Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pathname and search params SSG (#41247)
Bailout to client rendering when using useSearchParams and usePathName during SSG. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
Hannes Bornö
committed
Oct 25, 2022
1 parent
2277eb9
commit 1d596ca
Showing
16 changed files
with
337 additions
and
36 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
17 changes: 17 additions & 0 deletions
17
packages/next/client/components/static-generation-bailout.ts
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,17 @@ | ||
import { DynamicServerError } from './hooks-server-context' | ||
import { staticGenerationAsyncStorage } from './static-generation-async-storage' | ||
|
||
export function staticGenerationBailout(reason: string) { | ||
const staticGenerationStore = | ||
staticGenerationAsyncStorage && 'getStore' in staticGenerationAsyncStorage | ||
? staticGenerationAsyncStorage?.getStore() | ||
: staticGenerationAsyncStorage | ||
|
||
if (staticGenerationStore?.isStaticGeneration) { | ||
// TODO: honor the dynamic: 'force-static' | ||
if (staticGenerationStore) { | ||
staticGenerationStore.revalidate = 0 | ||
} | ||
throw new DynamicServerError(reason) | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/app-static/app/hooks/use-pathname/[slug]/layout.js
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 @@ | ||
export default function Layout({ children }) { | ||
return children | ||
} | ||
|
||
export function generateStaticParams() { | ||
return [{ slug: 'slug' }] | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/app-static/app/hooks/use-pathname/[slug]/page.js
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,12 @@ | ||
'use client' | ||
import { usePathname } from 'next/navigation' | ||
|
||
export const config = { | ||
dynamicParams: false, | ||
} | ||
|
||
export default function Page() { | ||
const pathname = usePathname() | ||
|
||
return <p id="pathname">{pathname}</p> | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/app-static/app/hooks/use-search-params/[slug]/layout.js
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 @@ | ||
export default function Layout({ children }) { | ||
return children | ||
} | ||
|
||
export function generateStaticParams() { | ||
return [{ slug: 'slug' }] | ||
} |
19 changes: 19 additions & 0 deletions
19
test/e2e/app-dir/app-static/app/hooks/use-search-params/[slug]/page.js
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,19 @@ | ||
'use client' | ||
import { useSearchParams } from 'next/navigation' | ||
|
||
export const config = { | ||
dynamicParams: false, | ||
} | ||
|
||
export default function Page() { | ||
const params = useSearchParams() | ||
|
||
return ( | ||
<> | ||
<p id="params-first">{params.get('first') ?? 'N/A'}</p> | ||
<p id="params-second">{params.get('second') ?? 'N/A'}</p> | ||
<p id="params-third">{params.get('third') ?? 'N/A'}</p> | ||
<p id="params-not-real">{params.get('notReal') ?? 'N/A'}</p> | ||
</> | ||
) | ||
} |
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
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,15 @@ | ||
'use client' | ||
|
||
export default function Page({ searchParams }) { | ||
return ( | ||
<h1 | ||
id="params" | ||
data-param-first={searchParams.first ?? 'N/A'} | ||
data-param-second={searchParams.second ?? 'N/A'} | ||
data-param-third={searchParams.third ?? 'N/A'} | ||
data-param-not-real={searchParams.notReal ?? 'N/A'} | ||
> | ||
hello from searchParams prop client | ||
</h1> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/app/app/search-params-prop/server/page.js
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 @@ | ||
export default function Page({ searchParams }) { | ||
return ( | ||
<h1 | ||
id="params" | ||
data-param-first={searchParams.first ?? 'N/A'} | ||
data-param-second={searchParams.second ?? 'N/A'} | ||
data-param-third={searchParams.third ?? 'N/A'} | ||
data-param-not-real={searchParams.notReal ?? 'N/A'} | ||
> | ||
hello from searchParams prop server | ||
</h1> | ||
) | ||
} |
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.