Skip to content

Fix getmetadata invalid site url #1362

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

Merged
merged 5 commits into from
May 30, 2025
Merged

Conversation

adriangohjw
Copy link
Member

Problem

We had an issue last week where url doesn't have https:// and thus calling site build to fail with an unknown error

Solution

Breaking Changes

  • Yes - this PR contains breaking changes
    • Details ...
  • No - this PR is backwards compatible

Bug Fixes:

  • wrap in try/catch for better error handling
  • add checks for http:// or https:// and auto append if missing

@adriangohjw adriangohjw self-assigned this May 26, 2025
@adriangohjw adriangohjw requested a review from a team as a code owner May 26, 2025 07:36
@adriangohjw adriangohjw added the bug Something isn't working label May 26, 2025
@datadog-opengovsg
Copy link

datadog-opengovsg bot commented May 26, 2025

Datadog Report

Branch report: fix-getmetadata-invalid-site-url
Commit report: b1aba41
Test service: isomer-studio

✅ 0 Failed, 564 Passed, 42 Skipped, 1m 26.42s Total Time
⬆️ Test Sessions change in coverage: 1 increased (+0.09%)

Copy link
Contributor

@seaerchin seaerchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just throw a better error instead during URL construction?

Comment on lines 63 to 75
const canonicalUrl = (() => {
if (!props.site.url) return props.page.permalink
try {
const siteUrl =
props.site.url.startsWith("http://") ||
props.site.url.startsWith("https://")
? props.site.url
: `https://${props.site.url}`
return new URL(props.page.permalink, siteUrl).toString()
} catch {
return props.page.permalink
}
})()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i disagree with this approach; i'd rather we just throw an error here so that we can guarantee that the site.url in our db is a well-formed url.

if we go with this approach, we should also disallow http://

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @seaerchin has a good point, we should rather fix and be opinionated about the expected input as the schema is the contract to be followed/adhered to. When it does not match the expected input, it should error out

Else, we will end up with schemas of site that vary between each other.

As per URL standards it should be https:// or http://. But for Isomer, we will strictly disallow http, so it should throw as well for http

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay will update!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@harishv7 harishv7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left minor comments

@@ -58,11 +58,25 @@ const getMetaImage = (props: IsomerPageSchemaType) => {
}
}

const getCanonicalUrl = (props: IsomerPageSchemaType) => {
if (!props.site.url) return props.page.permalink
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that either site.url or page.permalink will always be present

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

page.permalink is stored in the page as part of the JSON for every blob. It should unless someone intentionally remove it directly (which should not happen)

const getCanonicalUrl = (props: IsomerPageSchemaType) => {
if (!props.site.url) return props.page.permalink

if (!props.site.url.startsWith("https://")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think can add a comment explaining the decision made that we want to explicitly throw errors to standardise the schema for site config urls

@adriangohjw adriangohjw enabled auto-merge (squash) May 30, 2025 09:14
@adriangohjw adriangohjw merged commit 9ad5eab into main May 30, 2025
18 checks passed
@adriangohjw adriangohjw deleted the fix-getmetadata-invalid-site-url branch May 30, 2025 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants