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

Determines whether PUBLIC_URL is same-origin before registering SW. #2432

Merged
merged 1 commit into from
Jun 26, 2017

Conversation

jeffposnick
Copy link
Contributor

R: @Timer @gaearon
FYI: @addyosmani

This is a bit more explicit than trying to use a regular expression. It uses the URL object inside of a block that first checks to see if navigator.serviceWorker is defined; URL should always exist in browsers that support service workers.

From testing locally, I saw scenarios in which process.env.PUBLIC_URL wasn't set at all (which contradicts what @Timer suggested in the issue).

The code in this PR should behave properly regardless of whether it's unset, whether it's set to a local path, or whether it's set to a full URL.

Fixes #2374

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact [email protected] if you have any questions.

@Timer
Copy link
Contributor

Timer commented Jun 2, 2017

From testing locally, I saw scenarios in which process.env.PUBLIC_URL wasn't set at all (which contradicts what @Timer suggested in the issue).

It should always be provided; it just may be blank. This is changing in the near future.

Either way; this code should work.

@Timer
Copy link
Contributor

Timer commented Jun 2, 2017

I'm guessing we will need to add url-polyfill for this?

Would it be better to use Node's url?

url.parse(url.resolve(window.location, process.env.PUBLIC_URL))
> url.parse(url.resolve('https://github.com/facebookincubator/create-react-app/pull/2432', 'http://google.com'))
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'google.com',
  port: null,
  hostname: 'google.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'http://google.com/' }

// --

> url.parse(url.resolve('https://github.com/facebookincubator/create-react-app/pull/2432', '/foo'))
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'github.com',
  port: null,
  hostname: 'github.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/foo',
  path: '/foo',
  href: 'https://github.com/foo' }

@jeffposnick
Copy link
Contributor Author

All browsers that support service workers support the URL object, and the code that uses the URL object is conditionally executed inside of a block that first checks for SW support. So it seemed safe to use the native URL here. I'm happy to throw in a 'URL' in window check if you'd feel better about that.

If we shouldn't use URL for some reason, I'd rather re-implement this using string comparisons than incur the cost of bringing in a polyfill or external library.

@Timer
Copy link
Contributor

Timer commented Jun 2, 2017

All browsers that support service workers support the URL object, and the code that uses the URL object is conditionally executed inside of a block that first checks for SW support.

Oops, I overlooked this. Great!

@gaearon
Copy link
Contributor

gaearon commented Jun 26, 2017

Thanks!

@gaearon gaearon mentioned this pull request Jun 28, 2017
romaindso pushed a commit to romaindso/create-react-app that referenced this pull request Jul 10, 2017
wmonk referenced this pull request in wmonk/create-react-app-typescript Aug 7, 2017
morgs32 pushed a commit to BrickworkSoftware/create-react-app that referenced this pull request Sep 1, 2017
@jeffposnick jeffposnick deleted the check-public-url-origin branch September 18, 2017 15:13
@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants