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

Django PWA and AWS Static Files #48

Open
josylad opened this issue Jul 2, 2020 · 6 comments
Open

Django PWA and AWS Static Files #48

josylad opened this issue Jul 2, 2020 · 6 comments

Comments

@josylad
Copy link

josylad commented Jul 2, 2020

Hi,
my Django PWA is giving errors and I think it is related to the fact that I am serving all my static files from AWS S3.
How do make PWA to pick static files from AWS and work properly??

I get this error "Uncaught (in promise) TypeError: Request failed"

@josylad
Copy link
Author

josylad commented Jul 17, 2020

Fixed.

@josylad josylad closed this as completed Jul 17, 2020
@josylad josylad reopened this Jul 17, 2020
@ghost
Copy link

ghost commented Nov 25, 2020

Fixed.

I have the same issue. Could you please share the solution?

@saileshkush95
Copy link

I'm also facing same problem

@josylad
Copy link
Author

josylad commented Feb 13, 2021

Fixed.

I have the same issue. Could you please share the solution?

Hello,
I was able to fix it by creating my own service worker and then point at it using the PWA_SERVICE_WORKER_PATH variable (PWA_APP_FETCH_URL is passed through).

PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'my_app', 'serviceworker.js')

@josylad
Copy link
Author

josylad commented Feb 13, 2021

Hello,
@saileshkush95 @i-konuk
I was able to fix it by creating my own service worker and then point at it using the PWA_SERVICE_WORKER_PATH variable (PWA_APP_FETCH_URL is passed through).

PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'my_app', 'serviceworker.js')

This problem is due to the fact that Django cannot find all the static files listed in the Serviceworker.js file, so you should create a custom service worker, copy the exact one from the documentation and delete the url to files that do not exist on your server or edit the url to the correct one and it should work fine.

@josylad
Copy link
Author

josylad commented Feb 13, 2021

My Example Service worker.

// Base Service Worker implementation.  To use your own Service Worker, set the PWA_SERVICE_WORKER_PATH variable in settings.py

var staticCacheName = "django-pwa-v" + new Date().getTime();
var filesToCache = [
    // '/offline',
    'https://my-app.s3.amazonaws.com/static/images/my_app_icon2.png',
    'https://my-app.s3.amazonaws.com/static/images/pwa-app-icon.png',
    
];

// Cache on install
self.addEventListener("install", event => {
    this.skipWaiting();
    event.waitUntil(
        caches.open(staticCacheName)
            .then(cache => {
                return cache.addAll(filesToCache);
            })
    )
});

// Clear cache on activate
self.addEventListener('activate', event => {
    event.waitUntil(
        caches.keys().then(cacheNames => {
            return Promise.all(
                cacheNames
                    .filter(cacheName => (cacheName.startsWith("django-pwa-")))
                    .filter(cacheName => (cacheName !== staticCacheName))
                    .map(cacheName => caches.delete(cacheName))
            );
        })
    );
});

// Serve from Cache
self.addEventListener("fetch", event => {
    event.respondWith(
        caches.match(event.request)
            .then(response => {
                return response || fetch(event.request);
            })
            .catch(() => {
                return caches.match('offline');
            })
    )
});

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