Skip to content

Images not caching while using PWA #3174

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
deadcoder0904 opened this issue Sep 22, 2017 · 11 comments
Closed

Images not caching while using PWA #3174

deadcoder0904 opened this issue Sep 22, 2017 · 11 comments

Comments

@deadcoder0904
Copy link

The website is here & the code is here

I have put my images in public/assets folder but don't know how to configure service-worker to cache images when there is no internet connection

I did read the docs & from what I understood it caches automatically I guess 🤔, but not sure

So when I visit the above website offline, it shows only text & no images are served while offline 😢

Please take a look

@chee
Copy link

chee commented Sep 22, 2017

if you import the images from within the javascript (similar to how you've imported the css) they will be precached

for instance, you could put your assets directory inside src and do

import add from '../assets/add.jpg'

const SausageComponent = () =>
  <img src={add} />

@chee
Copy link

chee commented Sep 22, 2017

or indeed:

import add from '../assets/add.jpg'

const SausageComponent = () =>
  <Parallax.Layer
    style={{
      backgroundImage: `url(${add})`
    }}
  />

@deadcoder0904
Copy link
Author

Ok let me check

@deadcoder0904
Copy link
Author

Also my question is can't we do it by keeping it in public folder only ❓

@deadcoder0904
Copy link
Author

Great it works now, I did put it in the src directory. Thanks @chee 👍

@deadcoder0904
Copy link
Author

Also can it import() programmatically (perhaps using System.import()) an Array of Images & I've read #2545 where the solution doesn't work ❔

@Timer
Copy link
Contributor

Timer commented Sep 22, 2017

If you follow my example in that issue it'll work fine, you need to use require not import to grab arrays of images.

@deadcoder0904
Copy link
Author

Yep it works. Sorry in that issue someone said it doesn't work so I didn't test 😂

@Timer
Copy link
Contributor

Timer commented Sep 22, 2017

I'll close this as resolved then. 😄

@deadcoder0904
Copy link
Author

Also my question is can't we do it by keeping it in public folder only ❓

Can you answer this ❔

@Timer
Copy link
Contributor

Timer commented Sep 22, 2017

The public/ folder is not processed by webpack, and thus cannot be added to the service worker or any other benefits.

Having them in src/ and using require makes webpack aware of them and automatically puts them in your build for you.

@Timer Timer closed this as completed Sep 22, 2017
@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

3 participants