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

Behavior of calling global toUrl method of global require #1696

Closed
jslegers opened this issue Oct 27, 2017 · 2 comments
Closed

Behavior of calling global toUrl method of global require #1696

jslegers opened this issue Oct 27, 2017 · 2 comments

Comments

@jslegers
Copy link

I'm using the following code to create relative URLs :

define([], function() {

  var root;

  function toUrl(path) {
    return root + path;
  }

  if(require && require.toUrl) {
    root = require.toUrl('.');
  } else {
    root = window.location.origin + '/';
  }

  return {
    toUrl: toUrl
  };
});

It uses require.toUrl('.') (with the global require object) to determine the baseUrl of my app.

Is it safe to use require.toUrl this way? Is this behavior stable? Is it documented somewhere? And if the answer is "no" to any of these questions, can you suggest a better alternative?

I noticed people on StackOverflow mentioning this technique, but I can't find any official documentation for this!

@jrburke
Copy link
Member

jrburke commented Oct 29, 2017

The only official docs for it are here, but it does not talk about the top level toUrl. However, the behavior you seems reasonable given that it is the top level toUrl, so it is not relative to any module ID, just the baseUrl.

There are no plans to change the behavior of toUrl, so its behavior should stay consistent for the 2.x series. There are no plans for a 3.x series.

Closing as a discussion ticket, but feel free to continue discussion here.

@jrburke jrburke closed this as completed Oct 29, 2017
@jslegers
Copy link
Author

jslegers commented Apr 25, 2019

I'm having another issue with toUrl.

See #515 (comment).

I submitted a pull request to fix this @ #1779.

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

No branches or pull requests

2 participants