Skip to content

Commit

Permalink
Make Webpacker::Manifest#lookup public API (#1853)
Browse files Browse the repository at this point in the history
I'm using [Service Workers](https://developers.google.com/web/fundamentals/primers/service-workers/?hl=en) with this gem.
To register a Service Worker in a browser, the path of `sw.js` file is needed.

```js
navigator.serviceWorker.register('/path-to-sw.js')`
```

But `Webpacker::Manifest#lookup` and `Webpacker::Manifest#lookup!` seem to be nodoc. Could you make them public API?
My usage of these methods is following:

```erb

 <!DOCTYPE html>
 <html>
   <head>
    <script>
      window.serviceWorkerPath = "<%= Webpacker.manifest.lookup!('sw.js') %>";
    </script>
    <link href="/manifest.json" rel="manifest">
```

```js
// app/javascript/packs/application.js

document.addEventListener("DOMContentLoaded", () => {
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register(window.serviceWorkerPath);
  }
});
```
  • Loading branch information
ttanimichi authored and gauravtiwari committed Jan 3, 2019
1 parent d82f405 commit 98235d3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/webpacker/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def refresh
@data = load
end

# Computes the relative path for a given Webpacker asset using manifest.json.
# If no asset is found, returns nil.
#
# Example:
#
# Webpacker.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
def lookup(name, pack_type = {})
compile if compiling?

Expand All @@ -39,6 +45,7 @@ def lookup(name, pack_type = {})
end
end

# Like lookup, except that if no asset is found, raises a Webpacker::Manifest::MissingEntryError.
def lookup!(name, pack_type = {})
lookup(name, pack_type) || handle_missing_entry(name)
end
Expand Down

0 comments on commit 98235d3

Please sign in to comment.