Skip to content

feat: Implement Progressive Web App (PWA) functionality#10

Open
GYFX35 wants to merge 1 commit intomainfrom
feature/add-pwa-support
Open

feat: Implement Progressive Web App (PWA) functionality#10
GYFX35 wants to merge 1 commit intomainfrom
feature/add-pwa-support

Conversation

@GYFX35
Copy link
Owner

@GYFX35 GYFX35 commented Aug 11, 2025

This commit turns the Flask application into a Progressive Web App.

Key changes include:

  • A manifest.json file to describe the application.
  • A sw.js service worker for basic offline caching.
  • Updates to templates/base.html to link the manifest and register the service worker.
  • New routes in app/routes.py to serve the PWA files.

Additionally, this commit includes fixes for the application's environment and a bug fix for a new version of a dependency:

  • Installed missing Python dependencies (Flask, SQLAlchemy, etc.).
  • Fixed an AttributeError in Flask-Babel due to an API change in version 4.0.0, updating the code to use the new locale_selector argument in the Babel constructor.
  • Installed wtforms-sqlalchemy which was a missing dependency.

Summary by Sourcery

Implement Progressive Web App functionality by adding a web app manifest, service worker for offline caching, updating the base template to register the service worker, and adding routes to serve PWA assets, along with dependency installations and a Flask-Babel compatibility fix.

New Features:

  • Add PWA support with a manifest.json, service worker (sw.js), and offline caching

Bug Fixes:

  • Fix Flask-Babel compatibility by using the new locale_selector argument in the Babel constructor

Chores:

  • Install missing Python dependencies including Flask, SQLAlchemy, and wtforms-sqlalchemy

This commit turns the Flask application into a Progressive Web App.

Key changes include:
- A `manifest.json` file to describe the application.
- A `sw.js` service worker for basic offline caching.
- Updates to `templates/base.html` to link the manifest and register the service worker.
- New routes in `app/routes.py` to serve the PWA files.

Additionally, this commit includes fixes for the application's environment and a bug fix for a new version of a dependency:
- Installed missing Python dependencies (Flask, SQLAlchemy, etc.).
- Fixed an `AttributeError` in Flask-Babel due to an API change in version 4.0.0, updating the code to use the new `locale_selector` argument in the Babel constructor.
- Installed `wtforms-sqlalchemy` which was a missing dependency.
@sourcery-ai
Copy link

sourcery-ai bot commented Aug 11, 2025

Reviewer's Guide

Transforms the Flask application into a PWA by adding a web app manifest and service worker for offline caching, integrating them into the base template and routing, and updating dependencies including a Babel API fix.

Sequence diagram for service worker registration and caching

sequenceDiagram
    participant User
    participant Browser
    participant ServiceWorker
    User->>Browser: Load CARE web app
    Browser->>Browser: Load manifest.json
    Browser->>ServiceWorker: Register sw.js
    ServiceWorker->>ServiceWorker: Install event (cache resources)
    Browser->>ServiceWorker: Fetch resource
    ServiceWorker-->>Browser: Serve cached resource if available
Loading

Class diagram for new PWA-related Flask routes

classDiagram
    class FlaskApp {
        +manifest()
        +service_worker()
    }
    FlaskApp : manifest() serves manifest.json
    FlaskApp : service_worker() serves sw.js
Loading

File-Level Changes

Change Details Files
Add PWA static assets for offline support
  • Create manifest.json with app metadata and icon definitions
  • Implement sw.js with install and fetch event handlers to cache assets
app/static/manifest.json
app/static/sw.js
Integrate PWA in base template
  • Link manifest.json in the document head
  • Add service worker registration script to window load event
templates/base.html
Serve PWA files via new Flask routes
  • Add /manifest.json endpoint using send_from_directory
  • Add /sw.js endpoint for service worker
app/routes.py
Update dependencies and fix Babel locale_selector API change
  • Include missing Python dependencies (Flask, SQLAlchemy, wtforms-sqlalchemy)
  • Update Babel initialization to use the new locale_selector argument
app/__init__.py
requirements.txt

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @GYFX35 - I've reviewed your changes - here's some feedback:

  • Align the manifest.json start_url and your urlsToCache with the actual Flask routes (e.g. use “/” instead of “/index”) so the service worker doesn’t miss any pages.
  • Add an activate event handler in sw.js to clean up old caches when you bump CACHE_NAME, preventing clients from holding onto stale assets.
  • Instead of adding custom routes for manifest.json and sw.js, leverage Flask’s built-in static file serving (or adjust static_url_path) to avoid duplicating static routes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Align the manifest.json start_url and your urlsToCache with the actual Flask routes (e.g. use “/” instead of “/index”) so the service worker doesn’t miss any pages.
- Add an activate event handler in sw.js to clean up old caches when you bump CACHE_NAME, preventing clients from holding onto stale assets.
- Instead of adding custom routes for manifest.json and sw.js, leverage Flask’s built-in static file serving (or adjust static_url_path) to avoid duplicating static routes.

## Individual Comments

### Comment 1
<location> `app/static/sw.js:25` </location>
<code_context>
+        if (response) {
+          return response;
+        }
+        return fetch(event.request);
+      })
+  );
</code_context>

<issue_to_address>
Consider adding error handling for failed network requests in the fetch event.

Providing a fallback response or custom offline page will improve user experience when fetch(event.request) fails.

Suggested implementation:

```javascript
        return fetch(event.request).catch(() => {
          return caches.match('/offline.html');
        });

```

Make sure that `/offline.html` is cached during the service worker's install event so it is available as a fallback. If you do not already cache it, add it to your cache list in the install handler.
</issue_to_address>

### Comment 2
<location> `app/routes.py:262` </location>
<code_context>
         return redirect(url_for('login'))
     return render_template('register.html', title='Register', form=form)
+
+@app.route('/manifest.json')
+def manifest():
+    return send_from_directory('static', 'manifest.json')
+
+@app.route('/sw.js')
</code_context>

<issue_to_address>
Consider setting appropriate MIME type for manifest.json response.

Set the mimetype to 'application/manifest+json' or 'application/json' in send_from_directory to ensure proper browser parsing.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
@app.route('/manifest.json')
def manifest():
    return send_from_directory('static', 'manifest.json')
=======
@app.route('/manifest.json')
def manifest():
    return send_from_directory('static', 'manifest.json', mimetype='application/json')
>>>>>>> REPLACE

</suggested_fix>

### Comment 3
<location> `app/routes.py:266` </location>
<code_context>
+def manifest():
+    return send_from_directory('static', 'manifest.json')
+
+@app.route('/sw.js')
+def service_worker():
+    return send_from_directory('static', 'sw.js')
</code_context>

<issue_to_address>
Consider setting the correct MIME type for the service worker script.

Ensure 'sw.js' is served with 'application/javascript' to prevent registration issues and browser warnings if the default is incorrect.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

if (response) {
return response;
}
return fetch(event.request);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider adding error handling for failed network requests in the fetch event.

Providing a fallback response or custom offline page will improve user experience when fetch(event.request) fails.

Suggested implementation:

        return fetch(event.request).catch(() => {
          return caches.match('/offline.html');
        });

Make sure that /offline.html is cached during the service worker's install event so it is available as a fallback. If you do not already cache it, add it to your cache list in the install handler.

Comment on lines +262 to +264
@app.route('/manifest.json')
def manifest():
return send_from_directory('static', 'manifest.json')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider setting appropriate MIME type for manifest.json response.

Set the mimetype to 'application/manifest+json' or 'application/json' in send_from_directory to ensure proper browser parsing.

Suggested change
@app.route('/manifest.json')
def manifest():
return send_from_directory('static', 'manifest.json')
@app.route('/manifest.json')
def manifest():
return send_from_directory('static', 'manifest.json', mimetype='application/json')

Comment on lines +266 to +268
@app.route('/sw.js')
def service_worker():
return send_from_directory('static', 'sw.js')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider setting the correct MIME type for the service worker script.

Ensure 'sw.js' is served with 'application/javascript' to prevent registration issues and browser warnings if the default is incorrect.

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

Successfully merging this pull request may close these issues.

1 participant