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

Service Worker path & embedding in Rails app #59

Closed
mcampos-quinn opened this issue Aug 1, 2021 · 3 comments
Closed

Service Worker path & embedding in Rails app #59

mcampos-quinn opened this issue Aug 1, 2021 · 3 comments

Comments

@mcampos-quinn
Copy link

Just documenting what I figured out to embed replayweb.page in a Ruby on Rails app (a Blacklight discovery implementation). Still working on our implementation of embedding WARCs, but in case anyone else runs into the same issues I did just getting it running I hope this is helpful.

I had a heck of a time getting Rails to register the service worker at ./replay/sw.js. First I tried registering the service worker as an object in the Rails app, but it turned out to be a red herring since the ui.js script really does all the heavy lifting. All I needed was to get the right path to sw.js which turned out to be tricky! I ended up using a gem called rails-static-router that allowed me to reroute the request for the sw.js file to the actual path on disk (in the Rails Public folder). So (using Rails 5):

/my-app/Gemfile

gem 'rails-static-router'

/my-app/config/routes.rb

get '/root-path/replay/sw.js', to: static('/replay/sw.js') # the `replay` folder is inside the app `Public` folder

That's it!

@ikreymer
Copy link
Member

ikreymer commented Aug 1, 2021

Hi @mcampos-quinn, thanks for sharing this, and great to see this use case! Is there anything that you think would make this easier to work with Rails?

The idea was that users could just create a directory called replay that could contain the sw.js. Is that harder to do?

Would ./static/replay/sw.js be easier? If so, the path can be changed with the replayBase attribute, eg.

<replay-web-page replayBase="./static/replay/" ... >

or any other thoughts on making it be easier to integrate?

Embedding is a key use case for replaywebpage so hoping to make this as simple as possible!

@mcampos-quinn
Copy link
Author

Honestly I'm not sure, I'm no Rails expert 🙃. I tried declaring different paths and using routing to redirect was the simplest solution I found. It would be great to not add another dependency to our code though! I'll do some more tinkering.

@mcampos-quinn
Copy link
Author

Wow, turns out I had tried and rejected using the replayBase attribute too quickly! I tried it again and it works as expected, with the replay/sw.js path under public in my app. Truly magical stuff.

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