@teleporthq/teleport-plugin-next-data-source 0.42.4 → 0.42.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -774,12 +774,95 @@ export const createNextArrayMapperPaginationPlugin: ComponentPluginFactory<{}> =
774
774
  )
775
775
  }
776
776
 
777
+ cleanupStaticDataProviders(blockStatement)
778
+
777
779
  return structure
778
780
  }
779
781
 
780
782
  return paginationPlugin
781
783
  }
782
784
 
785
+ function cleanupStaticDataProviders(blockStatement: types.BlockStatement): void {
786
+ const findAllDataProviders = (node: any, results: any[] = []): any[] => {
787
+ if (!node) {
788
+ return results
789
+ }
790
+
791
+ if (node.type === 'JSXElement' && node.openingElement?.name?.name === 'DataProvider') {
792
+ results.push(node)
793
+ }
794
+
795
+ if (node.type === 'ReturnStatement' && node.argument) {
796
+ findAllDataProviders(node.argument, results)
797
+ } else if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
798
+ if (node.children && Array.isArray(node.children)) {
799
+ node.children.forEach((child: any) => findAllDataProviders(child, results))
800
+ }
801
+ } else if (node.type === 'JSXExpressionContainer') {
802
+ if (
803
+ node.expression &&
804
+ (node.expression.type === 'JSXElement' || node.expression.type === 'JSXFragment')
805
+ ) {
806
+ findAllDataProviders(node.expression, results)
807
+ }
808
+ } else if (node.type === 'BlockStatement') {
809
+ if (node.body && Array.isArray(node.body)) {
810
+ node.body.forEach((stmt: any) => findAllDataProviders(stmt, results))
811
+ }
812
+ } else if (node.type === 'ConditionalExpression') {
813
+ findAllDataProviders(node.consequent, results)
814
+ findAllDataProviders(node.alternate, results)
815
+ }
816
+
817
+ return results
818
+ }
819
+
820
+ const allDataProviders = findAllDataProviders(blockStatement)
821
+
822
+ allDataProviders.forEach((dataProvider) => {
823
+ const hasInitialData = dataProvider.openingElement.attributes.some(
824
+ (attr: any) => attr.type === 'JSXAttribute' && attr.name.name === 'initialData'
825
+ )
826
+ const hasFetchData = dataProvider.openingElement.attributes.some(
827
+ (attr: any) => attr.type === 'JSXAttribute' && attr.name.name === 'fetchData'
828
+ )
829
+ const paramsAttr = dataProvider.openingElement.attributes.find(
830
+ (attr: any) => attr.type === 'JSXAttribute' && attr.name.name === 'params'
831
+ )
832
+
833
+ // Case 1: Static SSR/SSG DataProviders (initialData, no fetchData, params)
834
+ // Remove params to prevent refetch attempts - data was already fetched in getStaticProps
835
+ if (hasInitialData && !hasFetchData && paramsAttr) {
836
+ dataProvider.openingElement.attributes = dataProvider.openingElement.attributes.filter(
837
+ (attr: any) => attr.type !== 'JSXAttribute' || attr.name.name !== 'params'
838
+ )
839
+ }
840
+
841
+ // Case 2: Client-side DataProviders with plain object params (fetchData, non-memoized params)
842
+ // Wrap params in useMemo to prevent infinite refetch loops
843
+ else if (hasFetchData && paramsAttr && paramsAttr.value?.type === 'JSXExpressionContainer') {
844
+ const paramsExpression = paramsAttr.value.expression
845
+
846
+ // Check if params are already memoized (useMemo or useCallback call)
847
+ const isAlreadyMemoized =
848
+ paramsExpression.type === 'CallExpression' &&
849
+ paramsExpression.callee.type === 'Identifier' &&
850
+ (paramsExpression.callee.name === 'useMemo' ||
851
+ paramsExpression.callee.name === 'useCallback')
852
+
853
+ // If params are a plain ObjectExpression, wrap in useMemo
854
+ if (!isAlreadyMemoized && paramsExpression.type === 'ObjectExpression') {
855
+ const memoizedParams = types.callExpression(types.identifier('useMemo'), [
856
+ types.arrowFunctionExpression([], paramsExpression),
857
+ types.arrayExpression([]), // Empty deps - params are static
858
+ ])
859
+
860
+ paramsAttr.value.expression = memoizedParams
861
+ }
862
+ }
863
+ })
864
+ }
865
+
783
866
  function findParentNode(root: any, target: any, currentParent: any = null): any | null {
784
867
  if (!root || !target) {
785
868
  return null