Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion core/lib/makeswift/makeswift-page-shim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,73 @@
import { Page as MakeswiftPage } from '@makeswift/runtime/next';
import { ComponentPropsWithoutRef } from 'react';

type MetadataValue = string | number | boolean | MetadataObject | MetadataValue[] | null | undefined;
type MetadataObject = { [key: string]: MetadataValue };

/**
* Filters metadata to only include meaningful values while preserving structure.
* - Always disables favicon to prefer the store favicon from /favicon.ico
* - Removes falsy values (empty strings, null, undefined) to use BigCommerce defaults
* - Preserves nested objects and meaningful boolean values
* @param metadata - The metadata object to filter
* @returns The filtered metadata object with meaningful values only
*/
function filterMetadata(metadata: MetadataValue): MetadataObject | false {
if (metadata === false) {
return false;
}

if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
return { favicon: false };
}

const filtered: MetadataObject = { favicon: false };
const entries = Object.entries(metadata);

const processedEntries = entries.reduce<MetadataObject>((acc, [key, value]) => {
// Always disable favicon to use store favicon
if (key === 'favicon') {
return acc;
}

// Skip null/undefined values to use BigCommerce defaults
if (value === null || value === undefined) {
return acc;
}

// Skip empty strings to use BigCommerce defaults
if (typeof value === 'string' && value.trim() === '') {
return acc;
}

// Preserve arrays as-is
if (Array.isArray(value)) {
acc[key] = value;
return acc;
}

// Recursively filter nested objects (but not arrays)
if (typeof value === 'object' && value !== null) {
const filteredNested = filterMetadata(value);

if (filteredNested !== false && Object.keys(filteredNested).length > 1) {
// Only include nested objects if they have more than just the favicon property
acc[key] = filteredNested;
}

return acc;
}

// Include all other meaningful values (non-empty strings, numbers, booleans)
acc[key] = value;
return acc;
}, {});

return { ...filtered, ...processedEntries };
}

export function MakeswiftPageShim(props: ComponentPropsWithoutRef<typeof MakeswiftPage>) {
return <MakeswiftPage {...props} />;
const metadata = filterMetadata(props.metadata);

return <MakeswiftPage {...props} metadata={metadata} />;
}
Loading
Loading