Skip to content

Commit

Permalink
create instance once
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 16, 2024
1 parent 7faa51d commit 3d26450
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { ActionAsyncStorage } from './action-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage'

export const actionAsyncStorage: ActionAsyncStorage = createAsyncLocalStorage()
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { AsyncLocalStorage } from 'async_hooks'
import { createAsyncLocalStorage } from './async-local-storage'

// Share the instance module in the next-shared layer
;('TURBOPACK { transition: next-shared }')
import { actionAsyncStorage } from './action-async-storage-instance'
export interface ActionStore {
readonly isAction?: boolean
readonly isAppRoute?: boolean
}

export type ActionAsyncStorage = AsyncLocalStorage<ActionStore>

export const actionAsyncStorage: ActionAsyncStorage = createAsyncLocalStorage()
export { actionAsyncStorage }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createAsyncLocalStorage } from './async-local-storage'
import type { RequestAsyncStorage } from './request-async-storage.external'

export const requestAsyncStorage: RequestAsyncStorage =
createAsyncLocalStorage()
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type { ResponseCookies } from '../../server/web/spec-extension/cookies'
import type { ReadonlyHeaders } from '../../server/web/spec-extension/adapters/headers'
import type { ReadonlyRequestCookies } from '../../server/web/spec-extension/adapters/request-cookies'

// Share the instance module in the next-shared layer
;('TURBOPACK { transition: next-shared }')
import { createAsyncLocalStorage } from './async-local-storage'
import { requestAsyncStorage } from './request-async-storage-instance'
import type { DeepReadonly } from '../../shared/lib/deep-readonly'

export interface RequestStore {
Expand All @@ -21,8 +22,7 @@ export interface RequestStore {

export type RequestAsyncStorage = AsyncLocalStorage<RequestStore>

export const requestAsyncStorage: RequestAsyncStorage =
createAsyncLocalStorage()
export { requestAsyncStorage }

export function getExpectedRequestStore(callingExpression: string) {
const store = requestAsyncStorage.getStore()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { StaticGenerationAsyncStorage } from './static-generation-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage'

export const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
createAsyncLocalStorage()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { FetchMetrics } from '../../server/base-http'
import type { Revalidate } from '../../server/lib/revalidate'
import type { PrerenderState } from '../../server/app-render/dynamic-rendering'

import { createAsyncLocalStorage } from './async-local-storage'
// Share the instance module in the next-shared layer
;('TURBOPACK { transition: next-shared }')
import { staticGenerationAsyncStorage } from './static-generation-async-storage-instance'

export interface StaticGenerationStore {
readonly isStaticGeneration: boolean
Expand Down Expand Up @@ -53,5 +55,4 @@ export interface StaticGenerationStore {
export type StaticGenerationAsyncStorage =
AsyncLocalStorage<StaticGenerationStore>

export const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
createAsyncLocalStorage()
export { staticGenerationAsyncStorage }
6 changes: 3 additions & 3 deletions packages/next/src/shared/lib/lazy-dynamic/preload-css.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use client'

import { requestAsyncStorage } from '../../../client/components/request-async-storage.external'
import { getExpectedRequestStore } from '../../../client/components/request-async-storage.external'

export function PreloadCss({ moduleIds }: { moduleIds: string[] | undefined }) {
// Early return in client compilation and only load requestStore on server side
if (typeof window !== 'undefined') {
return null
}

const requestStore = requestAsyncStorage.getStore()
const requestStore = getExpectedRequestStore('next/dynamic css')
const allFiles = []

// Search the current dynamic call unique key id in react loadable manifest,
// and find the corresponding CSS files to preload
if (requestStore?.reactLoadableManifest && moduleIds) {
if (requestStore.reactLoadableManifest && moduleIds) {
const manifest = requestStore.reactLoadableManifest
for (const key of moduleIds) {
if (!manifest[key]) continue
Expand Down

0 comments on commit 3d26450

Please sign in to comment.