# Different remote and local resource URLs, with Service Workers!

DevFeed: [Different remote and local resource URLs, with Service Workers!](<https://devfeed.tech/articles/different-remote-and-local-resource-urls-with-service-workers-52102.md>)

Original publisher: [Read original article](<https://lea.verou.me/2017/10/different-remote-and-local-resource-urls-with-service-workers/>)

Author: Lea Verou

Published: 2017-10-30T00:00:00Z

Content type: article

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [software-development](<https://devfeed.tech/topics/software-development.md>), [PWA](<https://devfeed.tech/topics/pwa.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [blog](<https://devfeed.tech/tags/blog.md>), [development](<https://devfeed.tech/tags/development.md>), [http](<https://devfeed.tech/tags/http.md>), [https](<https://devfeed.tech/tags/https.md>), [js](<https://devfeed.tech/tags/js.md>), [library](<https://devfeed.tech/tags/library.md>), [local](<https://devfeed.tech/tags/local.md>), [localhost](<https://devfeed.tech/tags/localhost.md>), [mavo](<https://devfeed.tech/tags/mavo.md>), [original](<https://devfeed.tech/tags/original.md>), [remote](<https://devfeed.tech/tags/remote.md>), [repo](<https://devfeed.tech/tags/repo.md>), [request](<https://devfeed.tech/tags/request.md>), [resource](<https://devfeed.tech/tags/resource.md>), [resources](<https://devfeed.tech/tags/resources.md>), [service-workers](<https://devfeed.tech/tags/service-workers.md>), [solutions](<https://devfeed.tech/tags/solutions.md>), [test](<https://devfeed.tech/tags/test.md>), [tips](<https://devfeed.tech/tags/tips.md>), [url](<https://devfeed.tech/tags/url.md>), [urls](<https://devfeed.tech/tags/urls.md>), [web](<https://devfeed.tech/tags/web.md>), [web-development](<https://devfeed.tech/tags/web-development.md>)

## AI overview

The author's Service Worker setup lets local Mavo development keep the normal remote URLs in HTML: on localhost, it intercepts matching Mavo requests, tries a local server URL with a cache-busting timestamp, and falls back to the original request if the local fetch fails. The author says this works more smoothly than earlier approaches, with caveats: the worker only takes effect on a cached page load, must be copied into each repository because of same-origin limits, and may need explanation for new contributors. Earlier options include manually switching URLs, which risks committing a broken local URL; an onerror fallback, which adds a failed request before loading remotely and exposes debug URLs; conditional JavaScript URL selection, which the author calls clunky; and gulp copying and watching, which requires remembering to run the watcher and maintaining copies. The excerpt also lists untested alternatives: .htaccess redirects, symbolic links, user scripts, and hosts-file changes.

## Source excerpt

I often run into this issue where I want a different URL remotely and a different one locally so I can test my local changes to a library. Sure, relative URLs work a lot of the time, but are often not an option. Developing Mavo is yet another example of this: since Mavo is in a separate repo from mavo.io (its website) as well as test.mavo.io (the testsuite), I can't just have relative URLs to it that also work remotely. I've been encountering this problem way too frequently pretty much since I started in web development. In this post, will describe all solutions and workarounds I've used over time for this, including the one I'm currently using for Mavo: Service Workers! The manual one Probably the first solution everyone tries is doing it manually: every time you need to test, you just change the URL to a relative, local one and try to remember to change it back before committing. I still use this in some cases, since us developers are a lazy bunch. Usually I have both and use my editor's (un)commenting shortcut for enabling one or the other: <script src="https://get.mavo.io/mavo.js"></script> <!--<script src="../mavo/dist/mavo.js"></script>--> However, as you might imagine, this approach has several problems, the worst of which is that more than once I forgot and committed with the active script being the local one, which resulted in the remote website totally breaking. Also, it's clunky, especially when it's two resources whose URLs you need to change. The JS one This idea uses a bit of JS to load the remote URL when the local one fails to load. <script src="http://localhost:8000/mavo/dist/mavo.js" onerror="this.src='https://get.mavo.io/mavo.js'"></script> This works, and doesn't introduce any cognitive overhead for the developer, but the obvious drawback is that it slows things down for the server since a request needs to be sent and fail before the real resource can be loaded. Slowing things down for the local case might be acceptable, even though undesirable,