Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - clean stega from salaries data string #673

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
52 changes: 28 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,41 +147,45 @@ When building custom components in Sanity Studio that need to fetch data, it's i

**Implementation Example:**

```typescript
```tsx
import React, { useEffect, useState } from "react";
import { fetchWithToken } from "studio/lib/fetchWithToken";

const MyCustomComponent: React.FC = () => {
const [data, setData] = useState<any>(null);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchData = async () => {
try {
const query = '*[_type == "myType"]'; // Replace with your GROQ query
const result = await fetchWithToken<any>(query);
setData(result);
} catch (err) {
console.error("Error fetching data:", err);
setError(err.message);
}
};

fetchData();
}, []);

if (error) {
return <div>Error: {error}</div>;
}

return <div>{JSON.stringify(data)}</div>; // Render your component's UI
const [data, setData] = useState<any>(null);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchData = async () => {
try {
const query = '*[_type == "myType"]'; // Replace with your GROQ query
const result = await fetchWithToken<any>(query);
setData(result);
} catch (err) {
console.error("Error fetching data:", err);
setError(err.message);
}
};

fetchData();
}, []);

if (error) {
return <div>Error: {error}</div>;
}

return <div>{JSON.stringify(data)}</div>; // Render your component's UI
};

export default MyCustomComponent;
```

By using fetchWithToken, you ensure that all data fetching happens securely, with the server-side API route handling the sensitive token.

### Steganography in Presentation

To enable preview functionality in the Presentation view, Sanity applies [steganography](https://www.sanity.io/docs/stega) to the string data. This manipulates the data to include invisible HTML entities to store various metadata. If the strings are used in business logic, that logic will likely break in the Presentation view. To fix this, Sanity provides the `stegaClean` utility to remove this extra metadata. An example of this in action can be found in [CompensationsPreview.tsx](src/compensations/CompensationsPreview.tsx), where JSON parsing of salary data fails without stega cleaning.

### OpenGraph image customization

As part of providing the basic metadata for the [OpenGraph Protocol](https://ogp.me), a fallback image is generated if no other is specified. Fonts and background can be customized as shown below.
Expand Down
12 changes: 10 additions & 2 deletions src/compensations/CompensationsPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";
import { stegaClean } from "@sanity/client/stega";
import { QueryResponseInitial, useQuery } from "@sanity/react-loader";
import { Suspense } from "react";

Expand All @@ -18,7 +19,7 @@ const CompensationsPreview = ({
initialCompensations,
initialLocations,
}: CompensationsPreviewProps) => {
const { data } = useQuery<CompensationsPage>(
const { data: compensationsData } = useQuery<CompensationsPage>(
COMPENSATIONS_PAGE_QUERY,
{ slug: initialCompensations.data.slug.current },
{ initial: initialCompensations },
Expand All @@ -29,10 +30,17 @@ const CompensationsPreview = ({
{ initial: initialLocations },
);

compensationsData.salariesByLocation = stegaClean(
compensationsData.salariesByLocation,
);

return (
locationData && (
<Suspense>
<Compensations compensations={data} locations={locationData} />
<Compensations
compensations={compensationsData}
locations={locationData}
/>
</Suspense>
)
);
Expand Down