Skip to content

Conversation

@marinaaisa
Copy link
Member

Bug/issue #165347857, if applicable:

Summary

We want to normalize Sample Code URLs, by adding the baseUrl to them if the path is relative and the baseUrl is provided. If the path is absolute or the baseUrl is not provided, it does not prefixes the URL.

Because the component CallToActionButton.vue is not only being used for Sample Code links, we created a new prop called linksToAsset that will normalize the relative URLs, in the cases that they are assets, like Sample Code.

Dependencies

NA

Testing

Steps:

  1. Provide a baseUrl
  2. Go to a page for Sample Codes that points to a relative path
  3. Assert that the link gets prefixed with the baseUrl

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran npm test, and it succeeded
  • Updated documentation if necessary

@marinaaisa marinaaisa requested a review from mportiz08 November 24, 2025 17:09
},
methods: {
normalizeUrl(url) {
if (!this.linksToAsset || isAbsoluteUrl(url)) return url;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we even need to check for special flags or absolute URLs as a special case here? I think the logic should pretty much match up with the logic in components for image and video assets, which always "normalize" the URLs. I think the normalizePath function should already be a noop for absolute URLs, and I don't think there's any special logic for assets vs documents with regards to a custom base path.

Copy link
Member Author

Choose a reason for hiding this comment

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

For the normalizePath function to work, the path needs to be relative. This means that we need to normalize the relative path first, to make sure that the normalizePath function reads it as a relative path.

An example is the simpleRelativePath const that I wrote in the tests, which value is foo/bar:
foo/bar needs to be normalize as a relative path to become /foo/bar instead.
Otherwise normalizePath will think that foo/bar is an absolute URL and it won't prefix the baseUrl.

The reason why I'm exiting early in the case of being a real absolute URL (by using the isAbsoluteUrl function) is that, if we pass an absolute URL to normalizeRelativePath, it will turn it into a relative path like /website.com/samplecode and then normalizePath will prefix that path as baseUrl/website.com/samplecode. We want to prevent this scenario.

Copy link
Member Author

Choose a reason for hiding this comment

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

An alternative to this is to use the isAbsoluteUrl function within the normalizeRelativePath, so we will never accidentally turn an absolute URL into a relative path.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason why I'm exiting early in the case of being a real absolute URL (by using the isAbsoluteUrl function) is that, if we pass an absolute URL to normalizeRelativePath, it will turn it into a relative path like /website.com/samplecode and then normalizePath will prefix that path as baseUrl/website.com/samplecode. We want to prevent this scenario.

I don't think that's an issue for the existing normalizePath function or we would have hit similar issues with external image/video assets where we already call this function on all URLs.

Here's an example unit test showing the expected behavior for this function when called with an absolute URL:

it('does not change, if passed a url', () => {

Copy link
Member Author

Choose a reason for hiding this comment

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

The test is showing the expected behaviour for absolute URLs (like website.com/samplecode), but by normalising the relative path it adds a slash in the URL (like /website.com/samplecode) and we want to prevent this.

This works for images and videos, because we expect the relative path to always be correct. So we don't normalise the relative path.

Copy link
Contributor

Choose a reason for hiding this comment

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

The test is showing the expected behaviour for absolute URLs (like website.com/samplecode), but by normalising the relative path it adds a slash in the URL (like /website.com/samplecode) and we want to prevent this.

An absolute URL should have a scheme like https://website.com/samplecode.

This works for images and videos, because we expect the relative path to always be correct. So we don't normalise the relative path.

We do already normalize relative paths for images/videos in that function by prepending the base URL (example). Absolute URLs with a scheme would be unaffected by this normalizePath function as shown in the above linked unit test.

I think the confusion is that we also have a different normalizeRelativePath function (seen below in those tests) which normalizes relative paths to all be root relative (some/path => /some/path).

We can utilize both here as you've done, although it's slightly unfortunate if we're getting different styles of relative paths for sample code when compared to images/videos in the JSON.

It prefixes the URL with the baseUrl value if the path is relative and
the baseUrl is provided. If the path is absolute or the baseUrl is not
provided, it does not prefixes the URL.
We only want to normalize the URL in the case that it links to an asset,
that's why in this commit we introduce a new boolean prop for the
CallToActionButton called `linksToAsset`.
Now that we have the prop linksToAsset available for
CallToActionButton components, we want to pass it as true for the
download button used for Sample Codes, since the URL links to an asset.
We want to use this function to check if urls are absolute or not.
For the normalizePath function to work, the path needs to be relative.
This means that we need to normalize the relative path first, to make
sure that the normalizePath function reads it as a relative path.

For this, we should use the function isAbsoluteUrl to see if the url is
absolute or not.
We want to make sure that simple-relative paths like 'foo/bar' also work
when prefixing the baseUrl.
@marinaaisa marinaaisa force-pushed the normalize-sample-code-url branch from e3ae6c5 to f910276 Compare December 10, 2025 13:29
@marinaaisa
Copy link
Member Author

@swift-ci test

@marinaaisa marinaaisa merged commit 4711908 into swiftlang:main Dec 10, 2025
1 check passed
@marinaaisa marinaaisa deleted the normalize-sample-code-url branch December 10, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants