Create-react-app: Images not caching while using PWA

Created on 22 Sep 2017  ยท  11Comments  ยท  Source: facebook/create-react-app

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

question

Most helpful comment

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.

All 11 comments

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} />

or indeed:

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

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

Ok let me check

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

Great it works now, I did put it in the src directory. Thanks @chee ๐Ÿ‘

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

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

Yep it works. Sorry in that issue someone said it doesn't work so I didn't test ๐Ÿ˜‚

I'll close this as resolved then. ๐Ÿ˜„

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

Can you answer this โ”

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DaveLindberg picture DaveLindberg  ยท  3Comments

AlexeyRyashencev picture AlexeyRyashencev  ยท  3Comments

Aranir picture Aranir  ยท  3Comments

stopachka picture stopachka  ยท  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  ยท  3Comments