@teleporthq/teleport-plugin-common 0.43.41 → 0.43.44

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.
Files changed (40) hide show
  1. package/__tests__/node-handlers/node-to-jsx/utils.ts +55 -0
  2. package/__tests__/utils/url-search-param-sync.ts +42 -0
  3. package/dist/cjs/node-handlers/node-to-html/utils.js +24 -11
  4. package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
  5. package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  6. package/dist/cjs/node-handlers/node-to-jsx/index.js +117 -4
  7. package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
  8. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  9. package/dist/cjs/node-handlers/node-to-jsx/utils.js +41 -22
  10. package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
  11. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  12. package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
  13. package/dist/cjs/utils/ast-utils.js +36 -0
  14. package/dist/cjs/utils/ast-utils.js.map +1 -1
  15. package/dist/cjs/utils/url-search-param-sync.d.ts +2 -2
  16. package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -1
  17. package/dist/cjs/utils/url-search-param-sync.js +38 -6
  18. package/dist/cjs/utils/url-search-param-sync.js.map +1 -1
  19. package/dist/esm/node-handlers/node-to-html/utils.js +24 -11
  20. package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
  21. package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  22. package/dist/esm/node-handlers/node-to-jsx/index.js +115 -5
  23. package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
  24. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  25. package/dist/esm/node-handlers/node-to-jsx/utils.js +41 -22
  26. package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
  27. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  28. package/dist/esm/utils/ast-utils.d.ts.map +1 -1
  29. package/dist/esm/utils/ast-utils.js +36 -0
  30. package/dist/esm/utils/ast-utils.js.map +1 -1
  31. package/dist/esm/utils/url-search-param-sync.d.ts +2 -2
  32. package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -1
  33. package/dist/esm/utils/url-search-param-sync.js +38 -6
  34. package/dist/esm/utils/url-search-param-sync.js.map +1 -1
  35. package/package.json +4 -4
  36. package/src/node-handlers/node-to-html/utils.ts +33 -11
  37. package/src/node-handlers/node-to-jsx/index.ts +190 -22
  38. package/src/node-handlers/node-to-jsx/utils.ts +63 -30
  39. package/src/utils/ast-utils.ts +39 -0
  40. package/src/utils/url-search-param-sync.ts +50 -10
@@ -68,6 +68,17 @@ const clone = (expr: types.Expression): types.Expression =>
68
68
  // input). `depExpr` is the effect dependency that re-triggers the write-back
69
69
  // when that value changes — usually the same expression as `valueExpr`.
70
70
  //
71
+ // `defaultValueExpr` (optional) is the state's default value. When supplied
72
+ // (only for bindings whose default is a non-empty value, e.g. the sort
73
+ // dropdown's `name-asc`), the current value being EQUAL TO THE DEFAULT also
74
+ // deletes the key — so the canonical/default view keeps a clean, param-free
75
+ // URL instead of stickily writing `?sortBy=name-asc` on first load. This is
76
+ // the write-back half of the default-aware pairing that keeps the URL⇄state
77
+ // sync loop-free: the read-back below resolves a missing key back to the same
78
+ // default, so the two never disagree. Omitting it (empty-default bindings like
79
+ // `selectedCategory` / the search input) yields byte-identical output to the
80
+ // pre-default builder.
81
+ //
71
82
  // Notes:
72
83
  // 1. `router.isReady` gate: on the very first SSG render `router.query` is
73
84
  // empty, so without this gate any value initialized from
@@ -87,8 +98,23 @@ const clone = (expr: types.Expression): types.Expression =>
87
98
  export const buildUrlWriteBackEffect = (
88
99
  paramKey: string,
89
100
  valueExpr: types.Expression,
90
- depExpr: types.Expression
101
+ depExpr: types.Expression,
102
+ defaultValueExpr?: types.Expression
91
103
  ): types.ExpressionStatement => {
104
+ // value === '' || value == null [|| value === <default>]
105
+ let deleteCondition: types.Expression = types.logicalExpression(
106
+ '||',
107
+ types.binaryExpression('===', clone(valueExpr), types.stringLiteral('')),
108
+ types.binaryExpression('==', clone(valueExpr), types.nullLiteral())
109
+ )
110
+ if (defaultValueExpr) {
111
+ deleteCondition = types.logicalExpression(
112
+ '||',
113
+ deleteCondition,
114
+ types.binaryExpression('===', clone(valueExpr), clone(defaultValueExpr))
115
+ )
116
+ }
117
+
92
118
  const effectBody = types.blockStatement([
93
119
  // if (typeof window === 'undefined') return
94
120
  types.ifStatement(
@@ -118,14 +144,10 @@ export const buildUrlWriteBackEffect = (
118
144
  ])
119
145
  ),
120
146
  ]),
121
- // if (value === '' || value == null) { delete __nextQuery.key }
147
+ // if (value === '' || value == null [|| value === <default>]) { delete __nextQuery.key }
122
148
  // else { __nextQuery.key = String(value) }
123
149
  types.ifStatement(
124
- types.logicalExpression(
125
- '||',
126
- types.binaryExpression('===', clone(valueExpr), types.stringLiteral('')),
127
- types.binaryExpression('==', clone(valueExpr), types.nullLiteral())
128
- ),
150
+ deleteCondition,
129
151
  types.blockStatement([
130
152
  types.expressionStatement(
131
153
  types.unaryExpression(
@@ -211,9 +233,20 @@ export const buildUrlWriteBackEffect = (
211
233
  // (duplicate keys) per Next.js's typings; the helper normalizes both to a
212
234
  // string before comparing, defaulting to `''` for the missing-key case so the
213
235
  // comparison aligns with the write-back's `value === ''` empty-check.
236
+ //
237
+ // `defaultValueExpr` (optional) is the state's default value. When supplied
238
+ // (bindings with a non-empty default such as the sort dropdown's `name-asc`),
239
+ // an ABSENT or EMPTY URL value resolves to the default rather than `''` — via a
240
+ // trailing `(<normalized>) || <default>`. This is what makes the default sort
241
+ // "stick" on first load: without it the read-back would clobber the default
242
+ // state (`name-asc`) with `''` the instant the router hydrates with no `?sortBy`
243
+ // key, forcing an extra unsorted fetch and losing the ordering. It also keeps
244
+ // browser back/forward to the param-free URL restoring the default view.
245
+ // Omitting it reproduces the pre-default `''` fallback byte-for-byte.
214
246
  export const buildUrlReadBackEffect = (
215
247
  paramKey: string,
216
- setterName: string
248
+ setterName: string,
249
+ defaultValueExpr?: types.Expression
217
250
  ): types.ExpressionStatement => {
218
251
  const normalizeArg = types.conditionalExpression(
219
252
  // typeof __urlValue === 'string' ? __urlValue
@@ -238,6 +271,13 @@ export const buildUrlReadBackEffect = (
238
271
  )
239
272
  )
240
273
 
274
+ // With a default, an absent/empty normalized value (falsy `''`) falls through
275
+ // to the default: `(<normalized>) || <default>`. Without one, the bare
276
+ // normalized expression is emitted unchanged.
277
+ const nextValueExpr: types.Expression = defaultValueExpr
278
+ ? types.logicalExpression('||', normalizeArg, clone(defaultValueExpr))
279
+ : normalizeArg
280
+
241
281
  const effectBody = types.blockStatement([
242
282
  // if (!router.isReady) return
243
283
  types.ifStatement(
@@ -251,9 +291,9 @@ export const buildUrlReadBackEffect = (
251
291
  types.variableDeclaration('const', [
252
292
  types.variableDeclarator(types.identifier('__urlValue'), routerQueryAccess(paramKey)),
253
293
  ]),
254
- // const __nextValue = (normalized)
294
+ // const __nextValue = (normalized) [|| <default>]
255
295
  types.variableDeclaration('const', [
256
- types.variableDeclarator(types.identifier('__nextValue'), normalizeArg),
296
+ types.variableDeclarator(types.identifier('__nextValue'), nextValueExpr),
257
297
  ]),
258
298
  // setState((prev) => prev === __nextValue ? prev : __nextValue)
259
299
  types.expressionStatement(