BLOG-118 Fix to allow nullable published_time to support unpublished posts #121

Merged
squid merged 2 commits from BLOG-118_fix_error_when_access_unpublished_post into main 2025-08-06 21:36:54 +08:00
Owner

Description

This PR updates the application to handle posts that may not have a publication date (e.g., drafts) by making the published_time field optional across the entire post feature stack.

This ensures that draft posts can be processed and rendered without causing errors, and prevents search engine metadata from being generated for content that is not yet published.

Key Changes:

  • DTO & Schema (postInfoResponseDto.ts):

    • The Zod schema for PostInfoResponseSchema has been updated to mark published_time as .nullable().
    • The PostInfoResponseDto class now correctly handles a null value from the API, mapping it to Date | null.
  • Domain Entity (postInfo.ts):

    • The core PostInfo entity's publishedTime property is now typed as Date | null to reflect the business logic that a post may be unpublished.
  • View Model (postInfoViewModel.ts):

    • Updated publishedTime to be Date | null.
    • Added a new isPublished boolean getter for convenient conditional logic in the UI.
    • The formattedPublishedTime getter now returns string | null.
    • Dehydration and rehydration logic (dehydrate/rehydrate) has been updated to correctly handle the nullable publishedTime.
  • UI Component (PostContentPage.svelte):

    • The component now uses the new isPublished flag to conditionally render the <StructuredData> component for SEO. This ensures that structured data is only included for posts that have been officially published.

Package Changes

No response

Screenshots

No response

Reference

Resolves #118

Checklist

  • A milestone is set
  • The related issuse has been linked to this branch
### Description This PR updates the application to handle posts that may not have a publication date (e.g., drafts) by making the `published_time` field optional across the entire post feature stack. This ensures that draft posts can be processed and rendered without causing errors, and prevents search engine metadata from being generated for content that is not yet published. #### Key Changes: * **DTO & Schema (`postInfoResponseDto.ts`):** * The Zod schema for `PostInfoResponseSchema` has been updated to mark `published_time` as `.nullable()`. * The `PostInfoResponseDto` class now correctly handles a `null` value from the API, mapping it to `Date | null`. * **Domain Entity (`postInfo.ts`):** * The core `PostInfo` entity's `publishedTime` property is now typed as `Date | null` to reflect the business logic that a post may be unpublished. * **View Model (`postInfoViewModel.ts`):** * Updated `publishedTime` to be `Date | null`. * Added a new `isPublished` boolean getter for convenient conditional logic in the UI. * The `formattedPublishedTime` getter now returns `string | null`. * Dehydration and rehydration logic (`dehydrate`/`rehydrate`) has been updated to correctly handle the nullable `publishedTime`. * **UI Component (`PostContentPage.svelte`):** * The component now uses the new `isPublished` flag to conditionally render the `<StructuredData>` component for SEO. This ensures that structured data is only included for posts that have been officially published. ### Package Changes _No response_ ### Screenshots _No response_ ### Reference Resolves #118 ### Checklist - [x] A milestone is set - [x] The related issuse has been linked to this branch
squid added this to the 0.3 milestone 2025-08-06 21:34:33 +08:00
squid added 1 commit 2025-08-06 21:34:33 +08:00
BLOG-118 fix: make post info published time be able to receive null
Some checks failed
Frontend CI / build (push) Failing after 59s
Auto Comment On PR / add_improve_comment (pull_request) Successful in 14s
PR Title Check / pr-title-check (pull_request) Successful in 17s
8440d9068d
Collaborator

/improve

/improve
Collaborator

PR Code Suggestions

CategorySuggestion                                                                                                                                    Impact
Possible issue
Add null check for date property

The datePublished prop of the StructuredData component likely expects a Date object,
but state.data.info.publishedTime is typed as Date | null. Although there's an
isPublished check, it's safer to explicitly verify publishedTime is not null before
passing it. This prevents potential runtime errors if isPublished is true but
publishedTime is unexpectedly null.

frontend/src/lib/post/framework/ui/PostContentPage.svelte [25-32]

-{#if state.data.info.isPublished}
+{#if state.data.info.isPublished && state.data.info.publishedTime}
 	<StructuredData
 		headline={state.data.info.title}
 		description={state.data.info.description}
-		datePublished={state.data.info.publishedTime!}
+		datePublished={state.data.info.publishedTime}
 		image={state.data.info.previewImageUrl}
 	/>
 {/if}
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly identifies that state.data.info.publishedTime can be Date | null. Adding an explicit null check (&& state.data.info.publishedTime) before passing it to StructuredData prevents potential runtime errors, even with the isPublished flag, and correctly removes the non-null assertion operator (!).

Medium
## PR Code Suggestions ✨ <!-- --> <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>Possible issue</td> <td> <details><summary>Add null check for date property</summary> ___ **The <code>datePublished</code> prop of the <code>StructuredData</code> component likely expects a <code>Date</code> object, <br>but <code>state.data.info.publishedTime</code> is typed as <code>Date | null</code>. Although there's an <br><code>isPublished</code> check, it's safer to explicitly verify <code>publishedTime</code> is not null before <br>passing it. This prevents potential runtime errors if <code>isPublished</code> is true but <br><code>publishedTime</code> is unexpectedly <code>null</code>.** [frontend/src/lib/post/framework/ui/PostContentPage.svelte [25-32]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-118_fix_error_when_access_unpublished_post/frontend/src/lib/post/framework/ui/PostContentPage.svelte#L25-L32) ```diff -{#if state.data.info.isPublished} +{#if state.data.info.isPublished && state.data.info.publishedTime} <StructuredData headline={state.data.info.title} description={state.data.info.description} - datePublished={state.data.info.publishedTime!} + datePublished={state.data.info.publishedTime} image={state.data.info.previewImageUrl} /> {/if} ``` <details><summary>Suggestion importance[1-10]: 8</summary> __ Why: The suggestion correctly identifies that `state.data.info.publishedTime` can be `Date | null`. Adding an explicit null check (`&& state.data.info.publishedTime`) before passing it to `StructuredData` prevents potential runtime errors, even with the `isPublished` flag, and correctly removes the non-null assertion operator (`!`). </details></details></td><td align=center>Medium </td></tr></tr></tbody></table>
squid added 1 commit 2025-08-06 21:35:14 +08:00
BLOG-118 refactor: run format
All checks were successful
PR Title Check / pr-title-check (pull_request) Successful in 16s
Frontend CI / build (push) Successful in 1m12s
840302174d
Author
Owner

The datePublished prop of the StructuredData component likely expects a Date object,
but state.data.info.publishedTime is typed as Date | null. Although there's an
isPublished check, it's safer to explicitly verify publishedTime is not null before
passing it. This prevents potential runtime errors if isPublished is true but
publishedTime is unexpectedly null.

frontend/src/lib/post/framework/ui/PostContentPage.svelte [25-32]

-{#if state.data.info.isPublished}
+{#if state.data.info.isPublished && state.data.info.publishedTime}
 	<StructuredData
 		headline={state.data.info.title}
 		description={state.data.info.description}
-		datePublished={state.data.info.publishedTime!}
+		datePublished={state.data.info.publishedTime}
 		image={state.data.info.previewImageUrl}
 	/>
 {/if}

Ignore.

> **The <code>datePublished</code> prop of the <code>StructuredData</code> component likely expects a <code>Date</code> object, <br>but <code>state.data.info.publishedTime</code> is typed as <code>Date | null</code>. Although there's an <br><code>isPublished</code> check, it's safer to explicitly verify <code>publishedTime</code> is not null before <br>passing it. This prevents potential runtime errors if <code>isPublished</code> is true but <br><code>publishedTime</code> is unexpectedly <code>null</code>.** > > [frontend/src/lib/post/framework/ui/PostContentPage.svelte [25-32]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-118_fix_error_when_access_unpublished_post/frontend/src/lib/post/framework/ui/PostContentPage.svelte#L25-L32) > > ```diff > -{#if state.data.info.isPublished} > +{#if state.data.info.isPublished && state.data.info.publishedTime} > <StructuredData > headline={state.data.info.title} > description={state.data.info.description} > - datePublished={state.data.info.publishedTime!} > + datePublished={state.data.info.publishedTime} > image={state.data.info.previewImageUrl} > /> > {/if} > ``` Ignore.
squid merged commit 08c5262df6 into main 2025-08-06 21:36:54 +08:00
squid deleted branch BLOG-118_fix_error_when_access_unpublished_post 2025-08-06 21:36:54 +08:00
Sign in to join this conversation.
No description provided.