From 4cf2dbbcfdb303c5b795df442b9a2cbd095e2f62 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Sat, 18 May 2024 17:26:10 +0400 Subject: [PATCH] implement get-params for rsc --- packages/next/src/client/components/get-params.ts | 11 +++++++++++ .../static-generation-async-storage.external.ts | 2 ++ .../static-generation-async-storage-wrapper.ts | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 packages/next/src/client/components/get-params.ts diff --git a/packages/next/src/client/components/get-params.ts b/packages/next/src/client/components/get-params.ts new file mode 100644 index 0000000000000..4fa39660848f7 --- /dev/null +++ b/packages/next/src/client/components/get-params.ts @@ -0,0 +1,11 @@ +import { staticGenerationAsyncStorage } from './static-generation-async-storage.external' + +export const getParams = () => { + const store = staticGenerationAsyncStorage.getStore() + + if (!store) return null + + const { params } = store + + return params +} diff --git a/packages/next/src/client/components/static-generation-async-storage.external.ts b/packages/next/src/client/components/static-generation-async-storage.external.ts index fc6ac8a4ad3d6..47103d6917a3e 100644 --- a/packages/next/src/client/components/static-generation-async-storage.external.ts +++ b/packages/next/src/client/components/static-generation-async-storage.external.ts @@ -7,11 +7,13 @@ import type { PrerenderState } from '../../server/app-render/dynamic-rendering' // Share the instance module in the next-shared layer import { staticGenerationAsyncStorage } from './static-generation-async-storage-instance' with { 'turbopack-transition': 'next-shared' } +import type { ParsedUrlQuery } from 'querystring' export interface StaticGenerationStore { readonly isStaticGeneration: boolean readonly pagePath?: string readonly urlPathname: string + readonly params?: ParsedUrlQuery readonly incrementalCache?: IncrementalCache readonly isOnDemandRevalidate?: boolean readonly isPrerendering?: boolean diff --git a/packages/next/src/server/async-storage/static-generation-async-storage-wrapper.ts b/packages/next/src/server/async-storage/static-generation-async-storage-wrapper.ts index bf9fcb863ab98..e2d032940d0f6 100644 --- a/packages/next/src/server/async-storage/static-generation-async-storage-wrapper.ts +++ b/packages/next/src/server/async-storage/static-generation-async-storage-wrapper.ts @@ -6,6 +6,7 @@ import type { RenderOptsPartial } from '../app-render/types' import { createPrerenderState } from '../../server/app-render/dynamic-rendering' import type { FetchMetric } from '../base-http' +import type { ParsedUrlQuery } from 'querystring' export type StaticGenerationContext = { urlPathname: string @@ -17,6 +18,7 @@ export type StaticGenerationContext = { isServerAction?: boolean waitUntil?: Promise experimental?: Pick + params?: ParsedUrlQuery /** * Fetch metrics attached in patch-fetch.ts @@ -84,6 +86,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper< const store: StaticGenerationStore = { isStaticGeneration, urlPathname, + params: renderOpts.params, pagePath: renderOpts.originalPathname, incrementalCache: // we fallback to a global incremental cache for edge-runtime locally