Skip to content

refactor(service-worker): improve localhost detection in scheduleInit… #62861

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SkyZeroZx
Copy link
Contributor

Replace simple string search with comprehensive regex pattern that detects:

  • localhost hostname
  • IPv6 loopback address [::1]
  • IPv4 loopback range 127.x.x.x

Extract logic into dedicated isLocalhost() method for better code organization
and remove existing TODO comment.

@pullapprove pullapprove bot requested a review from crisbeto July 29, 2025 04:13
@angular-robot angular-robot bot added the area: service-worker Issues related to the @angular/service-worker package label Jul 29, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 29, 2025
@SkyZeroZx SkyZeroZx force-pushed the service-worker-detect-localhost branch 4 times, most recently from 0c85c8c to 0c2409d Compare July 29, 2025 04:26
Comment on lines 942 to 958
private isLocalhost(): boolean {
return /(localhost|\[::1\]|127(?:\.\d{1,3}){3})(:\d+)?\/?$/.test(this.scope.registration.scope);
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest pulling this out into a top-level function that takes the scope as parameter, such that you can add tests for this logic.

Also the regex can be adjusted to not have any capturing groups:

Suggested change
private isLocalhost(): boolean {
return /(localhost|\[::1\]|127(?:\.\d{1,3}){3})(:\d+)?\/?$/.test(this.scope.registration.scope);
}
private isLocalhost(): boolean {
return /(?:localhost|\[::1\]|127(?:\.\d{1,3}){3})(?::\d+)?\/?$/.test(this.scope.registration.scope);
}

Q should the localhost case be prefixed with something like \W to require a non-word in front of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, I just updated with some improvements and tests.

@SkyZeroZx SkyZeroZx force-pushed the service-worker-detect-localhost branch 3 times, most recently from 2d7c4c6 to 26c75b0 Compare July 30, 2025 01:46
…ialization

Replaces ad hoc localhost check with a dedicated method using a regular expression for more reliable detection
@SkyZeroZx SkyZeroZx force-pushed the service-worker-detect-localhost branch from 26c75b0 to e52f071 Compare July 30, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: service-worker Issues related to the @angular/service-worker package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants