-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: prevent error on not set location href #12494
Conversation
Run & review this pull request in StackBlitz Codeflow. |
@@ -1545,7 +1545,7 @@ function cleanScssBugUrl(url: string) { | |||
typeof window !== 'undefined' && | |||
typeof location !== 'undefined' | |||
) { | |||
const prefix = location.href.replace(/\/$/, '') | |||
const prefix = location.href?.replace(/\/$/, '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could replace 1546 with typeof location?.href === 'string'
. If href
isn't defined, then we don't need to call replace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mschakulat you haven't fixed this part yet, which I agree would be a nicer check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bluwy Yes, that's correct. I have adjusted it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately condition must be typeof location !== 'undefined'
.
In my project with svelte-kit and scss doesn't work new condition, because location is not defined.
Optional chaining doesn't work on not defined variables.
Would you give more info about your environment that is triggering this bug? Interesting that we didn't get a report before. |
Unfortunately i can't say too much about it. We use a proprietary package (https://bitmovin.com/). The code is encrypted and therefore the context is hard to see. But I see that the 'location' object is edited there: |
be87566
to
e2ab663
Compare
Description
The current implementation of the function
cleanScssBugUrl
checks if the 'window' and 'location' globals are defined, and then replaces the prefix of the URL with the location href. However, in some cases, the 'location.href' value can be undefined, resulting in an error.To resolve this issue, I've updated the code to use optional chaining (?.) to ensure that the 'replace' function only runs if 'location.href' is defined. This modification ensures that the function can handle cases where 'location.href' is undefined and prevents errors from being thrown.
This change has been tested and verified to work as expected.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).