Skip to content

Fallback key can be used even if fallback JSON is invalid #22

@marklearst

Description

@marklearst

Problem:
hasFallback is derived from fallbackFile || parsedFallbackFile. If fallbackFile is a string but invalid JSON, parsedFallbackFile becomes undefined, but hasFallback still becomes true. That forces the fallback SWR key even though fallback data can't be parsed, and the fetcher will then try a "fallback" key and "fallback" token, causing a failure.

References:

  • src/contexts/FigmaVarsProvider.tsx
  • src/hooks/useVariables. ts
  • src/hooks/usePublishedVariables.ts
  • src/utils/swrKeys.ts

Citations:

  • src/contexts/FigmaVarsProvider.tsx
  • src/hooks/useVariables.ts
  • src/hooks/usePublishedVariables.ts
  • src/utils/swrKeys.ts

Code Snippets

src/hooks/useVariables.ts:

const hasFallback = Boolean(fallbackFile || parsedFallbackFile)
const key = getVariablesKey({
  fileKey,
  token,
  providerId,
  hasFallback,
})

src/hooks/usePublishedVariables.ts:

const hasFallback = Boolean(fallbackFile || parsedFallbackFile)
const key = getPublishedVariablesKey({
  fileKey,
  token,
  providerId,
  hasFallback,
})

src/contexts/FigmaVarsProvider.tsx:

const parsedFallbackFile = useMemo(() => {
  if (!fallbackFile) return undefined
  // If already parsed (object), validate...
  if (typeof fallbackFile === 'object') { ... }
  // If string, parse JSON
  // ...
}, [fallbackFile])

src/utils/swrKeys.ts:

export function getVariablesKey(params: VariablesKeyParams): readonly [string, string] | null {
  const { fileKey, token, providerId, hasFallback } = params
  if (hasFallback) {
    return [`fallback-${providerId ?? 'default'}`, 'fallback'] as const
  }
  if (token && fileKey) {
    // ...
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions