Skip to content

Commit

Permalink
fix(core): components API execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Dec 19, 2022
1 parent 2f2e467 commit 299ac06
Showing 1 changed file with 9 additions and 5 deletions.
Expand Up @@ -43,17 +43,21 @@ export function useMiddlewareComponents<T extends {}>(props: {
const {defaultComponent, pick} = props

return useMemo(() => {
// 1. Flatten the config tree into a list of configs
const flattened = flattenConfig(options, [])
// Flatten the config tree into a list of configs
const flattened = [...flattenConfig(options, [])]

// 2. Pick the middleware components from the configs
// Since the middleware chain is executed backwards, we need to reverse the list of configs here.
// This is important because we want the order of the Components API to be consistent with the order of the other APIs.
flattened.reverse()

// Pick the middleware components from the configs
const pickedComponents = flattened.map(({config}) => pick(config))

// 3: Since we try to pick the components in all configs, some results may be undefined.
// Since we try to pick the components in all configs, some results may be undefined.
// Therefore, we filter these values out before passing the result to the middleware creator.
const result = pickedComponents.filter(Boolean)

// 4. Create the middleware component
// Create the middleware component
return _createMiddlewareComponent(defaultComponent, result)
}, [defaultComponent, options, pick])
}

1 comment on commit 299ac06

@vercel
Copy link

@vercel vercel bot commented on 299ac06 Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.