Dev Helpers
← Back

Apply fallback tags only to blank records

Apply fallback tags only to blank records. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/apply-fallback-tags-only-to-blank-records/page.tsx
export const metadata = { title: "Apply fallback tags only to blank records" };
export default function Page() {
  const rows = [
    { title: "Tide", tags: ["sea", "blue"] },
    { title: "Dune", tags: ["sand", "gold"] },
    { title: "Reef", tags: ["sea", "gold"] },
    { title: "Blank", tags: [] },
  ];
  const result = rows.map((r) => ({
    ...r,
    tags: r.tags.length ? r.tags : ["uncategorized"],
  }));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Apply fallback tags only to blank records"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}