Skip to content

Commit

Permalink
fix(blog-post): inject timestamp in the URL to cache burst the image …
Browse files Browse the repository at this point in the history
…optimization when the page is statically generated
  • Loading branch information
mrlemoos committed Mar 25, 2024
1 parent 60af435 commit fbc18af
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/domains/blog-post/read-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type RenderNode,
documentToReactComponents,
} from '@contentful/rich-text-react-renderer';
import { BLOCKS } from '@contentful/rich-text-types';

import Badge from '~/components/badge/badge';
import Image from '~/components/image/image';
Expand Down Expand Up @@ -32,7 +33,7 @@ const INSTAPAPER_URL = 'https://instapaper.com' as const;
* contains the render functions for each type of node.
*/
const renderNode: RenderNode = {
'embedded-asset-block': ({
[BLOCKS.EMBEDDED_ASSET]: ({
data: {
target: {
fields: {
Expand All @@ -42,10 +43,20 @@ const renderNode: RenderNode = {
},
},
}) => {
const src = `https://${url}`;
const timestamp = Date.now();
const src = `https:${url}?timestamp=${timestamp}`;

return (
<Image
// NOTE: The "?timestamp=${timestamp}" above is the only workaround to
// fix the issue with the `next/image` component not rendering the
// assets from Contentful. This is a known issue with the
// `next/image` component and a few remote Content Delivery
// Networks (CDNs) like Contentful's. It works on the local
// environment, however, when we deploy the changes to the Vercel
// cloud, the error occurs. The following discussion on GitHub
// explains the problem and the workaround:
// https://github.com/vercel/next.js/discussions/20138
src={src}
alt={title}
width={600}
Expand Down

0 comments on commit fbc18af

Please sign in to comment.