question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with baseUrl and JavaScriptFetch, JavaScriptServiceWorkerRegistration...

See original GitHub issue

Right now assets have a baseUrl which defaults to their url, or the url of the baseUrl of their nearest non-inline ancestor. However this is a flawed concept for JavaScript as it has a much more complex situation than that. Notably import is resolved relative to baseUrl however fetch, navigator.serviceWorker.register and similar are relative to the URL of the including document. Currently the code treats these as the same, which results in unfortunate results such as navigator.serviceWorker.register('file:///home/kevincox/p/kevincox.ca/build/service-worker.js').

However this problem is basically impossible unless assetgraph knows all possible references to the JavaScript file. There are cases where we can probably assume this such as when using assetgraph-hashfiles however there are also cases where this can not be assumed, such as if you are providing a JavaScript library that you expect users (and possibly your website as well) to access off of a well-known URL.

I encountered this problem with serviceWorker.register but I believe the same issue exists with fetch.

I think the best solution here is to not modify the referenced URL when the URL of the asset containing the reference is changed. There are a couple of cases where we can do something “clever” by handling the case where the only user of the script is moved, however this action at a distance is probably more confusing than it is helpful.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
kevincoxcommented, Nov 1, 2020

Ok, I made a test case. It is roughly as simple as expected 🙂

1reaction
kevincoxcommented, Nov 1, 2020

I’ll see if I can extract it. The setup is fairly simple though.

  • index.html has <script src=sw.js>
  • sw.js has navigator.serviceWorker.register("/service-worker.js");
  • graph.findAssets({fileName: "sw.js"}).forEach(a => a.url = "https://cdn.example");
  • graph.writeAssetsToDisc({protocol: "file:"}, "dist/")

Completely untested but that is the gist of it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can fetch function work with a base url? #4439 - GitHub
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity...
Read more >
Fetch with absolute url prefix - node.js - Stack Overflow
Is there any way of avoiding the localhost part, other than simply placing localhost in a variable? const baseUrl = 'http://localhost'; fetch(` ...
Read more >
parcel-resolver-ts-base-url - npm
This is a big issue both when migrating existing projects leveraging these properties and for developers having preference for TypeScript path ...
Read more >
[SOLVED] What should be used for the value of .Site.BaseURL?
I've always filled this in with the URL of the site. This always seemed to cause problems from dev to testing to live,...
Read more >
Jekyll's site.url and baseurl - Made Mistakes
Further confusing the issue, GitHub Pages does not use the latest version of Jekyll to build sites. It is currently locked at 3.9.0,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found