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

fix: Fixes an invalid link for security schemes when using the Astro base option #55

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

taiseimiyaji
Copy link
Contributor

@taiseimiyaji taiseimiyaji commented Nov 26, 2024

Closes #54

Thank you for maintaining and contributing to this amazing repository. It has been incredibly helpful for our project, and I truly appreciate the effort that goes into its development.

Please note that English is not my first language, so I apologize if any part of this pull request or issue report seems unnatural or unclear. I hope the provided details are sufficient, and I’m happy to clarify or adjust anything if needed.

Describe the pull request

This pull request fixes an issue where the base configuration in Astro was not applied to the security component. Specifically, links generated by the security component did not include the base prefix, leading to incorrect or broken links.

The changes ensure that the base configuration is consistently applied to all generated links.

Why

These changes are necessary to ensure that:

  1. The base configuration in Astro is respected across all components, including security.
  2. Links generated by the security component work correctly, even when the base configuration is specified.
  3. The application behaves as expected in environments where the base path is configured (e.g., hosted in subdirectories).
    Without this fix, links generated by the security component fail in environments where a base configuration is required.

How

The changes were implemented as follows:

  1. Introduced logic to handle import.meta.env.BASE_URL, trimming any trailing slash to avoid double slashes when concatenating paths.
  2. Updated the href attribute in the security component to prepend BASE_URL to the existing link path.
  3. Ensured the changes are backward-compatible by handling cases where BASE_URL is undefined or empty.
    The key code modification:
const baseURL = (import.meta.env.BASE_URL || '').replace(/\/$/, ''); // Trim trailing slash or fallback to empty
Updated the link generation logic:
<a href={`${baseURL}${getBaseLink(schema.config)}#${slug(scheme)}`}>{scheme}</a>

This ensures that links are always prefixed correctly, regardless of the presence or value of BASE_URL.

issue: #54

Copy link

vercel bot commented Nov 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
starlight-openapi ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 27, 2024 10:05am

Copy link
Owner

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution 🙌

It has been incredibly helpful for our project, and I truly appreciate the effort that goes into its development.

Thanks a lot, happy to hear that!

Please note that English is not my first language, so I apologize if any part of this pull request or issue report seems unnatural or unclear. I hope the provided details are sufficient, and I’m happy to clarify or adjust anything if needed.

No need to apologize, your English is great and the details you provided are very clear. Thanks for the PR.

I took the liberty to move out the logic in a reusable function so we can in the future use it in other places as well as this is a common pattern in Astro integrations/Starlight plugins and we can release your fix as soon as possible.

Amazing work 🎉

@HiDeoo HiDeoo changed the title Fix: Fixed base settings are applied to Security.astro component fix: Fixes an invalid link for security schemes when using the Astro base option Nov 27, 2024
@HiDeoo HiDeoo merged commit 87fb6c8 into HiDeoo:main Nov 27, 2024
3 checks passed
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.

The base configuration in Astro is not applied to the security component.
2 participants